autoboxing - Autoboxing Up through version 4 of Java, an...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Autoboxing Up through version 4 of Java, an assignment like Integer b; b= 25; int i;   i= b; were illegal, because the types of the variable and expression did not match —one is an int and the other is class Integer. In version 5 of Java, autoboxing was introduced, which makes such assignments legal. In the first assign- ment, b= 25; the integer 25 is automatically wrapped in a new instance of class Integer ; it is as if the pro- grammer had written b=  new Integer(25); In the second assignment, i= b; since an int value is needed, the int value is automatically extracted from In- teger b; it is as if the programmer had written i= b.intValue(); Autoboxing is a convenience for the programmer, allowing them to shorten their code. Autoboxing is done with all primitive types and their corresponding wrapper classes. For example, here is autoboxing of a character: Character c= 'g'; Moreover, the autoboxing can be carried out in places other than assignments. We give the rule for autoboxing for type
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 07/07/2008 for the course CS 101 taught by Professor Gries during the Spring '08 term at Cornell University (Engineering School).

Page1 / 2

autoboxing - Autoboxing Up through version 4 of Java, an...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online