Sep 19, 2013

Job of new operator in java

What does new operator do?

new operator in java is used for creating the object of the class. new operator instantiates the class and allocates memory to the object. As an object is instantiated all non-static member of the class are activated and ready to be used. It is the well known working of new operator, I want to tell you what happens in the memory when an object is created in the memory i.e. job of new operator or responsibilities of new operator in java programming.
We will discuss step by step jobs of new operator. First i want to tell you how we create an object?

class_name <reference variable> = new class_name();

Here, <reference variable> is the variable of specified class and it stores the hashcode(address of object where it has been created) returned by the new operator. hashcode is in the form of hexadecimal code.
Now we will discuss job of new operator.
  1. If .class file is not loaded  then to load the .class file from hard disk to RAM and store into method memory area(mma).
  2. To allocate the memory for all non-static context and allocate the memory for all static member.
  3. Execute all static blocks.
  4. Create the context or segment for an object of class into heap memory area and generate a unique id that is known as hashcode. Non technically  we call it reference id or unique id.
  5. Allocate memory for all non-static member of a class inside an object.
  6. If any non-static variable of a class which is not initialized, is initialized by its default value.
  7. Execute all non-static blocks into top to bottom order.
  8. Execute constructor.
  9. Write the hashcode into specified variable.
Note: If .class file is already loaded then step no 4 to 9 will be followed only.

Use of new operator in java programming is very broad. new operator is responsible for many tasks which we have discussed above. It completes the discussion of What actually new operator does in java. For more queries please visit internal mechanism of object.

Sep 18, 2013

Internal Mechanism of public static void main(String args[])

Internal Mechanism of public static void main(String args[]) in java programming:

Java: public static void main(String args[]): Let us explaining public static void main(String args[]). Java is a platform independent language in which main() method is declared as public as well as static and its return type is void. In main method a String type array parameter is passed. Let us take a look why do we write main() method as public static void main(String args[]). We will explain each term step by step as below-

Why do we use public keyword:

public is an access specifier in java programming i.e. it defines the scope and visibility of class members. main() is also a class member when we declare main() method as public it can be called by JVM. main() is a method which is called by JVM automatically. There are two environment in java programming one is JDK environment and another is JVM environment. If JVM calls a method written in JDK environment then that method need to be declared as public. As we know that when ever we want to call a method out side of the class or package then that method must be declared as public. This is the concept behind main() method declared as public.

Why do we use static keyword:

static is an access access modifier in java programming i.e. it defines characteristics of class members. static member of the class are automatically loaded into memory without creating object of the class and non-static member needs to load into memory with object. So if we don't declare main() method as static then it would be a non-static method and would need to be called by object reference. Unfortunately JVM don't create any object automatically in this situation any instance member will not be instantiated through out its life.

Why do we use void return type:

main() method is automatically called by JVM. Its return type is void i.e. it returns no value. If it have any return type then it would be received by JVM and there is no procedure for using a value received by JVM. So we declare main() method with void return type.

Why do we use String args[]:

In main() method an array instance of String class is passed. Purpose of passing this type parameter is to receive the COMMAND LINE ARGUMENT when program is executed. In String args[], args is an array type variable of class String. Value inputted by command line argument is treated as String type however it is an integer value.

For example-

If user has inputted two integer numbers for addition that will be treated as String in this situation we need to convert these no. into integer type by using parsing technique.

It completes the great explanation of public static void main(String args[]).

Sep 13, 2013

How to create an Object in Java

How to create an Object in Java: Creating Object is very important step to execute our java program successfully. As we know we can call all non-static members only after creating object of the class so must need to create an object. We can execute non-static block without creating object below java5 version. In this post we will know how to create an object in java. Object is an instance variable of the class and it can be created by using "new" keyword.

"new" keyword is responsible for allocating memory to the object. Code to create an object is as follows-


new Java_program();
new Java_example();
new Test();

In above example we are creating an object by using new keyword. "new" kyword is allocating memory to the object anf returning reference of the object. This reference is needed to access the non-static members of the class so we have to store in a variable called a reference variable.


Java_program ob=new Java_program();

Here, ob is the reference variable which stores the referenece of the object. We can access non-static members using "ob"  as follows-

class Java_program
   int a=10,b=5;
   void mul()
       System.out.println("Multiplication is="+a*b);
  public static void main(String argxyz[])
      Java_program ob=new Java_program();

Internals Of Logical AND Logical OR in Java

Logical AND(1st condition && 2nd condition)-

Internals of Logical AND and Logical OR:Lets describe What is the internal mechanism of Logical AND and Logical OR.Logical AND is a Logical operator in java which returns a boolean value. If first condition false then in Logical AND, pointer does not check second condition. For second condition we use Bitwise AND.

 Logical OR(First condition || 2nd condition)-

Logical OR is also a Logical Opeartor which returns a boolean value. In Logical OR If first condition is true then pointer does not check 2nd condition . For check 2nd condition we use Bitwise OR.
For example-
Logical OR java         
int x= 10 && 20; 

Logical AND javaint y= 10 & 20;

Both operator Bitwise AND and Logical AND use with condition  but Logical AND does not use without condition. Bitwise AND use with and Without condition.

Sep 12, 2013

Internals Of Java Compiler

Internal mechanism of Compiler in java:Let us describe internals of java compiler.A Java Compiler is the collection of programs which is used to compile the java programs. Java Compiler checks java source code if syntactically correct then generates .class file (bytecode) and save into current working directory else java compiler generates compile time error.
for example-
 Here javac is used to invoke java compiler and to load the source code from Hard Disk to Ram.


Sep 8, 2013

this reference in Java

this reference-

this keyword in java programming is used to refer the current class object. this keyword is used when we want to call methods without creating object. this reference stores in the memory when an object of the current class is instantiated. We can say "this" is a special keyword in java programming which is used to refer current class object or instance of any class. By using this keyword we can call methods of current class as well as constructor. Let us see use of this reference in java and its interesting example.
  • this is the keyword of java.
  • this reference holds the address of current class's object.
  • this reference can not be used inside any static context.
  • this reference can be used inside any non-static context.
  • this reference refers to the current class object.
  • this reference stores in the memory how many times object of the class is instantiated.
this  reference is used to call a non-static method of the current class without using the object. for example- if there is a method names "getdata", we can call this method as follows-

here, this keyword is holding the reference of the current class object so it can also call the any method of the same class. however we can also call the methods without this reference such as-
this reference is very useful in some cases like-

        class This1 
int k=200;
void m1()
int k=100;
public static void main(String[] args) 
This1 d1=new This1();
This1 d2=new This1();
This1 d3=new This1();



this reference in java

It completes the discussion of this reference.we will discuss about this-call later. For more study about this reference please follow these links-