Java Intermediate

0% completed

Previous
Next
Wrapper Classes in java

In Java, wrapper classes provide a way to use primitive data types (int, char, double, etc.) as objects. Each primitive type has a corresponding wrapper class in the java.lang package:

Image
Primitive TypeWrapper Class
booleanBoolean
byteByte
charCharacter
shortShort
intInteger
longLong
floatFloat
doubleDouble

Key Benefits of Wrapper Classes

  • Object Representation: Enables primitives to be used in collections that require objects (e.g., ArrayList<Integer>).
  • Utility Methods: Provide methods for converting and manipulating primitive values.
  • Constants: Offer useful constants like MAX_VALUE and MIN_VALUE.
  • Autoboxing and Unboxing: Automatic conversion between primitives and their corresponding wrapper classes.

Wrapper Classes and Their Key Methods

1. Boolean Class

MethodDescription
parseBoolean(String s)Parses the string argument as a boolean.
valueOf(boolean b)Returns a Boolean instance representing the specified boolean value.
booleanValue()Returns the value of this Boolean object as a primitive boolean.
compare(boolean a, boolean b)Compares two boolean values.
toString(boolean b)Returns a String object representing the specified boolean.

2. Byte Class

MethodDescription
parseByte(String s)Parses the string argument as a signed decimal byte.
valueOf(byte b)Returns a Byte instance representing the specified byte value.
byteValue()Returns the value of this Byte as a primitive byte.
compare(byte a, byte b)Compares two byte values.
toString(byte b)Returns a String object representing the specified byte.

3. Character Class

MethodDescription
isLetter(char ch)Determines if the specified character is a letter.
isDigit(char ch)Determines if the specified character is a digit.
toUpperCase(char ch)Converts the character to uppercase.
toLowerCase(char ch)Converts the character to lowercase.
valueOf(char c)Returns a Character instance representing the specified char value.

4. Short Class

MethodDescription
parseShort(String s)Parses the string argument as a signed decimal short.
valueOf(short s)Returns a Short instance representing the specified short value.
shortValue()Returns the value of this Short as a primitive short.
compare(short a, short b)Compares two short values.
toString(short s)Returns a String object representing the specified short.

5. Integer Class

MethodDescription
parseInt(String s)Parses the string argument as a signed decimal integer.
valueOf(int i)Returns an Integer instance representing the specified int value.
intValue()Returns the value of this Integer as a primitive int.
compare(int a, int b)Compares two int values.
toString(int i)Returns a String object representing the specified int.
MAX_VALUEMaximum value an int can have (2147483647).
MIN_VALUEMinimum value an int can have (-2147483648).

6. Long Class

MethodDescription
parseLong(String s)Parses the string argument as a signed decimal long.
valueOf(long l)Returns a Long instance representing the specified long value.
longValue()Returns the value of this Long as a primitive long.
compare(long a, long b)Compares two long values.
toString(long l)Returns a String object representing the specified long.
MAX_VALUEMaximum value a long can have (9223372036854775807L).
MIN_VALUEMinimum value a long can have (-9223372036854775808L).

7. Float Class

MethodDescription
parseFloat(String s)Parses the string argument as a floating-point number.
valueOf(float f)Returns a Float instance representing the specified float value.
floatValue()Returns the value of this Float as a primitive float.
compare(float a, float b)Compares two float values.
toString(float f)Returns a String object representing the specified float.
MAX_VALUEMaximum value a float can have (3.4028235e+38f).
MIN_VALUEMinimum value a float can have (1.4e-45f).

8. Double Class

MethodDescription
parseDouble(String s)Parses the string argument as a double.
valueOf(double d)Returns a Double instance representing the specified double value.
doubleValue()Returns the value of this Double as a primitive double.
compare(double a, double b)Compares two double values.
toString(double d)Returns a String object representing the specified double.
MAX_VALUEMaximum value a double can have (1.7976931348623157e+308).
MIN_VALUEMinimum value a double can have (4.9e-324).

Example: Using Wrapper Classes in a Single File

Below is a comprehensive example demonstrating the usage of various wrapper classes within a single Java file using the Solution class.

Java
Java

. . . .

Here, we have used different wrapper classes in the above example.

Wrapper classes in Java bridge the gap between primitive types and object-oriented programming by providing object representations of primitive values. This integration allows primitives to be used seamlessly within Java's collection framework and leverages the rich set of utility methods provided by these classes for data manipulation and conversion.

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next