We've moved! — MindVault360 is now SrcForge. Better design, more content & premium notes.

Visit SrcForge →

MindVault360 has moved!

We've upgraded to SrcForge — a faster, more professional platform with better content, premium notes, and a modern design.

Visit us at SrcForge

Tuesday, December 17, 2024

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 →

Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home