Difference between revisions of "Autoboxing"

From Suhrid.net Wiki
Jump to navigationJump to search
Line 4: Line 4:
 
* Wrapper objects are '''immutable''' !
 
* Wrapper objects are '''immutable''' !
 
* All have two constructors - one takes a primitive, other a string representation.
 
* All have two constructors - one takes a primitive, other a string representation.
* A valueOf() method also takes a string and returns a wrapper object in return.
+
* A valueOf() method also takes a string and returns a wrapper object in return. Also accepts an optional base (for Octal, Hex etc)
 
* Wrapper to primitive - use the xxxValue() methods like intValue() and floatValue()
 
* Wrapper to primitive - use the xxxValue() methods like intValue() and floatValue()
 
* String to primitive - e.g. Integer.parseInt("22"), Double.parseDouble("3.14");
 
* String to primitive - e.g. Integer.parseInt("22"), Double.parseDouble("3.14");
* toString() returns the string representation of a primitive.
+
* toString() returns the string representation of the value represented by the wrapper.
 +
* also base conversion is possible for Integer's and Long's - e.g. toBinaryString(), toHexString() and toOctalString().
 +
 
  
 
[[Category:OCPJP]]
 
[[Category:OCPJP]]

Revision as of 02:14, 13 August 2011

Wrapper Classes

  • Wrapper classes for primtives are a mechanism to include primitives in activities reserved for objects. e.g. being part of Collections.
  • Wrapper objects are immutable !
  • All have two constructors - one takes a primitive, other a string representation.
  • A valueOf() method also takes a string and returns a wrapper object in return. Also accepts an optional base (for Octal, Hex etc)
  • Wrapper to primitive - use the xxxValue() methods like intValue() and floatValue()
  • String to primitive - e.g. Integer.parseInt("22"), Double.parseDouble("3.14");
  • toString() returns the string representation of the value represented by the wrapper.
  • also base conversion is possible for Integer's and Long's - e.g. toBinaryString(), toHexString() and toOctalString().