问题
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.
回答1:
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
回答2:
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
回答3:
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
回答4:
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
回答5:
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.
来源:https://stackoverflow.com/questions/19510684/declaration-of-wrapper-classes