Difference between revisions of "Autoboxing"

From Suhrid.net Wiki
Jump to navigationJump to search
Line 41: Line 41:
  
  
* Widening is preferred over Boxing. This is to allow legacy code to function the way it used to.
+
* '''Widening is preferred over Boxing'''. This is to allow legacy code to function the way it used to.
  
 
<syntaxhighlight lang="java5">
 
<syntaxhighlight lang="java5">
Line 62: Line 62:
 
}
 
}
  
 +
</syntaxhighlight>
 +
 +
* Widening is prefered over Var-Args. Again, this is to preserve legacy code behavior because var-args is a Java 5 feature.
 +
 +
<syntaxhighlight lang="java5">
  
 +
        static void foo() {
 +
            byte a = 10;
 +
            byte b = 20;
 +
            go(a, b);
 +
        }
 +
 +
 +
        static void go(byte... x) {
 +
System.out.println("Var Args");
 +
}
 +
 +
static void go(long x, long y) {
 +
System.out.println("Widen"); //Widen will print.
 +
}
  
 
</syntaxhighlight>
 
</syntaxhighlight>
 
  
  
 
[[Category:OCPJP]]
 
[[Category:OCPJP]]

Revision as of 01:12, 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

  • In every case when an exact match isnt found - the JVM uses the method with the smallest argument that is wider than the parameter.


  • Widening is preferred over Boxing. This is to allow legacy code to function the way it used to.
public class OverloadTest {

	public static void main(String[] args) {
		int i = 42;
		foo(i); 
	}
	
	static void foo(Integer x) {
		System.out.println("Box");
	}
	
	static void foo(float f) {
		System.out.println("Widen"); //Widen will print.
	}

}
  • Widening is prefered over Var-Args. Again, this is to preserve legacy code behavior because var-args is a Java 5 feature.
        static void foo() {
             byte a = 10;
             byte b = 20;
             go(a, b);
        }


        static void go(byte... x) {
		System.out.println("Var Args");
	}
	
	static void go(long x, long y) {
		System.out.println("Widen"); //Widen will print.
	}