Declaration of wrapper classes [duplicate]
Asked Answered
D

5

3

This question is in continuation to a question How can a string be initialized using " "?

I would like to raise your attentation that even Integer, Double, Character, Float, Boolean wrapper class can also be declared in the same way String is declared like:

String s = "Test string"

Integer i = 10; //valid
Double d = 10.00; //valid
Boolean b = true; //valid

Does these class are also given special treatment like the String class.

Disentail answered 22/10, 2013 at 6:35 Comment(1)
@moroun [link]ntu.edu.sg/home/ehchua/programming/java/J3d_String.htmlDisentail
M
4

As I pointed out In my previous answer(How can a string be initialized using " "?)

Yes, to retain primitive types in an OOP, designers made bridge between primitives and Object's with Wrappers and they have a special treatment.

The reason is clearly explained in docs.

There are, however, reasons to use objects in place of primitives, and the Java platform provides wrapper classes for each of the primitive data types. These classes "wrap" the primitive in an object. Often, the wrapping is done by the compiler—if you use a primitive where an object is expected, the compiler boxes the primitive in its wrapper class for you. Similarly, if you use a number object when a primitive is expected, the compiler unboxes the object for you. For more information, see Autoboxing and Unboxing

We use primitives extensively in our programs, So it might be a design decision to allowing syntax like

   Integer i = 10; //primitive style

Then memory allocates at compile time itself for i since it is a primitive type, when they found with Wrapper type declarations with an Assignment operator =

Syntax wise ,that is more handy and happy(at least for me :)).

Than writing,

   Integer i = new Integer(10); //Object creation style
Mandal answered 22/10, 2013 at 6:42 Comment(0)
U
3

All these following statements:

Integer i = 10; //valid
Double d = 10.00; //valid
Boolean b = true; //valid

are valid because of autoboxing

Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes

Unreflective answered 22/10, 2013 at 6:37 Comment(2)
Hi @Juned thanks for your quick reply, one more question, HOW the object of Integer, Double, Boolean and other wrapper classes are created by just declaring data type by '=' sign.Disentail
@user2704032 Compiler does that job for you. Autoboxing/Unboxing was added to help programmers to write things in a easier way while working with primitives and corresponding wrapper classes.Unreflective
S
1

Yes primitive Wrapper classes also behave like String class.

You can illustrate like below

Integer i1 = new Integer(10); //valid
Integer i2 =10;
System.out.println(i1==i2); // this one is false
i1=10;  
System.out.println(i1==i2); //// this one is true
Schizothymia answered 22/10, 2013 at 6:48 Comment(0)
H
0

All wrapper classes of primitive types behave this way. It is called autoboxing and was introduced in java 1.5:

http://docs.oracle.com/javase/tutorial/java/data/autoboxing.html

Hypothecate answered 22/10, 2013 at 6:39 Comment(0)
R
0

String str = "Test String"; having a special treatment is irrespective to and the Integer i = 10; what is the special treatment String s = "Test String";? these are called String literals gets the memory in String constant pool of jvm. the one special significance is in terms of Garbage Collection is, the pooled constants are never be influenced by garbage collection. Making 'str' as null does not make "Test String" is eligible for garbage collection. WHY?:: JVM will try to reuse this "Test String" in future. The garbage collection algorithm excludes the objects which are in pooled memory. so normal GC rules won't apply here. check this out: why String literals are not garbage collected

Now how this treatment is quite different to wrapper's auto boxing. automatic boxing is introduced from JDK1.5. auto boxing & auto unboxing When Integer i = 10; the compiler replaces this statement with Integer i = Integer.valueOf(10); only the internal cache wrapper objects of JVM are acts like String literals remaining are not. now what are internal cache wrapper objects?

Integer i = 100;
Integer j = 100;

references i & j are given with a single pre existed object's address. that is why if( i==j) //true

Integer k = 130; 
if( i==k) // false

because k's value is beyond the cache range which is -128 to 127 for Integer. check this:Integer wrapper behaviour when value range is -128 to 127 In the above if we nullify the reference k then its object undergoes for the GC which is not same treatment like String literals. If we nullify the i or j then corresponding cached object never be influenced by GC which is same treatment like String literals.

Rochester answered 22/10, 2013 at 7:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.