Difference between revisions of "Autoboxing"

From Suhrid.net Wiki
Jump to navigationJump to search
Line 35: Line 35:
 
** Character from \u0000 to \u007f
 
** Character from \u0000 to \u007f
 
** Short and Integer from -128 to 127
 
** Short and Integer from -128 to 127
 +
 +
== Overloading ==
 +
 +
  
 
[[Category:OCPJP]]
 
[[Category:OCPJP]]

Revision as of 00:54, 15 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 through static utility methods in Integer and Long - e.g. toBinaryString(), toHexString() and toOctalString().

Autoboxing Intro

  • New feature in Java 5 which avoids having to manually wrap and unwrap a primitive.
  • A wrapper can be used like a primitive.
  • But since wrappers are immutable, any "change" in the wrappers value leads to a new wrapper object being created. See below: incrementing y means y will refer to a new object.
Integer y = new Integer(42);
Integer x = y;
System.out.println(x==y); //Prints true
y++;
System.out.println(x==y); //Prints false

Equality

  • Wrapper's equals() method will check if the values are the same.
  • Like the string pool, JVM tries to save memory when wrappers are used, so a pool is maintained for certain values of wrappers.
  • The '==' operator will return true when for wrapper references when their values are the same and they:
    • All Boolean Wrappers
    • All Byte Wrappers
    • Character from \u0000 to \u007f
    • Short and Integer from -128 to 127

Overloading