0% found this document useful (0 votes)
10 views9 pages

Java Wrapper Classes Examples

The document contains several Java programs that demonstrate the use of wrapper classes, including finding minimum and maximum integer values, calculating the sum of double values, and illustrating autoboxing and unboxing for boolean values. It also shows how to convert an int to a String using the Integer wrapper class and how to use wrapper classes in a collection like ArrayList. Overall, these examples cover essential operations and functionalities related to Java wrapper classes.

Uploaded by

kumar811ak47
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views9 pages

Java Wrapper Classes Examples

The document contains several Java programs that demonstrate the use of wrapper classes, including finding minimum and maximum integer values, calculating the sum of double values, and illustrating autoboxing and unboxing for boolean values. It also shows how to convert an int to a String using the Integer wrapper class and how to use wrapper classes in a collection like ArrayList. Overall, these examples cover essential operations and functionalities related to Java wrapper classes.

Uploaded by

kumar811ak47
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

deV

Programs on Wrapper Classes

Question 1:

Write a Java program to find the minimum


and maximum value of an integer using
Integer wrapper class methods.

Solution 1:

public class MinMaxIntegerExample


{

public static void main(String[] args) {


// Finding minimum and maximum
values using Integer wrapper class

int minValue = Integer.MIN_VALUE;


int maxValue = Integer.MAX_VALUE;

// Displaying the results


[Link]("Minimum Value
of Integer: " + minValue);
[Link]("Maximum Value
of Integer: " + maxValue);
}
}

Question 2:

Write a Java program that uses wrapper


classes to calculate the sum of two double
values entered by the user.

Solution 2:

import [Link];

public class DoubleSumExample


{

public static void main(String[] args) {


// Using wrapper classes to calculate
the sum of two double values
Scanner scanner = new
Scanner([Link]);

[Link]("Enter the first


double value: ");
Double num1 =
[Link]();

[Link]("Enter the second


double value: ");
Double num2 =
[Link]();

Double sum = num1 + num2;

// Displaying the result


[Link]("Sum of the two
double values: " + sum);
}
}

Question 3:
Write a Java program to demonstrate
autoboxing and unboxing for a boolean
value.

Solution 3:

public class BooleanAutoboxingExample


{

public static void main(String[] args) {


// Autoboxing: converting boolean to
Boolean
Boolean boolObj = true;
// Unboxing: converting Boolean to
boolean
boolean boolValue = boolObj;

// Displaying the results


[Link]("Autoboxing: " +
boolObj);
[Link]("Unboxing: " +
boolValue);
}
}

Question 4:

Write a Java program to convert an int


value to a String using the Integer
wrapper class.

Solution 4:
public class
IntToStringConversionExample
{

public static void main(String[] args) {


// Converting int to String using
Integer wrapper class
int intValue = 42;
String stringValue =
[Link](intValue);

// Displaying the result


[Link]("Converted String
value: " + stringValue);
}
}

Question 5:
Write a Java program to demonstrate the
usage of wrapper classes in a collection
(e.g., ArrayList).

Solution 5:

import [Link];

public class
WrapperClassInCollectionExample
{

public static void main(String[] args) {


// Using wrapper classes in an
ArrayList
ArrayList<Integer> numbersList =
new ArrayList<>();

// Adding values using autoboxing


[Link](10);
[Link](20);
[Link](30);
// Displaying the values
[Link]("Numbers in the
ArrayList: " + numbersList);

// Accessing values using unboxing


int firstNumber = [Link](0);

// Displaying the result


[Link]("First number in
the ArrayList: " + firstNumber);
}
}

These simple Java programs cover


various aspects of wrapper classes,
including finding minimum and maximum
values, performing calculations,
demonstrating autoboxing and unboxing,
and using wrapper classes in a collection.
THE END

Common questions

Powered by AI

Wrapper classes in Source 1 support OOP principles by enabling primitive data types to be utilized as objects, strengthening data encapsulation and abstraction. They allow for enriched interaction with methods and compatibility with Java's object-oriented features, thus enhancing modularity and reuse of code components .

The program in Source 1 leverages the Integer wrapper class's built-in constants, MIN_VALUE and MAX_VALUE, to determine the minimum and maximum values an integer can hold. It assigns these constants to variables minValue and maxValue, respectively, then prints them out .

The program demonstrates autoboxing by automatically converting a primitive boolean value into a Boolean object when it assigns 'true' to the Boolean variable boolObj. Conversely, unboxing occurs when the Boolean object is converted back to a primitive boolean and assigned to boolValue. This feature simplifies code by reducing manual conversion code, enhancing readability and reducing errors .

To enhance performance, a developer can minimize the use of wrapper classes where possible, opting for primitive data types to reduce overhead from autoboxing and unboxing. This approach includes especially favoring primitives within computationally heavy loops or high-frequency method calls where instantaneous performance is crucial .

The conversion is achieved using the Integer class's static method toString, which converts the given int value of 42 to a String. Potential uses include preparing integer data for display purposes or storing them in databases that accept string data types .

The constants MIN_VALUE and MAX_VALUE define the lower and upper bounds of what an Integer type can hold, crucial in preventing overflows and underflows during computations. In software development, they are often used to ensure that values remain within permissible limits, thus maintaining system stability and predictability .

The program demonstrates wrapper classes in collections by using an ArrayList of Integer objects, where values are added using autoboxing. The benefits include the seamless integration of primitive types in collections and the ability to leverage ArrayList's methods with Integer objects while still maintaining the ease of primitive operations via autoboxing and unboxing .

Using wrapper classes in collections like ArrayLists is practical as it allows the integration of primitive data in collections, utilizing their in-built methods while supporting null values. Unlike primitives, wrapper classes provide enhanced functionality such as flexibility of use across Java's Collection framework, albeit with a slight overhead due to additional wrapping and unwrapping operations .

The program uses Double wrapper classes to handle inputs, allowing operations that benefit from object methods. This matches primitive types in terms of syntax simplicity due to autoboxing, but offers extra methods inaccessible with pure primitives, like method chaining and null handling. It streamlines input plus computation processes within object-oriented paradigms .

The program prompts the user to input two double values through the Scanner object, where these inputs are wrapped into Double objects due to the wrapper class usage. The Double class allows direct arithmetic operations to calculate the sum (num1 + num2), which is then printed out to demonstrate the result .

You might also like