Showing posts with label new keyword. Show all posts
Showing posts with label new keyword. Show all posts

Saturday, 15 February 2014

Object Creation and Destruction

In java Objects are stored in heap.

How to create an object
   Syntax
      new ClassName(parameters);
'new' keyword is used to create an object in Java. parameters are optional.

   Example
      new Person()

Creationof object is not sufficient to access the object methods and instance properties. There must be a reference to the object, to access the instance methods and properties of an object.

How to assign an object to a reference
   Syntax
      ClassName reference = new ClassName(parameters);

   Example
      Person p1 = new Person()

By using the reference p1, we can access the instance behaviors and properties of an object.

One object can be referred by more than one reference like below
Person p2 = p1;

Now p1 and p2 point to the same object. Here the reference count for the object is 2, since p1 and p2 are referring to the same object.

If reference count for an object is zero, then the object is eligible for garbage collection. I.e, the memory used by the object is freed and used for other purposes.

Remember, even if the objects is created inside a method, or block also, it always stored in the heap, only references stored on stack.

Detailed view of Object creation and Destruction
1. Lets say there is a class called Person, and an object created like below
      Person p1 = new Person()

object of the class Person is created in the heap and reference p1 refers to the newly created object


2. Assigning one more reference to the previously created object.
       Person p2 = p1;
As per the above statement references p1 and p2 refers to the same object.

Reference count is 2


3. Create one more object
     Person p3 = new Person()


4. To remove reference for an object, there are two ways. One way set null to the reference, or make the reference to point to other object.
     p2= null;

Above statement makes the reference variable p2 to set to null.

5. p2 = p3;
Now p2 refers to the object pointed by the reference p3




6. p2 = null;
p3 = null;

Now the object which is previously pointed by reference variables p2 and p3 has reference count zero. So it is eligible for garbage Collection.


You may like


Increase heap size                                                 Garbage Collection                                                 Home

Friday, 7 February 2014

Arrays

An array is a series of elements of the same type placed in contiguous memory locations. Elements in the array are accessed by using index positions.

Suppose if you want to calculate sum of 9 integers, you have to define 9 variables like below

    int var1, var2, var3, var4, var5, var6, var7, var8, var9;

    var1 = 5;
    var2 = -10;
    var3 = 7;
    var4 = 8
    var5 = 11;
    var6 = 12;
    var7 = 14;
    var8 = 21;
    var9 = 0;

By using new keyword we can declare like below

    int var[] = new int[9];

    var[0] = 5;
    var[1] = -10;
    var[2] = 7;
    var[3] = 8
    var[4] = 11;
    var[5] = 12;
    var[6] = 14;
    var[7] = 21;
    var[8] = 0;

As you see, Array indexes starts from 0. var[0] represents 5, var[2] represents 7 and so on.

continue                                                 One Dimensional Array                                                 Home

Wednesday, 5 February 2014

Creating Object

Syntax to create an Object
     ClassName objectName = new ClassName();

     Example
          Person personA = new Person();

          Here Person is the class name
          personA is the object created for the class Person.

    Once the class is defiined, you can create any number of objects to it.

               Person personB = new Person();
               Person personC = new Person();

How to access properties of an object
     java provides '.' opertor to access th properties of an object.
     Syntax:
          objectName.propertyName = value;

     Example
          personA.height = 6;
          personB.height = 5.5;

How to call the behaviors
     Just like how you are accessing properties, you can call the behaviours.
     Syntax
     objectName.behavior();

     Example
     personA.getHeight();
     personB.getHeight();

Complete Working Example of Person class
class Person{
 float height, weight;
 String color, country;
 
 float getHeight(){
  return height;
 }
 
 float getWeight(){
  return weight;
 }
 
 String getColor(){
  return color;
 }
 
 String getCountry(){
  return country;
 }
 
 public static void main(String args[]){
 
  /* Declaring two person objects */
  Person personA = new Person();
  Person personB = new Person();
  
  /* Setting properties for personA object */
  personA.height = 6;
  personA.weight = 75;
  personA.color = "White";
  personA.country = "India";
  
  /* Setting properties for personB object */
  personB.height = 5.5f;
  personB.weight = 76;
  personB.color = "Black";
  personB.country = "America";
  
  /* Calling the behaviors and printing the details of personA */
  System.out.println("PersonA Height is " + personA.getHeight());
  System.out.println("PersonA Weight is " + personA.getWeight());
  System.out.println("PersonA color is " + personA.getColor());
  System.out.println("PersonA country is " + personA.getCountry());
  System.out.println();
  
  /* Calling the behaviors and printing the details of personB */
  System.out.println("PersonB Height is " + personB.getHeight());
  System.out.println("PersonB Weight is " + personB.getWeight());
  System.out.println("PersonB color is " + personB.getColor());
  System.out.println("PersonB country is " + personB.getCountry());
 }
}
   
Output
PersonA Height is 6.0
PersonA Weight is 75.0
PersonA color is White
PersonA country is India

PersonB Height is 5.5
PersonB Weight is 76.0
PersonB color is Black
PersonB country is America
  





Class Declaration                                                 instance Variables                                                 Home