Friday, April 2, 2010

Various Classes in Java technology


The Object class
All classes in JavaTM technology are directly or indirectly derived from the Object class. Some of the subclasses of Object class are - Boolean, Number, Void, Math, String, StringBuffer etc.
Some of the important methods defined in the Object class are given below. These methods are available to all Java classes.
  1. boolean equals(Object obj) - The equals method in Object class returns true if two references point to the same object. Some classes like String and Boolean overload this method. 
  2. String toString() - The function is used to convert objects to String. If a subclass does not override this method, the method returns a textual representation of the object, which has the following format : @".
  3. The following methods related to threads are also defined in Object class -
    void notify()
    void notifyall()
    void wait(long timeout) throws InteruptedException
    void wait(long timeout, int nanosec) throws InteruptedException
    void wait() throws InteruptedException
Wrapper classes
Corresponding to all the primitive types Java technology defines wrapper classes. Some examples of these wrapper classes are - Character, Boolean, Integer, Double.
Important methods in the Math class
Some of the methods defined in the Math class are used frequently. These are explained below. Besides the functionality, it is important to understand the arguments and return type of these functions.static double ceil(double(d)) : The method ceil returns the smallest double value equal to a mathematical integer, that is not less than the argument. For example,


ceil(3.4) returns 4.0

ceil(-2.3) returns -2.0

ceil(3.0) returns 3.0

static double floor(double(d)) : The method floor returns the largest double value equal to a mathematical integer, that is not greater than the argument. For example,


floor(3.4) returns 3.0

floor(-2.3) returns -3.0

floor(3.0) returns 3.0

static int round (float f) and static long round(double d) : The method round returns the integer closest to the argument.


round(3.7) returns 4

round(3.2) returns 3

round(3.0) returns 3

round(-3.1) returns -3

String class
The String class is used to implement immutable character strings. This means that the character string cannot be changed once it has been created. Some of the important methods are explained below.int length() - The number of characters in the String class are returned by the length() method. String substring(int startIndex) String substring(int startIndex, int endIndex) The method substring extracts a substring from a string. The method extracts a string from the startIndex to the index endIndex - 1. If endIndex is not specified then string till the end of input string is returned. The example below illustrates this


String str = "I am a string";

int len = str.length();

String str2 = str.substring(2,5);

After the above statements str2 contains the string "am ". The string str still has the same value "I am a string". The variable len has value 13.
StringBuffer class
The StringBuffer class implements mutable strings. This means that the characters stored in the string and the capacity of the string can be changed.
Garbage Collection
Java technology's Garbage collection is complex. In this section I am only giving a brief overview of Garbage Collection. Java technology supports automatic garbage collection. This means that the programmer does not need to free the memory used by objects. Java technology's runtime environment can claim the memory from the objects that are no longer in use. Objects that are not being referred become candidates for garbage collection. It is important to note that these objects are candidates only. Java technology does not guarantee that Garbage collection would happen on these objects. Before actually freeing up the memory, garbage collector invokes the finalize() method of the Object being freed. The System.gc() method can be invoked by the program to suggest to Java technology that the Garbage Collector be invoked. However there is no guarantee when the garbage collection would be invoked. There is also no guarantee on the order in which objects will be garbage collected. The example illustrates when a string Object becomes available for Garbage Collection.


public class GCTest {

public static void main(String args[]) {

String a,b;

String c = new String("test");

a = c;

c = null; // The String "test" is not yet 

//available for GC as a still points to "test"

b = new String("xyz");

b = c; // String "xyz" is now available for GC.

a = null; 

//String "test" is now available for GC.

}

}

Operators in java

Operators in java are similar to c++ and c.
Operators are actions that manipulate, combine or compare variables.

Introduction to Operators in java
Covering Operators one by one
Operator Precedence

Syntax and Grammar of Java

  1. Identifiers are names of variables, functions, classes etc.
  2. A keyword or reserved word 
  3. Java technology supports three type of comments
  4. Read about full lexical structure of java
  5. 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.
  6. Java technology supports the 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). Click here for more.
  7. Corresponding to all the primitive type there is a wrapper class defined. These classes provide useful methods for manipulating primitive data values and objects. See here. 
  8. Limits of these datatypes
  9. Default values of primary datatypes 
  10. Variables 
  11. A Java source file format
  12. 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 invoking java Sky blue gray
    would invoke main method of Sky class with an array of two elements - blue and gray. 
    args[0]=blue , args[1] = gray


Chitika