Tuesday, October 4, 2011

Method

Method is used to show the behavior of the object.

Example of Human class:

class Human
{
Human()
{
System.out.println("I am Human");
}
Human(int a)
{
System.out.println("I am Human"+" "+a);
}
void sing()
{
System.out.println("I can sing");
}
void play()
{
System.out.println("I play cricket");
}
public static void main(String ar[])
{
Human h=new Human();
h.sing(); h.play();
Human h1=new Human();
h1.sing();
Human h2=new Human(10);
h2.play();
}
}


Constructor overloading

class ConstOver
{
ConstOver()
{
System.out.println("I am Human");
}
ConstOver(int a)
{
System.out.println("I am Human"+" "+a);
}
ConstOver(int a,int b)
{
int c=a+b;
System.out.println("I am Human"+" "+c);
}
ConstOver(String s)
{
System.out.println("I am Human"+" "+s);
}
public static void main(String ar[])
{
new ConstOver();
new ConstOver(1);
new ConstOver(1,1);
new ConstOver("Om");
}
}

Constructor

Constructor is used to show the behavior of the class and
it also use to create the object of the class.

Name of constructor is same as class name.

When we create object of class, constructor call automatically.

Constructor has no return type.

It show the behavior of class, so name of constructor is same as name of class.

"new" keyword is used to allocate memory for object.

When we don't define a constructor in our program then JVM provides default constructor.

Without parametrized constructor is default constructor.


In following Example constructor is used only to show the behavior of the Human class.

class Human
{
Human()
{
System.out.println("I am Human");
}
public static void main(String arg[])
{
new Human();
}
}

In following Example constructor is used to show the behavior of the Human class and also use to create the object of the Human class.

class Human
{
Human()
{
System.out.println("I am Human");
}
public static void main(String arg[])
{
Human h;
h=
new Human();
}
}


Note:
Human h;
h is reference of Human class. It has no memory.

h=new Human();
Here we used "new" keyword to allocate the memory for h.
In this example constructor call automatically because we created object h of class Human.
We use following single line code for creating object of a class,
which is simple form of above two line code.

Human h=new Human();






Saturday, October 1, 2011

Simple Program

class Hello
{
public static void main(String arg[])
{
System.out.println("Hello, I am Human");
}
}

Brief consideration in above simple program:
class: we are declaring a new class using keyword 'class'.
Hello is the name of class. We put all the code( members of class) between opening curly braces({) and closing curly braces(}).
public- It will available the main method to the interpreter.
static- Allow to call the main() without instantiation of object.
void- no return type.
main() method is called at run time.
String ar[]: array of String type.
We pass the parameter (String ar[]). It means we are able to take input from command line as string.
It is always necessary to pass this parameter to main method either we are taking input or not in our program.

System is predefined class in java and out is object.
'System.out ' is reference of PrintStream class.
It connect the file(in above example "Hello, I am human") to the command line.
println() is method in System class which print output/string/text in command line.

Output of above program is as follow in command line:


JVM

Jvm-Java virtual machine.
Jvm is both S/W and H/W.
JVM is platform dependent but it makes Java platform independent.
Jvm execute java code.
Jvm has compiler and interpreter.
Compiler: javac (Which is developed in java language)
Source code ----javac---->Byte code.(.class file)



Byte code is in machine code(0,1).
(We see in encrypted form when we open a class file in notepad.)

Interpreter: java (Which is developed in C language.).
Byte code------java----->Native code.(Result)



JVM's works in JRE:
1. To load the class : Class loader.
2. To verify the byte code: Byte code verifier.
3. To run the program.

In 2005, JIT(just in time) compiler launched.
JIT read all bytecode and give the native code.
Jit compiler is called by jvm when necessary.