* These classes can be used for converting a value from primitive type to class type.
* For every primitive type available in java we have one corresponding wrapper classes.
* All the wrapper classes (8) are available in java.lang package.
Creation of Wrapper Classes:
Every wrapper class contains two constructors taking one parameter each of primitive datatype & String type.
Byte b = new Byte(byte);
Byte b = new Byte(String);
Short s = new short(short);
Short s = new short(String);
Integer i = new Integer(int);
Integer i = new Integer(String);
Long l = new Long(long);
Long l = new Long(String);
Float f = new Float(float);
Float f = new Float(double);
Float f = new Float(String);
Double d = new Double (double);
Double d = new Double(String);
Character c = new Character(char);
Boolean b = new Boolean(boolean);
Boolean b = new Boolean(String);
Ex:How to create our own wrapper classes.
int i = 12;
Integer j = new Integer(i);
System.out.println(i);
System.out.println(j);
Boolean class Examples:
in String type (or) boolean literals – It is allowed(true) otherwise display it as false.
Boolean b1 = new Boolean(true); //true Boolean b2 = new Boolean(false); //true
//Boolean b3 = new Boolean(true); //false(because of T) Boolean b4 = new Boolean(“true”); //true
Boolean b5 = new Boolean(“TRUE”); //true Boolean b6 = new Boolean(“TRUE”); //true Boolean b7 = new Boolean(“false”); //false Boolean b8 = new Boolean(“hello”); //false Boolean b9 = new Boolean(“”); //false Boolean b10 = new Boolean(null); //false
* If we provide a boolean literal “true” then the boolean object will contain true, if we provide boolean literal false then the boolean object will contain false.
* If we provide true as a string in any case(upper (or) lower (or) mixed case) (“true” “TRUE”
“TRUE” “TRUE”) then the boolean object will contain true and any other content that is specified as string will store false into the boolean object.
Methods of Wrapper Classes:
1. Value Of():
This method can be used for converting a primitive type value (or) a string value into wrapper type.
Syntax1: Static WrapperClass ValueOf(primitivetype)
The above syntax can be used to convert a primitive type to wrapper. The syntax is available in all wrapper classes except character.
Ex: int i = 23;
Integer j = new Integer(i);
Integer k = new Integer("34");
2. xxxxValue:
This method can be used for converting wrapper type value to primitive type.
Syntax: primitivetype xxxValue() Ex: byte byteValue()
byteValue()
shortValue() These 6 methods are available in each class of all
intValue() ‘6’ classes below Byte, Short, Integer, Long, Float, Double.
longValue() floatValue() doubleValue()
CharValue() This method is available in Character class.
booleanValue() This method is available in Boolean class.
Ex: Integer i = Integer.ValueOF("23");
int j = i.intValue();
double d = i.doubleValue();
long l = i.longValue();
3. parse xxxx():
This method can be used for converting String type to primitive type.
Syntax: Static primitivetype parsexxxx(String);
The parsexxx() is available in all wrapper classes except “character ”.
Ex: String S = “11”;
int i = Integer.parseInt(S);
float f = Float.parsefloat(S);
byte b = Byte.parseByte(S);
4.
Static String toBinaryString(int/long) Static String toOctalString(int/long) Static String toHexaString(int/long)
* The above three methods can be used to convert a value from decimal number system to binary system, octal system & Hexadecimal system respectively.
* The above three methods are available in Integer & long wrapper classes.
Ex: int i = 12;
System.out.println(i);
System.out.println(Integer.toBinaryString(i));
System.out.println(Integer.toOctalString(i));
System.out.println(Integer.toHexaString(i));
Hierarchy of Wrapper Classes:
Boxing:
It is the process of converting a value from primitive type to wrapper type. This process is performed automatically by the compiler from 1.5 version & therefore called it as
AutoBoxing.
UnBoxing:
It is a process of converting a value from wrapper type to primitive type. This process is performed automatically by the compiler from 1.5 version onwards & there called it as
Auto Unboxing.
Integer m = new Integer(45);
int n = m;
enum:
The enum keyword is introduced in java 1.5 version & the purpose of an enum keyword is to create a group of named constants.
Syntax:
enum<EnumName>{
Constants }
Convention for writing enum name:
The enum name can contain any number of words & the first letter of every word should be specified in UpperCase.
Ex: enum Day{
SUN, MON, TUE, WED, THU, FRI, SAT;
}
* When a java program is compiled, the compiler will generate a (.class) file for every class, every interface & for every enum.
* When we compile the above program Day.java, the compiler will generate Day.class.
javac Day.java
javap Day
Final class Day extends Enum{
public static final Day SUN;
public static final Day MON;
. . . .
public static Day[] Values();
}
* Every enum will be internally a final class which extends from Enum class.
* Enum class is a predefined class which is abstract & available in java.lang package & it inherits from object class.
Rules:
* An enum in java is more powerful compare to enum in other languages, because an enum in java can contain variables, methods & constructors along with the constants.
* If an enum contains only constants then specifying the semicolon is optional.
* If an enum contains other code along with constants then specifying the semicolon is mandatory.
* If an enum contains other code along with constants then the constants must be specified as the first statement in an enum.
* An enum can be empty (or) it can contain only constants (or) it can contain other code along with costants but it cannot contain other code without the constants.
* An enum can contain main() & they can be executed.
Ex: enum with main():
enum Day{
SUN, MON, TUE, WED, THU, FRI, SAT;
Psum(String[] args){
System.out.println(“enum main method”);
} }
javac Day.java
javap Day
O/P: enum main method
* Every enum will contain a constructor.
* An enum cannot be instantiated by the programmer i.e., the programmer cannot create the object directly but an object of enum can be created by declaring a constant in an enum.
* The constructors of an enum will be executed one time for every constant that is declared.
enum Day{
SUN, MON, TUE, WED, THU, FRI, SAT;
Day(){
System.out.println(“enum main method”);
}
Psum(String[] args){
System.out.println(“enum main method”);
} }
O/P: enum constructor enum constructor enum constructor enum constructor enum constructor enum constructor enum constructor enum main method
Ex: enum Fruit{
Apple(30), MANGO(90), GRAPES, ORANGE(70), BANANA(40);
int Price;
Fruit(){
price = 50;
}
Fruit(int Price){
this.price = price;
}
int getPrice(){
return Price;
} }
class Eat{
Psum(String[] args){
Fruit f = Fruit.MANGO;
System.out.println(f.Price);
Fruit[] fr = Fruit.Values();
for(Fruit x:fr){
System.out.println(x+":"+x.getPrice());
} } }
To the switch statement we can pass byte, short, int, char as an argument & it is valid upto 1.4 version from 1.5 version onwards, we can pass byte, short, int, char primitive types & its corresponding four wrapper classes(Byte, Short, Integer & Character) & enum.
enum Day{
SUN, MON, TUE, WED, THU, FRI, SAT;
}
class Work{
Psum(String[] args){
Day d = Day.SUN;
Switch(d){
Case MON:System.out.println(“Boring, back to office”);
break;
Case FRI:System.out.println(“preparing to enjoy”);
break;
Case SAT:System.out.println(“sleeping”);
break;
Case SUN:System.out.println(“eating”);
} }
}
Note: The case labels must be exactly same as that of the constants available in enum.