Showing posts with label Core Java2. Show all posts

Java Virtual Machine (JVM) Architecture.

0
Java Virtual Machine (JVM) Architecture:
Definition of JVM:
Virtual Machine:
In general terms VM is a SW that creates an environment between the computer platform and end user in which end user can operate programs.
Original meaning for VM:
As per its functionality is creation of number of  different identical execution environments on a single computer to executer programs is called VM.
Java Virtual Machine:
It is also a VM that runs Java bytecode by creating five identical runtime areas to execute class members. This bytecode is generated by java compiler in a JVM understandable format.

How can we start JVM process?
By using “java” tool.
The java launcher, java, initiates the Java virtual machine instance.
Types of JVMs:
The java 2 SDK, Standard Edition, contains two implementations of the Java virtual machines.
  1. Java HotSpot Client VM.
  2. Java HotSpot Server VM.

1.Java HotSpot Client VM:
The Java HotSpot Client VM is the default virtual machine of the Java 2 SDK and Java 2 Runtime Environment. As its name implies, it is tuned for best performance when running applications in a client environment by reducing application start-up time and memory footprint.

2. Java HotSpot Server VM:
The Java HotSpot Server VM is designed for maximum program execution speed for applications running in a server environment. The Java HotSpot Server VM is invoked by using the –server command-line option when launching an application, as in 
Java –server MyApp
Some of the features Java HotSpot technology, common to both VM implementations, are the following.

Adaptive compiler:
  • Applications are launched using a standard interpreter, but the code is then analyzed as it runs to detect performance bottlenecks, or “hot spots”.
  • The Java HotSpot VMs compile those performance-critical portions of the code for a boost in performance, while avoiding unnecessary compilation of seldom-used code(most of the program).
  • The Java HotSpot VMs also uses the adaptive compiler to decide, on the fly, how best to optimize compiled code with techniques such as in-lining.
  • The runtime analysis performed by the compiler allows it to eliminate guesswork in determining which optimizations will yield the largest performance benefit.
Rapid memory allocation and garbage collection:
  • Java HotSpot technology provides for rapid memory allocation for objects, and it has a fast, efficient, state-of-the-art garbage collector.
Thread synchronization:
  • The Java programming language allows for use of multiple, concurrent paths of program execution (called “threada”).
  • Java HotSpot technology provides a thread-handling capability that is designed to scale readily for use in large, shared-memory multiprocessor servers.
JVM Architecture block diagram:

Explanation on Runtime Areas:
Whenever we execute a class by specifying its corresponding class name by  using the command “java <ClassName>”, the Java launcher, Java, immediately initiates the Java Runtime environment for the class execution as a layer on top of OS, and further the entire setup is divided in to % java Runtime Areas named as
1. Method Area.
  2. Heap Area.
  3. Java Stack Area.
  4. Program counter registers Area.
  5. Native Methods Stacks area.

1. Method Area:
  • All classes bytecode is loaded and stored in this runtime area, and all static variables are created in this runtime area.
2. Heap Area:
  • It is the main memory of JVM. All objects of classes – non-static variables memory are created in this runtime area. This runtime area memory is a finite memory.
  • This area can be configured at the time of setting up of runtime environment using non standard option like
         Java –xms <size> classname
  • The area can be expandable by its own, depending on the objects creation.
  • Method area and Heap area both are sharable memory areas.
3. Java Stack Area:
  • In this runtime area all Java methods are executed.
  • In this runtime JVM by default creates two threads, they are
      • Main thread.
      • Garbage collector thread.
  • Main thread is responsible to execute Java methods stats with main method, also responsible to create objects in heap area if it finds “new” keyword in any method logic.
  • Garbage collector thread is responsible to destroy all unused objects from heap area.
  Note: Like in C++, in Java, we do not have destructors to destroy objects.
  • For each method execution JVM creates separate block in main thread. Technically this block is called Stack Frame. This stack frame is created when method is called and is destroyed after method execution.
  Note: Java operations are called “stack based operations (sequential)”, because every method is executed only in stack.

4. Program Counter Registers Area:
  • In this runtime area, a separate program counter register is created for every thread for tracking that thread execution by storing its instruction address.
5. Native Methods Stacks Area:
  • In Native Methods stack area, all Java native methods are executed.


Continue reading →

Definition of Java Virtual Machine (JVM).

0
Definition of Java Virtual Machine (JVM):
Virtual Machine:
In general terms VM is a SW that creates an environment between the computer platform and end user in which end user can operate programs.

Original meaning for VM:
As per its functionality is creation of number of  different identical execution environments on a single computer to executer programs is called VM.

Java Virtual Machine:
It is also a VM that runs Java bytecode by creating five identical runtime areas to execute class members. This bytecode is generated by java compiler in a JVM understandable format.

Continue reading →

Types of Java Virtual Machine (JVM).

0
Types of Java Virtual Machine (JVM):
The java 2 SDK, Standard Edition, contains two implementations of the Java Virtual Machines.
1. Java HotSpot Client VM.
2. Java HotSpot Server VM.

1.Java HotSpot Client VM:
The Java HotSpot Client VM is the default virtual machine of the Java 2 SDK and Java 2 Runtime Environment. As its name implies, it is tuned for best performance when running applications in a client environment by reducing application start-up time and memory footprint.

2. Java HotSpot Server VM:
The Java HotSpot Server VM is designed for maximum program execution speed for applications running in a server environment. The Java HotSpot Server VM is invoked by using the –server command-line option when launching an application, as in Java –server MyApp.

Continue reading →

Write a program to display scope variable can be nexted.

0
/* Write a program to display scope variable can be nexted. */

class Scopevar{
    public static void main(String args[]){
        int x;
        x=10;
        if(x==10){
            int y;
            y=100;
            System.out.println("y="+y);
            }
        System.out.println("x="+x);
        }
    }

/*
Output:

E:>javac Scopevar.java
E:>java Scopevar

y=100
x=10

*/
Continue reading →

Write a program to demonstrate scope of a variable with in a block.

0
/* Write a program to demonstrate scope of a variable with in a block. */

class Scope{
    public static void main(String args[]){
        int x;
        for(x=0;x<3;x++){
            int y=-1;
            System.out.println("y="+y);
            y=100;
            System.out.println("y="+y);
            }
        }
    }

/*
Output:

E:>javac Scope.java
E:>java Scope

y=-1
y=100
y=-1
y=100
y=-1
y=100

*/
Continue reading →

Write a program to compute the area of the circle.

0
/*  Write a program to compute the area of the circle. */

class Area{
    public static void main(String args[]){
        double pi,r,a;
        pi=3.1416;
        r=10.8;
        a=pi*r*r;
        System.out.println("Area of circle:"+a);
        }
    }

/*
Output:

E:>javac Area.java
E:>java Area

Area of circle:366.436224


*/
Continue reading →

Write a program to compute the distance of light travels.

0
/* Write a program to compute the distance of light travels. */

class Light
{
    public static void main(String args[]){
        int lightspeed;
        long days;
        long seconds;
        long distance;
        lightspeed=186000;
        days=1000;
        seconds=days*24*60*60;
        distance=lightspeed*seconds;
        System.out.println("Lightspeed:"+lightspeed);
        System.out.println("Number of seconds:"+seconds);
        System.out.println("Total distance:"+distance);
        }
    }


/*
Output:

E:>javac Light.java
E:>java Light

Lightspeed:186000
Number of seconds:86400000
Total distance:16070400000000

*/
Continue reading →

Prove that this variable stores current object reference

1
Prove that this variable stores current object reference.

/* Prove that this variable stores current object reference. */

class A
{
int x,y;

A(){
x=5;
y=6;
}

A(int x,int y){
x=7;
y=8;
}

void m1(){
System.out.println("In m1");
System.out.println(x+"..."+y);

m2();
System.out.println("In m1 after m2 call");
System.out.println(x+"..."+y);
}

void m2(){
x=14;
y=15;

new B().m3(this);
}
}

class B
{
int p=5;
int q=6;

B(){
p=p+2;
q=q+3;
}

void m3(A a){
a.x=p+a.x;
a.y=q+a.y;

new C(this,a);
}
}

class C{
C(B b,A a){
b.p=a.x+2;
b.q=a.y+3;

a.x=b.p;
a.y=b.q=a.x+5;
}
}

class TVSCOReference  //This Variable Stores Current Object Reference.
{
public static void main(String[] args)
{
A a1=new A();
A a2=new A(1,2);

System.out.println("a1,a2 values before m1 call");
System.out.println(a1.x+"..."+a1.y);
System.out.println(a2.x+"..."+a2.y);

a1.m1();
System.out.println("a1,a2 values after m1 call");
System.out.println(a1.x+"..."+a1.y);
System.out.println(a2.x+"..."+a2.y);
}
}

Compilation:
G:\netlojava>javac TVSCOReference.java

Execution:
G:\netlojava>java TVSCOReference

Out Put:
a1,a2 values before m1 call
5...6
0...0
In m1 5...6 In m1 after m2 call
23...28
a1,a2 values after m1 call
23...28
0...0
Continue reading →

Can we share object as argument object to any method

0

/* Can we share object as argument object to any method.
Ans: yes, we must defined the method with parameter with that object class type. For example A class object we can share as a current to only A class methods. If can be shared as argument objects to either A class or B class */


class A
{
void m1()
{
System.out.println("class A and method m1");
}
void m2(A a)
{
System.out.println("class A and method m2");
}
}
class B
{
void m3()
{
System.out.println("class B and method m3");
}
void m4(A a)
{
System.out.println("class B and method m4");
}
}

class Test
{
public static void main(String[] args)
{
A a1=new A();
A a2=new A();

a1.m1();
a1.m2(a2);

// a1.m3(); //error: cannot find symbol a1.m3();
// a1.m4(a2);  //error: cannot find symbol a1.m4(a2);

B b1=new B();

b1.m3();
b1.m4(a2);
}
}

Compilation:
G:\netlojava>javac Test.java

Execution:
G:\netlojava>java Test

Out Put:
Continue reading →

Procedure in Creating Object.

0

Procedure in Creating Object:
In an object creation there are two phases
1) Object memory creation.
2) Object memory initialization.

The above two phases has four stemps.
example: Student s1=new Student
step-1: Referenced variable memory creation.
step-2: New keyword execution.
   2.1: As part of this step object memory is created with an non-static variables by assigning default values and then assigned value.
step-3: Invoking constrantes
new keyword invokes constracts by passing current object reference.
   3.1: As part of this step control is send to constrants body and reference is store by creating this keyword memory forther constraints logic is executed for initializing this object non-static variables.
step-4: After control return from constracter, new keyword return current object reference and store the reference is 's1' variable then 's1' variable start pointing to this object.

// Prog: NetloJavaInstitute.java

//Student.java

class Student {
 int rollNo;
 String name;
 String institute;

 Student(int rn,String nm,String ins){
  rollNo=rn;
  name=nm;
  institute=ins;
 }
}

//NetloJavaInstitute.java

 class NetloJavaInstitute{
  public static void main(String args[]){

   Student s1=new Student(1,"james","netlojava");
   Student s2=new Student(2,"gosling","netlojava");

   System.out.println("Student 1 Object Data");
   System.out.println("Roll No:"+s1.rollNo);
   System.out.println("Name:"+s1.name);
   System.out.println("Institute:"+s1.institute);

   System.out.println("\nStudent 2 Object Data");
   System.out.println("Roll NO:"+s2.rollNo);
   System.out.println("Name:"+s2.name);
   System.out.println("Institute:"+s2.institute);
  }
 }

compilation:
G:\netlojava>javac NetloJavaInstitute.java

Execution:
G:\netlojava>java NetloJavaInstitute

Out Put:

Continue reading →

Provide that 'this' variable stores current object reference.

0


/* Provide that 'this' variable stores current object reference.
print this variable in constructor and non-static method. In both placess same output is printed. i.e., classname@hashcode   */

// prog: ThisNSM.java

class A
{
A(){
System.out.println("In Constructor this:"+this);
}
void m1()
{
System.out.println("In NSM this:"+this);
}
}


class ThisNSM
{
public static void main(String[] args)
{
A a=new A();
System.out.println("In main a:"+a);
a.m1();
}
}

Compilation:
G:\netlojava> javac ThisNSM.java

Execution:
G:\netlojava> java ThisNSM

Out Put:

Continue reading →

6 uses of 'this' keyword

0

6 uses of 'this' keyword:

1. Stroring current object reference.
2. Accessing non-static variables and methods.
3. Accessing other constructor of same class without creating new object.
4. Differentiating non-static variables memory from one object to other object of some class in constructor, non-static block and non-static method.
5. Differentiating non-static variables from a parameter or from a local variable if both have same name.
6. Sending current object as argument and return type to other methods of same or another class.

Continue reading →

real world object

0

//Real World Object creation. Reduce the redundancy.

class  Employee
{
int empid;
String name;
String job;
Employee(int eid,String en,String ej)
{
empid=eid;
name=en;
job=ej;
}
void Display()
{
System.out.println("\n Employee Id "+empid+" Details");
System.out.println("Employee Id:"+empid);
System.out.println("Employee Name:"+name);
System.out.println("Employee JOb:"+job);
}
}


class CEmployee
{
public static void main(String[] args)
{
Employee e1=new Employee(1,"Smith","Java Developer");
Employee e2=new Employee(2,"John","Java Developer");
Employee e3=new Employee(3,"Scott","java developer");

e1.Display();
e2.Display();
e3.Display();
}
}


OutPut:


 Employee Id 1 Details
Employee Id:1
Employee Name:Smith
Employee JOb:Java Developer

 Employee Id 2 Details
Employee Id:2
Employee Name:John
Employee JOb:Java Developer

 Employee Id 3 Details
Employee Id:3
Employee Name:Scott
Employee JOb:java developer



Continue reading →

Purpose of Return statement.

0

Purpose of Return statement:
/*Basically return statement is used to terminate method execution and for sending control back to calling method.
-> "return;" sends control back to calling method without value.
-> "return <value>;" send's control back to calling method with value. */

//What is the Output from the below program.
//PORStatement.java

class PORStatement
{
static void method1(int a)
{
System.out.println("Before if");
if(a==12)
{
System.out.println("In if");
return;
}
System.out.println("After if");
System.out.println("Hi");
}
static int method2(int a)
{
System.out.println("Before if");
if(a==12)
{
System.out.println("In if");
return a+10;
}
System.out.println("After if");
System.out.println("Hi");
return 30;
}
public static void main(String[] args)
{
method1(12);
method1(24);

method2(12);
method2(24);
}
}

compilation:
G:\netlojava>javac PORStatement.java

Execution:
G:\netlojava>java PORStatement

Out Put:

Before if
In if
Before if
After if
Hi
Before if
In if
Before if
After if
Hi

Continue reading →

program to create real world object

0

program to create real world object

/* Write a program to create real world object called student in Java. Create to real world student object from the class with data.
rollno =1
name =james
institute =netlojava

rollno =2
name =gosling
institute =netlojava */

//Student.java

class Student {
int rollNo;
String name;
String institute;

Student(int rn,String nm,String ins){
rollNo=rn;
name=nm;
institute=ins;
}
}

//NetloJavaInstitute.java

class NetloJavaInstitute{
public static void main(String args[]){

Student s1=new Student(1,"james","netlojava");
Student s2=new Student(2,"gosling","netlojava");

System.out.println("Student 1 Object Data");
System.out.println("Roll No:"+s1.rollNo);
System.out.println("Name:"+s1.name);
System.out.println("Institute:"+s1.institute);

System.out.println("\nStudent 2 Object Data");
System.out.println("Roll NO:"+s2.rollNo);
System.out.println("Name:"+s2.name);
System.out.println("Institute:"+s2.institute);
}
}

compilation:
G:\netlojava>javac NetloJavaInstitute.java

Execution:
G:\netlojava>java NetloJavaInstitute

Out Put:
Student 1 Object Data
Roll No:1
Name:james
Institute:netlojava

Student 2 Object Data
Roll NO:2
Name:gosling
Institute:netlojava



Note: In student class constraintes replace parameter names with non-static variable names as shown below.
Student(int rollNo,String name,String institute) { rollNo=rollNum; name=name; institute=institute; } then compile and execute NetloJavaInstitute.java

OutPut:
Student 1 Object Data Roll No:0 Name:null Institute:null Student 2 Object Data Roll NO:0 Name:null Institute:null


//Real World Object creation.Reduce and redundancy.

class  Employee
{
int empid;
String name;
String job;
Employee(int eid,String en,String ej)
{
empid=eid;
name=en;
job=ej;
}
void Display()
{
System.out.println("\n Employee Id "+empid+" Details");
System.out.println("Employee Id:"+empid);
System.out.println("Employee Name:"+name);
System.out.println("Employee JOb:"+job);
}
}


class CEmployee
{
public static void main(String[] args)
{
Employee e1=new Employee(1,"Smith","Java Developer");
Employee e2=new Employee(2,"John","Java Developer");
Employee e3=new Employee(3,"Scott","java developer");

e1.Display();
e2.Display();
e3.Display();
}
}


OutPut:


 Employee Id 1 Details
Employee Id:1
Employee Name:Smith
Employee JOb:Java Developer

 Employee Id 2 Details
Employee Id:2
Employee Name:John
Employee JOb:Java Developer

 Employee Id 3 Details
Employee Id:3
Employee Name:Scott
Employee JOb:java developer


Continue reading →

How can a variable be created.

0

How can a variable be created.
Variable is a named memory location used to store data temporarily. During program execution we can modify that data. A Variable can be created by using Datatype. As we have two types of datatypes we can create two types of variables
1. Primitive Variables:- These variables are created by using primitive datatypes.
2. Referenced Variables:- These variables are created bt using referenced datatypes.

The Difference between primitive and referenced variables is "primitive variables stores data directly, where as referenced variables stores reference of the object, not direct values".
Note: As per compiler and JVM we do not have a  separate name called "referenced variable". It means, the variables created by using referenced datatype are also considered as like normal variables only.

Below program shows creating primitive and referenced variables
// Example.java

class Example
{
int x=10;
int y=10;
}

//Test.java

class Test{
public static void main(String args[]){
//primitive variables
int p=50;
int q=m1();
//referenced variables
String s1="a";
String s2=new String("a");

Example e=new Example();

//Referenced variables are initialized with object reference that is created and returned by "new" keyword, as shown in the abobe program.

}
static int m1(){
return 60;
}
        }

Limitation of variable:
It can only store single value at a time. If we assign new value, old value is replaced with new value. It means, always it returns latest modified value.
  • If we modify primitive value previous value is replaced with new value.
  • If we modify referenced variable previous object's reference is replaced with new object's reference and now this referenced variable is referencing to this new object.

Show memory structure from the below program and also output.

class Sample{
public static void main(String args[]){
int a=50;
System.out.println("a: "+a);
a=70;
System.out.println("a: "+a);
Example e1=new Example();
System.out.println("e1: "+e1);

e1=new Example();
System.out.println("e1: "+e1);
}
           }


Output:
a: 50
a: 70
e1: Example@f4a24a
e1: Example@cac268

Conclusion:
The value of primitive variable is mathematical data based on its data type.
The value of referenced variable is its class object's reference.

Continue reading →

Java Comments

0

/*
Java Comments:
A description about a basic programming element is called comment. Comments are meant for developer to understand the purpose.
Types of comments:
Java supports 3 types of comments
1. Single Line comment (//).
2. Multiline comment (/*    */).
3. Document comment (/**   */).

The statements placed inside these special characters are ignored by compiler while compling the class. It means, compiler will not generate bytecodes for those comments. So comments are not appeared in .class file.
Below program shows usage of all above 3 comments.
*/

//JavaComments.java

/**
* This program shows adding two integer numbers.
* @author: Netlojava
* @company: Netlojava.blogspot.com
* @date: 29-08-2013
* @Version: 1.0
*/

public class JavaComments
{
/*
  This method takes two integer numbers and return their addition result.
*/

           public static void main(String args[])
{
int a,b,c;   // a,b,c are integer varibules
a=5;   //a varibule 
b=6; //b varibule
c=a+b;
//addition of two numbers.
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
}


Compilation:
G:\> javac JavaComments.java

Execution:
G:\> java Java Comments

Out Put:

5
6
11


Continue reading →

Procedure to develop a JAVA program

0

Procedure to develop a JAVA program.
Step-1: Create a folder to store all your programs. This folder is called the Present Working Directory. Assuming the  folder path as (G:\netlojava\java_prog)

Step-2: Open notepad (start -> All Programs -> Accessories -> Notepad) and type below code


Step-3: Save this file in java_prog folder with name FirstProgram.java
Note: Java file name can be user defined name. It is not always mandatory to save java file name with same class name. If class is declared as "public" then only it is mandatory to save the file with class name. Let us try with different java file name.


Folder Structure

Program development is completed, now let us compile and execute.

Compilation and execution:
Step-4: Compilation
1. Open command prompt.
2. Change Drive and then directory to current working directory to Java_prog folder.
3. Then use javac tool to compile FirstProgram.java file, as show below figure.


Compiler has generated ".class" file successfully for the class JavaProgram in java_prog folder.
Note: Compiler generates ".class" file with the class name not java file name.
Check Java_prog folder for ".class" file name.


Step-5: Execution
Use java tool to execute JavaProgram class bytecodes file, as show below figure.


Compiler Activity:
It takes java file name as its input and generates bytecodes for all classes defined in that java file and stores each class bytecodes in a separate ".class" file with name same as class name.

JVM Activity:
It takes class name as its input and searches for a .class file with the given class name. If it found it reads and loads that .class file bytecodes into JVM, then starts that class logic execution by calling "main" method.


Continue reading →

Arithmetic Operators

0

Arithmetic Operators in java.

/*
-------------------- Arithmetic Operators --------------------
Java supports 5 Arithmetic operators
1. Addition (+)
2. Subtraction (-)
3. Multiplication (*)
4. Division (/)
5. Reminder (%)
-------------------- Arithmetic Operators precedence --------------------
*,/,%   operators have highest and same precedence
+,-       operators have next and same and precedence
note: In java all operators in an expression are executed from LEFT to RIGHT means from "=" operator to "," according to their precedence. */

// What is the output from the below program arithmetic statements.

class ArithmeticOperators{
     public static void main(String arg[]){

System.out.println(2+6-2);
System.out.println(2-2+6);
System.out.println(2*2+6);
System.out.println(2*6+2);
System.out.println(4/2*2);
System.out.println(2*6/5);
System.out.println(5*6/2);

System.out.println(5%2);
System.out.println(2%2);
System.out.println(2-2);
System.out.println(2+2);
System.out.println(8/2%2);

System.out.println(2-4+16/2*4%2);
System.out.println(4*2+8/2-3*3+4/2);
System.out.println(4*2+8/2-3*3+6/3);
System.out.println(8+8/2-3*3+6/3);
System.out.println(8+4-3*3+6/3);
System.out.println(12-9+2);
}
     }



Compilation:
G:\> javac ArithmeticOperators.java

Execution:
G:\> java ArithmeticOperators

Out Put:

6
6
10
14
4
2
15
1
0
0
4
0
-2
5
5
5
5
5

Continue reading →

What are the Java file's extensions.

0

What are the Java file's extensions.

In Java we have only two extension files. They are 
  •  ".java" extension file it called source code which is developed by developer.
  •  ".class" extension file it is called compiled code contains bytecode which is generated by compiler from source code.
Like in 'C' or 'C++', in java we do not have executable file means ".exe" extension files. More over in java, the compiled code is not machine language; instead it is an intermediate code, named as "bytecodes" stored in a new file with an extension ".class". This code is generated by java compiler and is only understandable by JVM.

Continue reading →

Recent Posts