MainMenu

Home Java Overview Maven Tutorials

Thursday, 17 October 2019

Wrapper class in Java

Wrapper in java



Wrapper Class In java






Wrapper Class In Java
In Java:
Primitive data types → int, float, char, boolean, etc.
Wrapper classes → Objects that wrap these primitive values.
Wrapper classes convert primitive data into objects.


before this , there is a question
Question :- Java is 100% Object Oriented Programing Language or not?
Answer :- Java is 99.9 % object Oriented Programming Language
It is not 100% Object Oriented Programing language , due to its primitive data Types :-
int, float , char, double etc..
These are derived from C and these are not objects
and 100% object oriented means that every thing should be in form of object.
and primitive data types are not object
for example :-
int data;
here data is not an object, it's a variable.

So in java , we have classes for every data types, we call the as wrapper classes,

Table for primitive data type and wrapper classes

>
Primitive TypeWrapper Class
booleanBOOLEAN
charCharacter
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble
for example for int, we have class Integer
primitive int variable
int x = 20;
not if i want to convert this varibale into an object, then i have to write
Integer i = new Interger(x); OR Integer i = new Interger(20);
now i is 20;
This way of converting primitive into object is called as AutoBoxing

Now, how to take out value from Integer Object
Interger i = new Integer(20);
direct value can't be assigned to object so we need to use a method datatypeValue
int j = i.intValue();
now j = 10;
So getting the priitive value from object is called as Unboxing
public class Wrappractice
{
public static void main(String args[])
{
int i = 20;
Interger mydata = new Integer(i); //Autoboxing
System.out.println("value of i is " + i);
int j;
j = mydata.intValue();
System.out.println( "value od j is " +j); //Unboxing
}
}
--------------------------------------------------------

Wrapper class are Immutable in Java


All primitive wrapper classes (Integer, Byte, Long, Float, Double, Character, Boolean and Short) are immutable in Java, so operations like addition and subtraction create a new object and not modify the old.


Example to prove that wrapper classes are Immutable

public class Wrap
{
public static void main(String[] args)
{
Integer x = new Integer(20);
System.out.println("before modification value of x is :" +x);
add(x);
System.out.println("After modification value of x is :" +x);
}
public static void add(int x)
{
x = x+1;
System.out.println(x);
}
}


Output :-


before modification value of x is :20
21
After modification value of x is :20

Note : String isn't a primitive type.

Why Do We Need Wrapper Classes?
1️⃣ Collections can store only objects
Example: ArrayList cannot store int, but can store Integer.
ArrayList list = new ArrayList<>();
list.add(10); // works

2️⃣ Convert String to number
int num = Integer.parseInt("100");

3️⃣ Convert number to String
String s = String.valueOf(200);

4️⃣ Useful methods
Wrapper classes have helpful methods like:
Integer.MAX_VALUE;
Integer.MIN_VALUE;
Character.isDigit('9');
๐Ÿš€ Autoboxing & Unboxing (Super Easy)
๐ŸŸก Autoboxing

Java automatically converts primitive → wrapper object
int a = 10;
Integer obj = a; // autoboxing

๐ŸŸข Unboxing
Java converts wrapper object → primitive
Integer obj = 20;
int b = obj; // unboxing

๐ŸŽ‰ Simple Example Showing Everything
public class WrapperExample {
public static void main(String[] args) {
// Primitive
int num = 50;
// Autoboxing (int → Integer)
Integer obj = num;
System.out.println("Integer object: " + obj);
// Unboxing (Integer → int)
int value = obj;
System.out.println("Primitive value: " + value);
// Convert String → int
int x = Integer.parseInt("123");
System.out.println("String to int: " + x);
// Convert int → String
String s = String.valueOf(999);
System.out.println("Int to String: " + s);
}
}
๐Ÿงช Real-life Use Case (Common in Selenium Automation)
❌ Not allowed:
ArrayList list = new ArrayList<>(); // ERROR
✅ Use Wrapper Class:
ArrayList ids = new ArrayList<>();
ids.add(101);
ids.add(102);

Wrapper classes make primitives usable with Java Collections.
⭐ Summary (In Simple Words)
Primitive = simple fast data
Wrapper = object version of primitive
Used in Collections, conversions, and APIs
Autoboxing/unboxing = automatic conversion between primitive & wrapper

No comments:

Post a Comment