Problem | Questions |
---|---|
Problem 1 | What are wrapper classes? |
Problem 2 | Why do we need Wrapper Classes in Java? |
Problem 3 | What are the different ways of creating Wrapper Class Instances? |
Problem 4 | What is Auto Boxing? |
Problem 5 | What are the advantages of Auto Boxing? |
Problem 6 | What is Casting? |
Problem 7 | What is Implicit Casting? |
Problem 8 | What is Explicit Casting? |
A primitive wrapper class in the Java programming language is one of eight
classes provided in thejava.lang package to provide object methods for the
eight primitive types. All of the primitive wrapper classes in Java are
immutable.
**Wrapper:** Boolean,Byte,Character,Double,Float,Integer,Long,Short
**Primitive:** boolean,byte,char ,double, float, int , long,short
A wrapper class wraps (encloses) around a data type and gives it an object
appearance.
Reasons why we need Wrapper Classes
• null is a possible value
• use it in a Collection
• Methods that support Object like creation from other types.. like String◦
**Integer number2 = new Integer("55");//String**
What are the different ways of creating Wrapper Class Instances?
Two ways of creating Wrapper Class Instances are described below.
**Using a Wrapper Class Constructor**
Integer number = new Integer(55);//int
Integer number2 = new Integer("55");//String
Float number3 = new Float(55.0);//double argument
Float number4 = new Float(55.0f);//float argument
Float number5 = new Float("55.0f");//String
Character c1 = new Character('C');//Only char constructor
//Character c2 = new Character(124);//COMPILER ERROR
Boolean b = new Boolean(true);
//"true" "True" "tRUe" - all String Values give True
//Anything else gives false
Boolean b1 = new Boolean("true");//value stored - true
Boolean b2 = new Boolean("True");//value stored - true
Boolean b3 = new Boolean("False");//value stored - false
Boolean b4 = new Boolean("SomeString");//value stored - false
**valueOf Static Methods**
Provide another way of creating a Wrapper Object
Integer hundred = Integer.valueOf("100");//100 is stored in variable
Integer seven = Integer.valueOf("111", 2);//binary 111 is converted to 7
What is Auto Boxing?
Autoboxing is the automatic conversion that the Java compiler makes between
the primitive types and their corresponding object wrapper classes.
For example, converting an int to an Integer, a double to a Double, and so on.
If the conversion goes the other way, this is called unboxing.
**Example 1**
Integer nineC = 9;
**Example 2**
Integer ten = new Integer(10);
ten++;//allowed. Java does hard work behind the screen for us
What are the advantages of Auto Boxing?
Auto Boxing helps in saving memory by reusing already created Wrapper objects.
Auto Boxing uses the static valueOf methods. However wrapper classes created
using new are not reused.
**Two wrapper objects created using new are not same object.**
Integer nineA = new Integer(9);
Integer nineB = new Integer(9);
System.out.println(nineA == nineB);//false
System.out.println(nineA.equals(nineB));//true
**Two wrapper objects created using boxing are same object.**
Integer nineC = 9;
Integer nineD = 9;
System.out.println(nineC == nineD);//true
System.out.println(nineC.equals(nineD));//true