- Identifiers are names of variables, functions, classes etc. The name used as an identifier must follow the following rules in JavaTM technology.
- Each character is either a digit, letter, underscore(_) or currency symbol ($,�, � or �)
- First character cannot be a digit.
- The identifier name must not be a reserved word.
- A keyword or reserved word in Java technology has special meaning and cannot be used as a user defined identifier. The list of keywords in Java technology is given below. It is important to completely remember this list as you can expect a question in Java Certification exam related to this.
abstract boolean break byte case catch char class const continue default do double else extends final finally float for goto if implements import instanceof int interface long native new null package private protected public return short static strictfp super switch synchronized this throw throws transient try void volatile while assert enum It is important to note the following
- const and goto are not currently in use.
- null, true, and false are reserved literals but can be considered as reserved words for the purpose of exam.
- It is important to understand that Java language is case-sensitive. So even though super is a keyword, Super is not.
- All the Java technology keywords are in lower case.
- strictfp is a new keyword added in Java 1.2. assert is added in Java 1.4 and enum in Java 5.0
- The list of keywords as defined by Sun is present here.
- A literal in Java technology denotes a constant value. So for example 0 is an integer literal, and ‘c’ is a character literal. The reserved literals true and false are used to represent boolean literals. “This is a string” is a string literal.
- Integer literals can also be specified as octal (base 8), or hexadecimal (base 16). Octal and hexadecimal have 0 and 0x prefix respectively. So 03 and 0x3 are representation of integer three in octal and hexa-decimal respectively.
- Java technology supports three type of comments
- A single line comment starting with //
- A multi-line comment enclosed between /* and */
- A documentation or javadoc comment is enclosed between /** and */. These comments can be used to generate HTML documents using the javadoc utility, which is part of Java language.
- Java technology supports the following primitive types – boolean (for representing true or false), a character type called char, four integer types (byte, short, int and long) and two floating point types (float and double). The details of these types are given below –
Data types Width (in bytes) Minimum value Maximum Value byte 1 -27 27 – 1 short 2 -215 215-1 int 4 -231 231 – 1 long 8 -263 263 – 1 char 2 0x0 0xffff float 4 1.401298e-45 3.402823e+38 double 8 4.940656e-324 1.797693e+308 - Corresponding to all the primitive type there is a wrapper class defined. These classes provide useful methods for manipulating primitive data values and objects.
Data types Wrapper class int Integer short Short long Long byte Byte char Character float Float double Double - Instance variables (data members of a class) and static variables are initialized to default values. Local variables (i.e. variables defined in blocks or inside member functions) are not initialized to default values. Local variables must be explicitly initialized before they are used. If local variables are used before initialization, compilation error gets generated. The defaults for static and instance variables are given in the table below.
Data types Default Values boolean false char ‘\u0000’ Integer types (byte, short, int, long) 0 Floating types (float, double) 0.0F or 0.0 D Object References null public static void main(String args[]) { int i; System.out.println(i); }
In this example printing of i generates a compilation error because local variable i is used before being initialized. The initialization of instance and static variables is an important concept both for understanding of Java language, and for Java Certification exam.
- A Java source file has the following elements in this specific order.
- An optional package statement. All classes and interfaces defined in the file belong to this package. If the package statement is not specified, the classes defined in the file belong to a default package. An example of a package statement is –
package testpackage;
- Zero or more import statements. The import statement makes any classes defined in the specified package directly available. For example if a Java source file has a statement importing the class “java.class.Button”, then a class in the file may use Button class directly without providing the names of the package which defines the Button class. Some examples of import statement are –
import java.awt.*; // All classes in the awt package are imported.
import java.applet.Applet; - Any number of class and interface definitions may follow the optional package and import statements.
If a file has all three of the above constructs, they must come in the specific order of package statement, one or more import statements, followed by any number of class or interface definitions. Also all the above three constructs are optional. So an empty file is a legal Java file.
- An optional package statement. All classes and interfaces defined in the file belong to this package. If the package statement is not specified, the classes defined in the file belong to a default package. An example of a package statement is –
- The Java interpreter executes a method called main, defined in the class specified in command line arguments. The main method is the entry point for execution of a class. In Java technology the main method must have the following signature –
public static void main(String args[])
The java interpreter is invoked with the name of the class as an argument. The class name is followed by possible set of arguments for the main function of the class. When a Java program is invoked then the Java interpreter name “java” and the class name are not passed to the main() method of the class. The rest of the command line arguments are passed as an array of String. For example invokingjava Sky blue gray
would invoke main method of Sky class with an array of two elements – blue and gray.