Varargs and Wrapper Classes
Varargs and Wrapper Classes
Character(char ch)
Here, ch specifies the character that will be wrapped by the Character object being
created.
char charValue( )
Wrapper classes
Boolean
Boolean is a wrapper around boolean values.
Boolean(boolean boolValue)
Boolean(String boolString)
In the first version, boolValue must be either true or false.
In the second version, if boolString contains the string “true” (in uppercase or lowercase),
then the new Boolean object will be true. Otherwise, it will be false.
boolean booleanValue( )
Wrapper classes
The Numeric Type Wrappers
The most commonly used type wrappers are those that represent numeric
values.
These are Byte, Short, Integer, Long, Float, and Double.
All of the numeric type wrappers inherit the abstract class Number.
Number declares methods that return the value of an object in each of the different number
formats. These methods are :
byte byteValue( )
short shortValue( )
int intValue( )
int longValue()
float floatValue()
double doubleValue( )
int i = iOb.intValue();
int i = iOb.intValue();
Wrapper classes
autoboxing and auto-unboxing
Below way to construct an Integer object that has the value 100:
Note : no object is explicitly created through the use of new. Java handles this automatically.
Wrapper classes
• Next, this int value is assigned to iOb in main( ), which causes the int return
value to be autoboxed.
Wrapper classes
Autoboxing/Unboxing Occurs in
Expressions
++iOb;
class AutoBox4
{ public static void main(String args[])
{ Integer iOb = 100;
Double dOb = 98.6;
dOb = dOb + iOb;
System.out.println("dOb after expression: " + dOb);
}
}
Wrapper classes
Because of auto-unboxing, we can use integer numeric objects to
control a switch statement.
For example, consider this fragment:
Integer iOb = 2;
switch(iOb)
{
case 1: System.out.println("one");
break;
case 2: System.out.println("two");
break;
default: System.out.println("error");
}
Wrapper classes
Autoboxing/Unboxing Boolean and Character Values
class AutoBox5
{ public static void main(String args[])
{
// Autobox/unbox a boolean.
Boolean b = true;
// b is auto-unboxed.
if(b) System.out.println("b is true");
// Autobox/unbox a char.
Character ch = 'x'; // box a char
char ch2 = ch; // unbox a char
System.out.println("ch2 is " + ch2);
}}
Wrapper classes
Boolean b;
// ...
while(b) { // ... }
Method calling Conversion action