WRAPPER CLASS IN JAVA

Maha

WRAPPER CLASS IN JAVA

In Java, a class whose object either wraps or contains primitive data types is called a Wrapper class. A field can be used to store primitive data types when an object to a wrapper class is created. To put it another way, we can wrap a primitive value in an object of the wrapper class.

Why use the wrapper class in Java?

The wrapper class in Java is used to convert the primitive data types into the objects. Objects are needed if they modify the arguments passed into a method.

What are the 8 wrapper classes in Java?

There are 8 Wrapper classes in Java these are 

char, 

byte, 

short, 

int, 

long, 

float, 

double and 

boolean.


Example:

public class IntegerExample

{

public static void main(String[] args)

{

Integer wrappedInt = new Integer(42); 

int value = wrappedInt.intValue();

System.out.println("Wrapped Integer value: " + value);

int incrementedValue = wrappedInt + 10;

System.out.println("Incremented value: " + incrementedValue);

String stringValue = wrappedInt.toString();

System.out.println("Wrapped Integer as String: " + stringValue);

String numberString = "100";

Integer parsedInt = Integer.parseInt(numberString);

System.out.println("Parsed Integer value: " + parsedInt);

Integer autoboxedInt = 20;

int unboxedInt = autoboxedInt;

System.out.println("Autoboxed and unboxed values: " + autoboxedInt + ", " + unboxedInt);

}

}

Output:

Wrapped Integer value: 42

Incremented value: 52

Wrapped Integer as String: 42

Parsed Integer value: 100

Autoboxed and unboxed values: 20, 20

Our website uses cookies to enhance your experience. Learn More
Accept !

GocourseAI

close
send