Wrapper Class


(Tap the post to see more)

It provides a way to use primitive data type as a reference data type.

Sample Program of wrapper class:

public class WrapperClassExample {

public static void main(String[] args) {

// Primitive data types

int intValue = 42;

double doubleValue = 3.14;

char charValue = 'A';


// Using wrapper classes

Integer integerObject = Integer.valueOf(intValue); // Wrapping int

Double doubleObject = Double.valueOf(doubleValue); // Wrapping double

Character charObject = Character.valueOf(charValue); // Wrapping char


System.out.println("Primitive Values:");

System.out.println("int: " + intValue);

System.out.println("double: " + doubleValue);

System.out.println("char: " + charValue);


System.out.println("\nWrapper Class Values:");

System.out.println("Integer: " + integerObject);

System.out.println("Double: " + doubleObject);

System.out.println("Character: " + charObject);


// Unwrapping - getting primitive values back

int unwrappedInt = integerObject.intValue();

double unwrappedDouble = doubleObject.doubleValue();

char unwrappedChar = charObject.charValue();


System.out.println("\nUnwrapped Values:");

System.out.println("Unwrapped int: " + unwrappedInt);

System.out.println("Unwrapped double: " + unwrappedDouble);

System.out.println("Unwrapped char: " + unwrappedChar);

}

}

← Back Next →

Comments

Popular posts from this blog

Information Security & Essential Terminology

Information Security Threat Categories