Friday, May 12, 2006

Java : Introduction - 1

Java program structure:


Application : An application (i.e. program )is a source code that is compiled and directly executed. In Java, to create an application, the program must have a class that defines a method called main(). The main() method in the class is the starting point for execution of any application.

Class : A class is a model of abstraction of a category of objects, defining their properties (fields) & behaviors (methods). It acts as a blue print for creating such objects. These fields & methods in a class definition are collectively called members in Java. The process of creating an object from a class is called instantiation.


Creating a object reference

String str1, str2;

Instantiation

str1 = new String(“hi this is sk”);

The new operator returns a reference of a new instance of object of String class, which can then be assigned to a reference variable of the same class.

Object : Each object has a unique identity and has its own copy of the fields declared in the class.It many have several reference variables (also called references or reference values) pointing to it, often called aliases.

UML Notation:

a. Notation for Objects:

str1 : String


str1 : String

b. Anonymous object (reference variable omitted)

: String

c. Explicit reference for Java objects

str1 : Ref(String)

------>

: String

Automatic garbage collection : when objects are no longer in use, their memory can be reclaimed & reallocated, if necessary, by the automatic garbage collection function of the Java runtime system.

Static members : These members are declared as static in the class definition and belong to the class and not to any object of the class. These are also called class members. These can be accessed by referencing the class name as well as via object references.


Static Variable

A static variable is initialized when the class is loaded at runtime. Also called static field and class variable.

Static methods

Also called class methods.


Instance members : These are instance fields & instance methods of an object collectively known as instance members. These can be accessed or invoked through an object reference.


Instance Variables

The fields which are allocated when the class is instantiated by creation of an object are called instance variables. These are also called non-static fields.

Their value represents the state of the object.

Instance methods

A method that belongs to an instance (object) of the class. Objects of the same class share its implementation.

The methods of the object defines its behavior.

For eg : String.valueOf(“9999.98”);

where valueOf() is a static method defined in the wrapper class String in java.lang.String package.

Objects communicate by passing messages. A method call spells out the complete message, the object, the method and the arguments. A dot '.' is used in java to to make the appropriate connection.

Eg :

String str1=”how big am i?”;

System.out.println(str1+ “ : “ +str1.length()) -> returns the length of the string str1.

A field may be accessed similiarly using the '.' dot. The use of dot is governed by the accessibility of the member.


public

accessibility inside & outside the class.

private

accessibility within the class.

protected

accessibility within the class and sub-classes (package accessibility?)


Inheritance :

Class Car entends Vehicle {

Car(String str){ // constructor calling super-class

super(String str);

}

}

Here Car is the subclass and Vehile is the superclass it is inheriting by extending it. A Superclass is a generalization where as a subclass is its specialization. A class can extend only one superclass. The subclass inherits the public & protected members of the superclass.

Inheritance relation UML diagram:


Vehicle

/\

|

Car

Aggregation : Building a composite object from other constituent objects that are its parts. Java supports aggregation of objects by reference since objects can't contain other objects explicitly.

Aggregation relation UML diagram:

Vehicle

ColorOfVehicle

Engine

noOfWheels

CostInWork()

timeToFinish()


has

<>-------------





Engine


Executing Java program :

The source file must be named with .java extention. The name of the file is Case-sensitive. Must be the same as the public class name.


Compilation

javac Test1.java

return Test1.class file containing java byte code.

Running

java Test1

runs the main() method from the specified class Test1

To start an application, the main() method of the public class is invoked my the Java interpreter (JVM). It must be declared as following :

public static void main(String[] argv){}


public

means the method is accessible from any class

static

Means the method belongs to the class

void

the method does not return any value

String[]

An array of strings used to pass information to the main method when the application is invoked


Comments: Post a Comment



<< Home
Advertisements

eBay.in - The World's Online Marketplace

ARCHIVES
  • March 2006
  • April 2006
  • May 2006
  • June 2006
  • July 2006
  • August 2006
  • September 2006

This page is powered by Blogger. Isn't yours?

Recent Postings

Google