EXISTENCE OF PRIMITIVE DATA TYPE
Primitive data type: Datatype or simply type defines the type of operation allowed on stored data. Primitive data type represent single item of data. They are built in nature i.e. when you define a datatype , range of data allowed, memory size,default value as well as operation allowed on that data is already get decided by the processor.
Datatype supported by JAVA: byte,short,int,long, float,double and char.
We all know that JAVA in not purely Object Oriented because it uses primitive datatype.
However JAVA also has WRAPPER CLASSES( a class representation of primitive datatype) that make JAVA as purely object oriented
So why JAVA support Primitive??
As per as I know, These of the following reason why java support primitive datatype:
Datatype supported by JAVA: byte,short,int,long, float,double and char.
We all know that JAVA in not purely Object Oriented because it uses primitive datatype.
However JAVA also has WRAPPER CLASSES( a class representation of primitive datatype) that make JAVA as purely object oriented
So why JAVA support Primitive??
As per as I know, These of the following reason why java support primitive datatype:
1. PERFORMANCE:
In Joshua Bloch's Effective Java, Item 5: "Avoid creating unnecessary objects", he posts the following code example:
and it takes 43 seconds to run. Taking the Long into the primitive brings it down to 6.8 seconds... if that's any indication why we use primitives.
|
2.MEMORY USAGE: Datatype has fixed memory size and the highest memory size is for double that 64 bit. However Instances of an object on the Java heap don't just take up memory for their actual fields. Inevitably, they also require some "housekeeping" information, such as recording an object's class, ID and status flags such as whether the object is currently reachable, currently synchronization-locked etc
- a normal object requires 8 bytes of "housekeeping" space;
TO SAVE A HUGE AMOUNT OF MEMORY CHUNK WE NEED PRIMITIVE DATA TYPE.
3. NULL POINTER EXCEPTION(NPE):
Simple although one of the possible reason of choosing primitive, as If primitive datatype give ease of use to programmer as they need not to check that data is null or not they automatically get initialized with their default value .
Integer in = null;
...
...
int i = in; // NPE at runtime
3. EASE OF USE:
class TEST {
public static void main(String[] args) {
System.out.println(new Integer(5) == new Integer(5));
System.out.println(new Integer(500) == new Integer(500));
System.out.println(Integer.valueOf(5) == Integer.valueOf(5));
System.out.println(Integer.valueOf(500) == Integer.valueOf(500));
}
}
RESULT is:
false
false
true
false
WHY true for 3 and false for 4??
Because they are two different objects. The 256 integers closest to zero [-128; 127] are cached by the JVM, so they return the same object for those. Beyond that range, though, they aren't cached, so a new object is
4. BACKWARD COMPATIBILITY:
Backward compatibility was the one of the issue why java does not removed primitive datatype after launching WRAPPER CLASSES
WHY REQUIREMENT OF WRAPPER CLASS:
1. SO that null value will be possible for data type.
2. To include primitive data type in COLLECTION.
3. To get type safety we use generics and generics need objects not primitives.
4. For polymorphism:
doSomething(Object object) can take any generic parameter since the attribute is of type Object i.e Superclass of all object. This cannot be achieved with primitive data type.
If you have any doubt or information on this. Please comment!
Thank you Oshin. This is very helpful.
ReplyDelete