156.
157.
CHAPTER
15
Enumerations, Autoboxing and
Wrapper Classes
What are the wrapper classes available in Java?
Answer:
Wrapper classes are classes
that correspond to the Java
primitive types. They help to convert primitive types to objects
and vice versa. This is particularly useful when you want to use
Collections. Since a Collection does
not accept primitive values,
you can use the corresponding wrapper type. Most of the
wrapper classes take their primitive
type or String as an
argument. Java supports the following wrapper classes:
Primitive Type
Wrapper Type
byte
Byte
short
Short
int
Integer
long
Long
float
Float
double
Double
char
Character
boolean
Boolean
What will be the output of the following code?
158.
public class BooleanDemo {
public static void main(String a[]){
//create Boolean using boolean primitive type
boolean boo1 = true;
Boolean booObj1 = new Boolean (boo1); //line 1
System.out.println(“Wrapper class Boolean output:
“+booObj1);
Boolean booObj2 = new Boolean (“false”); //line 2
System.out.println(“Wrapper class Boolean output:
“+booObj2);
System.out.println(booObj1.booleanValue());
}
}
Answer:
Line 1 creates a boolean wrapper type boolObj1 corresponding
to the bool variable. Line 2 creates a boolean wrapper
boolObj2 using a String value.
Java automatically converts
wrapper types to primitive types. In addition,
there is a method
called booleanValue() on the Boolean wrapper class that
returns the primitive boolean value. So,
this code will print the
following output:
Wrapper class Boolean output: true
Wrapper class Boolean output: false
true
Do'stlaringiz bilan baham: