Autoboxing dot java - is expected (or possible), the...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
/** Autoboxing Integer b; b= 25; // Illegal in java version 4: the types // of variable and expression don't match int i; i= b; // Illegal in java version 4: the types // of variable and expression don't match ===================== The assignments work in Java version 5 and later because of the introduction of "autoboxing". Autoboxing (and autounboxing) is just a convenience, allowing code to be shortened. Autoboxing works with all primitive types and their wrapper classes. Rule for autoboxing an int: If an expression e (say) of type int appears in a position in which an object of class Integer
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: is expected (or possible), the expression new Integer(e) is used in place of e. Rule for autounboxing an int: If an expression e (say) of type Integer appears in a position in which an expression of type int is expected, then the expression e.intValue() is used in place of e. */ public class AutoboxDemo { /** = "obj is 5". */ public static boolean isFive(Integer obj) { return obj.equals(new Integer(5)); } /** = "c is 4". */ public static boolean isFour(int c) { return c == 5; } }...
View Full Document

Ask a homework question - tutors are online