Showing posts with label Java Basic. Show all posts
Showing posts with label Java Basic. Show all posts

Dec 16, 2008

Identifiers in Java

Some points about Identifiers
  1. A name in a program is called an identifier.
  2. An identifier is sequence of characters which can be a letter, digit, connecting characters (Underscore _) or a currency symbol ($, ¢, ¥ or £).
  3. Identifier cannot start with a digit, after first character digits are allowed.
  4. Identifiers in java are case sensitive i.e. test and Test are two different Identifiers.
  5. Identifier can be of any length
  6. Keywords cannot be used as an Identifier.

Illegal Identifier

  1. 45abcd – Starting with a digit
  2. abcd@efgh – @ is not allowed
  3. new – Keyword not allowed

Legal Identifier

  1. $$ – $ is allowed
  2. Ab88cd – Digits are allowed after first character.
  3. Abc_88 – Underscore is a valid connecting character.

Important Point about hash code

Important points about hash code
  1. Hash code is used for increasing the performance of large collection of data.
  2. Hash code is not always unique.
  3. Hash code only tell about the bucket to go into, but not how to locate the name once we are in that bucket.
  4. Collection use the hash code value of the object to decide in which bucket / slot the object should land.
  5. If two objects have same hash code value then it is not necessary that they are equal.
  6. Hashing is a two step process firstly search the right bucket using the hash code value, then search for the element in the bucket using equals()

Dec 15, 2008

Basics of Java

Complexity can be handled by abstraction. In OOPS abstraction is modeled using Classes and objects.
A Class models abstraction by defining properties and behavior of an object.
Properties of an object are defined by the attributes, which are fields in java. A field in java is a variable that can hold value.
Behavior of an object is defined by the methods in java.

Object is an instance of the class. In java objects can only be manipulated using references.
Each Object created maintains its own copy of instance variables. Two objects can have same state if the values of their instance variable are same.
Object communicates with each other using message passing.

Static members/methods
  1. Certain members belong to Class only not to object these are called static members.
  2. A static member is initialized when the class is loaded at runtime.
  3. Certain methods that belong to the Class only and not to an object are called static methods.
  4. Client can access static methods using the class name.
  5. Static members of the Class can be accessed either using Object reference or using Class name.
Example of Class & Object
package Test;
//Class declaration
public class Example_Class {
//Attribute declaration - These variables defines the state of the object
private int first_attribute;
private int second_attribute;
//constructor
public Example_Class(int x, int y) {
//Initializing attributes of newly created object.
this.first_attribute = x;
this.second_attribute =y;
}
//Method - These methods define the behavior of the object
public int addResult() {
return first_attribute + second_attribute;
}
//Main method of Class
public static void main(String[] args) {
/* Object instantiation - Object is created using "new" keyword With new keyword Constructor of the class is called which returns the reference of the newly created object, which is assigned to
reference variable of appropriate Class.
*/
// objFirst ---------> Object1(1,2)
Example_Class objFirst = new Example_Class(1, 2);
//Both objFirst and objSecond object refernces will point to two different objects in heap
// objSecond ---------> Object2(1,2)

Example_Class objSecond = new Example_Class(1, 2);
int addResultFirst = objFirst.addResult();
System.out.println("Add Result First:" + addResultFirst);
//Calling method on second created Object
int addResultSecond = objSecond.addResult();
System.out.println("Add Result Second:" + addResultSecond);
}
}

Jul 25, 2008

Finalization in Java

protected void finalize() throws Throwable

  1. It is declared in Object class.
  2. Is invoked by the garbage collector after it determines that this object is no longer reachable and its space is to be reclaimed.
  3. finalize is that it is invoked if and when the JavaTM virtual machine has determined that there is no longer any means by which this object can be accessed by any thread that has not yet died, except as a result of an action taken by the finalization of some other object or class which is ready to be finalized.
  4. Subclasses of Object may override this definition.
  5. It is guaranteed that the thread that invokes finalize() will not be holding any user-visible synchronization locks when finalize() is invoked.
  6. Any exception thrown by the finalize() causes the finalization of this object to be halted, but is otherwise ignored.
  7. finalize() is invoked at most once per object, even if execution of this method causes the object to become reachable again and later it becomes unreachable again.