Monday, 24 October 2016

Primitives and Wrapper Classes


Not everything in Java is an object. There is a special group of data types (also known as primitive types) that will be used quite often in your programming. For performance reasons, the designers of the Java language decided to include these primitive types.
The primitive data types are not objects; they do not belong to any class; they are defined in the language itself.

Why Java use primitives?
Creating an object using new isn’t very efficient because new will place objects on the heap. This approach would be very costly for small and simple variables. Instead of create variables using new, Java can use primitive types to create automatic variables that are not references. The variables hold the value, and it’s place on the stack so its much more efficient.
               Java determines the size of each primitive type. These sizes do not change from one machine architecture to another (as do in most other languages). This is one of the key features of the language that makes Java so portable.
Take note that all numeric types are signed. (No unsigned types).
Wrapper classes are great. But at same time they are slow. Primitive types are just values, whereas Wrapper classes are stores information about complete class.

See Below examples -
The first Println will print true whereas the second one will print false. The problem is when comparing two wrapper class objects we cant use == operator. It will compare the reference of object and not its actual value.
Also if you are using a wrapper class object then never forget to initialize it to a default value. As by default all wrapper class objects are initialized to null.
The above code will give a NullPointerException as it tries to box the values before comparing with true and as its null.

 Now, why Java use Wrapper Classes? 
            As the name says, a wrapper class wraps (encloses) around a data type and gives it an object appearance. Wherever, the data type is required as an object, this object can be used. Wrapper classes include methods to unwrap the object and give back the data type.
             It can be compared with a chocolate. The manufacturer wraps the chocolate with some foil or paper to prevent from pollution. The user takes the chocolate, removes and throws the wrapper and eats it.
Practical Usage -
1.To convert simple data types into objects
2.To convert numeric strings into numeric values

Advantage of primitives over wrapper classes -
1. Primitives uses less memory than wrapper classes.  
 Memory utilization
A double in Java always occupies 64 bits in memory, but the size of a reference depends on the Java virtual machine (JVM). My computer runs the 64-bit version of Windows 7 and a 64-bit JVM, and therefore a reference on my computer occupies 64 bits. Based on the diagram in Figure 1 I would expect a single double such as n1 to occupy 8 bytes (64 bits), and I would expect a single Double such as n2 to occupy 24 bytes — 8 for the reference to the object, 8 for the double value stored in the object, and 8 for the reference to the class object for Double. Plus, Java uses extra memory to support garbage collection for objects types but not for primitive types. Let's check it out.
   
2. Primitives are faster in performance than Objects.
Means,Operations with primitives takes less time than operations with wrapper classes.

Imp Link ->
http://www.javaworld.com/article/2150208/java-language/a-case-for-keeping-primitives-in-java.html


No comments:

Post a Comment