java:basic

Basic Syntax

Java supports two forms of comments:

  • /* */ - which are block style comments and can span multiple lines
  • '' - which are line style commments and can only apply to a single line. ===== Variables ===== Any variable in Java has three main components; namely its name, its type and its scope. ==== Variable Names ==== There are very few restrictions on the names of Java variables. The only restrictions are: * Can consist of upper and lower case letters, digits, dollar sign ($) and the underscore ( _ ) character. * Must begin with a letter, dollar sign, or an underscore * Are case sensitive * Keywords cannot be used as identifiers * Within a given section of your program or scope, each user defined item must have a unique identifier * Can be of any length. ==== Variable Scope ==== Any variable defined inside a block or a method is called an Instance Variable. Any variable defined outside a block and inside the class is called a Class Variable ==== Variable Types ==== There are 4 main primative types in Java: * Integer: byte, short, int, and long. * Floating Point: float and double * Character: char * Boolean: variable with a value of true or false. Their default values are shown in the table below: ^Data Type^Default Value (for fields)^Range^ |byte|0|-127 to +128| |short|0|-32768 to +32767| |int|0| | |long|0L| | |float|0.0f| | |double|0.0d| | |char|'\u0000'|0 to 65535| |String (object)|null| | |boolean|false| | ===== Classes ===== A class is nothing but a blueprint for creating different objects which defines its properties and behaviors. An object exhibits the properties and behaviors defined by its class. A class can contain fields and methods to describe the behavior of an object. Methods are nothing but members of a class that provide a service for an object or perform some business logic. ===== Objects ===== An object is an instance of a class created using a new operator. The new operator returns a reference to a new instance of a class. This reference can be assigned to a reference variable of the class. The process of creating objects from a class is called instantiation. An object reference provides a handle to an object that is created and stored in memory. In Java, objects can only be manipulated via references, which can be stored in variables. ===== Interface ===== An Interface is a contract in the form of collection of method and constant declarations. When a class implements an interface, it promises to implement all of the methods declared in that interface. Instance Members Each object created will have its own copies of the fields defined in its class called instance variables which represent an object’s state. The methods of an object define its behaviour called instance methods. Instance variables and instance methods, which belong to objects, are collectively called instance members. The dot '.' notation with a object reference is used to access Instance Members. ===== Static Members ===== Static members are those that belong to a class as a whole and not to a particular instance (object). A static variable is initialized when the class is loaded. Similarly, a class can have static methods. Static variables and static methods are collectively known as static members, and are declared with a keyword static. Static members in the class can be accessed either by using the class name or by using the object reference, but instance members can only be accessed via object references. === Example of all the components above === An example of all the components above is shown in listing 1 below: <code java> /** Comment * Displays “Hello World!” to the standard output. */ public class HelloWorld { String output = “”; static HelloWorld helloObj; – Line 1 public HelloWorld(){ output = “Hello World”; } public String printMessage(){ return output; } public static void main (String args[]) { helloObj = new HelloWorld(); – Line 2 System.out.println(helloObj.printMessage()); } } </code> with the results: |Class Name|HelloWorld| |Object Reference|helloObj (in Line 1)| |Object Created|helloObj (In Line 2)| |Member Function|printMessage| |Field|output (String)| |Static Member|helloObj| |Instance Member|output (String)|
  • java/basic.txt
  • Last modified: 01/06/13 @ 20:54:16
  • by 127.0.0.1