Exercise 3-9 (Autoboxing)

Chapter_3     Exercise_3-8 Exercise_3-10







Exercise 3-9     TIJ, p. 61


Exercise 3-9: (2) Write a program that demonstrates that autoboxing works for all the primitive types and their wrappers.




Autoboxing.java


public class Autoboxing
{
public static void main(String[] args)
{
Boolean bool = false; // autoboxing
boolean b = bool; // unboxing
System.out.println("bool = " + bool + ", b = " + b);
Character ch = 'x';
char c = ch;
System.out.println("ch = " + ch + ", c = " + c);
Byte bte = 0;
byte bt = bte;
System.out.println("bte = " + bte + ", bt = " + bt);
Short sh = 0;
short s = sh;
System.out.println("sh = " + sh + ", s = " + s);
Integer ii = 0;
int i = ii;
System.out.println("ii = " + ii + ", i = " + i);
Long ll = 0l;
long l = ll;
System.out.println("ll = " + ll + ", l = " + l);
Float fl = 0.0f;
float f = fl;
System.out.println("fl = " + fl + ", f = " + f);
Double db = 0.0d;
double d = db;
System.out.println("db = " + db + ", d = " + d);
Void vd = null;
// void v = vd; // illegal (void vars not allowed)
System.out.println("vd = " + vd);
}
}
/*
javac Autoboxing.java
java Autoboxing
bool = false, b = false
ch = x, c = x
bte = 0, bt = 0
sh = 0, s = 0
ii = 0, i = 0
ll = 0, l = 0
fl = 0.0, f = 0.0
db = 0.0, d = 0.0
vd = null
*/









Chapter_3     Exercise_3-8 BACK_TO_TOP Exercise_3-10



Comments

Popular posts from this blog

Contents