Friday, June 13, 2008

Hello Primitives


Java is not 100% object oriented because of two main constraints:

  1. int, float, char etc are primitives and not objects
  2. Multiple inheritance not supported.

Java has an answer/solution to the above mentioned constraints. Wrapper classes are provided which allow us to create an object even when dealing with simple numbers. Thus instead of using int, we can create an object of type Integer or a Float object instead of float primitive. Java's answer to multiple inheritance is Interfaces; a class can implement more than one interfaces.

Even with these solutions/workarounds, the main reason behind Java not being purely object oriented is that we can still write a Java program with the use of primitives. This introduces use of an entity that is not an object, which violates the principle of object oriented program. Java allows the use of primitives for performance reasons. If it weren't for primitives, our mundane loops would have caused a catastrophe. A simple program to illustrate the Power of Primitives:


import java.util.Calendar;

public class HelloPrimitives {

public static void main(String args[]) {

//Nested loops using primitives
long lTimeInMillis = Calendar.getInstance().getTimeInMillis();
int i = 0;
int j = 0;

for( int counter = 0; counter < 1000; counter++ ) {

for( int innerCounter = 0; innerCounter < 3000; innerCounter++ ) {

j = innerCounter;
}

i = counter;
}

lTimeInMillis = Calendar.getInstance().getTimeInMillis() - lTimeInMillis;

//Nested loops using wrapper classes
Long oTimeInMillis = new Long( Calendar.getInstance().getTimeInMillis( ) );
Integer objI = null;
Integer objJ = null;
Integer objCounter = new Integer( 0 );

for( ; objCounter.intValue() < 1000; ) {

Integer integerInnerCounter = new Integer( 0 );

for( ; objInnerCounter.intValue() < 3000; ) {

objJ = new Integer( objInnerCounter.intValue() );
objInnerCounter = new Integer( objInnerCounter.intValue() + 1 );
}

objI = new Integer( objCounter.intValue() );
objCounter = new Integer( objCounter.intValue() + 1 );
}

oTimeInMillis = new Long( Calendar.getInstance().getTimeInMillis() - oTimeInMillis.longValue() );

System.out.println( "Time taken by primitives = " + lTimeInMillis );
System.out.println( "Time taken by wrapper classes = " + oTimeInMillis.longValue() );

}
}

The following output is observed on my machine (numbers are machine specific):
D:\amey\blogs\java\progs\hello_primitives>java HelloPrimitives
Time taken by primitives = 15
Time taken by wrapper classes = 1219


It can be seen that use of primitives improves the performance significantly. Stark reasons for this are

  1. Memory allocation for a primitive and object is very different. For primitive datatype, the value is directly stored at the memory address of the reference. Whereas objects are created on heap, and the memory location of the reference holds the memory location of the object on the heap.
  2. The values can be directly accessed in case of primitives while for objects, we need to invoke a method using the dot ',' operator. This calls for pushing the current method on the stack and popping it back at later instance which is time consuming.

One more tip for increasing performance is controlling number of times a primitive or object gets created. In the above program, we are creating innerCounter and objInnerCounter within the inner loop. Try initializing them just once above the outermost for loop and see the difference in timings.

Also increase the inner loop to 30,000.

int innerCounter = 0; //initialize only once
for( int counter = 0; counter < 1000; counter++ ) {
for( ; innerCounter < 30000; ) {
.....
.....
}


.....
.....
Integer objCounter = new Integer( 0 );
Integer objInnerCounter = new Integer( 0 ); //initialize only once
for( ; objCounter.intValue() < 1000; ) {



for( ; objInnerCounter.intValue() < 30000; ) {
.....
.....
}
}
The output on my machine is:
D:\amey\blogs\java\progs\hello_primitives>java HelloPrimitives
Time taken by primitives = 0
Time taken by wrapper classes = 16

Wow !

Cheers,
Amey

3 comments:

Unknown said...

Guruji,
I admire you for your teaching skills, before that for strong fundamentals. Look forward to great learning.. Thanks

balarami reddy said...

Hi...

Awesome and Inspiring articles on java.. i am a hard core fan of programming where i enjoy programming in free time.

Looking forward for more interesting articles...

farhan said...

Java programming
This is very helpful to learn java programming I am very thanks to you that you provide me great knowledge about java.
I am also try to write java program, some java coding is available on my blog please visit my blog and give me your important advise. This is very helpful to me.
http://javaprogramming-farhan.blogspot.com