20 October 2010

1 Memory DeAllocation-Garbage Collection in Java

Memory DeAllocation or Garbage Collection in Java

A major difficulty in dynamic memory allocation in C/C++ was that the programmer is responsible for de-allocating the dynamic memory at the right time. Even though experienced programmers can do this very well, beginners and average programmers often missed the statements for de-allocated which led to memory-leak in many systems.
An object that is not referred by any reference variable will be removed from the memory by the garbage collector, this process is known as Garbage Collection
Garbage Collection is Automatically done by JVM, the Automatic Garbage Collection of Java de-allocates the dynamic memory automatically when this memory is no more used by the program. This relieves the programmer from the overhead of memory de-allocation.
If a reference variable is declared within a function, the reference is invalidated soon as the function call ends.
Programmer can explicitly set the reference variable to null to indicate that the referred object is no longer in use.
Primitive types are not objects and they cannot be assigned null.
Read More...

0 Memory Allocation in Java

Memory Allocation in Java


        All local variables are stored in a stack. local variables are de-allocated in a last in first out order as soon as the method terminates.
All dynamically allocated arrays and objects are stored in heap. They need not be de-allocated in any specific order. They can be garbage collected (removed from the memory) as and when their use is over.

Garbage Collection in Java
       An object that is not referred by any reference variable will be removed from the memory by the garbage collector. Garbage Collection is automatic.
If a reference variable is declared within a function, the reference is invalidated soon as the function call ends.
Programmer can explicitly set the reference variable to null to indicate that the referred object is no longer in use.
Read More...

0 What is Method Overloading in Java?

Method Overloading in Java

Two or more methods in a Java class can have the same name, if their argument lists are different, this feature is known as Method Overloading.
Argument list could differ in
  • No of parameters
  • Data type of parameters
  • Sequence of parameters
void print(int i){
    System.out.println(i);
}
void print(double d){
    System.out.println(d);
}
void print(char c){
    System.out.println(c);
}

Method Overloading in Java is Static Polymorphism
Calls to overloaded methods will be resolved during compile time, In otherwords, when the overloaded methods are invoked, JVM will choose the appropriate method based on the arguments used for invocation. For example, if the print method is invoked with an int argument, the overloaded print method that accepts an int parameter will be chosen by the JVM.

Example of Different implementation of Method Overloading in Java
void add (int a, int b)
void add (int a, float b)
void add (float a, int b)
void add (int a, int b, float c)

Methods differing only in return type
Methods differing only in return type will not be treated as overloaded methods, it will be compilation error. For Example, the below given methods will give compilation error.
void print(int i){
    System.out.println(i);
}
int print(int i)){
    System.out.println(i);
    return i;
}

Overloading the Constructors
Just like other methods, constructors also can be overloaded.

public class Student{
    public Student(){
        mark = 100;
    }
    public Student(int rollNo, double mark){
        this.rollNo = rollNo;
        this.mark = mark;
    }
}
Read More...

0 How to Invoke methods in a Java class?

Invoking methods in a class


The following statement creates a new "Student" object and assigns its reference to "student".
Student student = new Student();

All the public members of the object can be accessed with the help of the reference.
Student student = new Student();
student.setRollNo(20);
System.out.println(student.getRollNo());
 The reference can be treated as the name of an object.
For using a Student object, a programmer need not know the internal implementation details of the class. One needs to know only the public methods to use a Student object. Abstraction is achieved by hiding the irrelevant implementation details and exposing the relevant interface details.
Read More...

0 How to Create Objects in Java?

Creating Objects in Java

In Java, all objects are created dynamically. The operator "new" is used for dynamic memory allocation.

The following statement creates an object of the class Student
new Student()
The above statement returns a reference to the newly created object. Creation of objects and arrays are very similar in Java.
The following statement creates a reference to the class "Student".
Student student;
The reference "student" can be used for referring to any object of type "Student".
//Declare a reference to class "Student"
Student student;
//Create a new Student object
//Make student refer to the new object
Student student = new Student();
Read More...