Commonly Used Wrapper Methods in Java
Introduction
Wrapper classes in Java come with many useful methods that help convert, compare, and manipulate data easily.
These methods are used frequently while working with Strings, numbers, and Collections. Let’s explore the most commonly used ones in simple language.
1. parseXxx(String s)
This method converts a String into its primitive data type.
It is mainly used when we take input as a string (for example, from users) and need to convert it into a number.
Syntax:
int num = Integer.parseInt("123");
double d = Double.parseDouble("45.6");
Example:
String s1 = "123";
int n = Integer.parseInt(s1);
System.out.println("After conversion: " + n); // here n is primitive datatype
Output:
After conversion: 123
Explanation:
- String s1 = "123";
- Here, a string variable s1 is created with the value "123".
- It looks like a number, but it’s actually a string, so we can’t perform mathematical operations on it yet.
- int n = Integer.parseInt(s1);
- The parseInt() method belongs to the Integer wrapper class.
- It converts a string (like "123") into a primitive int value (123).
- So now, n becomes a primitive int, not an object.
- System.out.println("After conversion: " + n);
- This line prints the integer value of n.
- The output will be:
- After conversion: 123
2. valueOf(String s)
This method converts a String into a wrapper class object instead of a primitive.
It’s useful when you need the data in object form.
Example:
String s2 = "456";
Integer obj = Integer.valueOf(s2);
System.out.println("Wrapper object: " + obj);
Output:
Wrapper object: 456
Explanation:
- String s2 = "456";
- Here, a string variable s2 is created with the value "456".
- Although it looks like a number, it is stored as text (string data type).
- Integer obj = Integer.valueOf(s2);
- The valueOf() method belongs to the Integer wrapper class.
- It converts the string "456" into an Integer object — not just a primitive value.
- So, obj is now an object of class Integer, which wraps the primitive value 456.
- System.out.println("Wrapper object: " + obj);
- This prints the value stored inside the Integer object.
- Output will be:
- Wrapper object: 456
3. toString()
Converts a wrapper object (or primitive) into its String representation.
Example:
Integer num = 100;
String str = num.toString();
System.out.println("Converted String: " + str);
Output:
Converted String: 100
Explanation:
- Integer num = 100;
- Here, a wrapper object of type Integer is created.
- The primitive value 100 is automatically converted into an Integer object — this process is called autoboxing.
- String str = num.toString();
- The toString() method is a built-in method of all wrapper classes.
- It converts the wrapper object’s value into a string form.
- After this line, str will store "100" (as text), not a number.
- System.out.println("Converted String: " + str);
- This prints the converted string value on the console.
- Output:
- Converted String: 100
4. compareTo()
Used to compare two wrapper objects.
Returns:
- 0 if equal
- >0 if first is greater
- <0 if first is smaller
Example:
Integer a = 10, b = 20;
int result = a.compareTo(b);
System.out.println("Result: " + result);
Output:
Result: -1
Explanation:
- Integer a = 10, b = 20;
- Two Integer wrapper objects are created.
- The primitive values 10 and 20 are automatically converted into Integer objects.
- This automatic conversion is called autoboxing.
- int result = a.compareTo(b);
- The compareTo() method compares the values of two wrapper objects.
- It returns:
- 0 → if both values are equal
- A positive number → if the first value is greater
- A negative number → if the first value is smaller
- Here, since a = 10 and b = 20, the method returns -1 because 10 < 20.
- System.out.println("Result: " + result);
- This prints the comparison result on the console.
- Output:
- Result: -1
5. xxxValue()
Returns the primitive value of a wrapper object.
For example: intValue(), doubleValue(), booleanValue(), etc.
Example:
Integer obj = 50;
int num = obj.intValue();
System.out.println("Primitive value: " + num);
Output:
Primitive value: 50
Explanation:
- Integer obj = 50;
- Here, a wrapper object of type Integer is created.
- The primitive value 50 is automatically converted into an Integer object.
- This automatic conversion from a primitive to its wrapper class is called autoboxing.
- int num = obj.intValue();
- The intValue() method is used to extract the primitive value stored inside the wrapper object.
- In this case, it takes the value 50 from the Integer object and returns it as a primitive int.
- This is an example of manual unboxing (you are explicitly converting the wrapper to a primitive).
- System.out.println("Primitive value: " + num);
- This prints the primitive integer value on the console.
- Output:
- Primitive value: 50
Complete Example
public class ShikshaSanchar {
public static void main(String[] args) {
String str1 = "123";
int num = Integer.parseInt(str1); // String → int
Integer obj = Integer.valueOf(num); // int → Wrapper
String str2 = obj.toString(); // Wrapper → String
int compare = obj.compareTo(200); // Comparison
System.out.println("Parsed int: " + num);
System.out.println("Wrapper object: " + obj);
System.out.println("String form: " + str2);
System.out.println("Comparison result: " + compare);
}
}
Output:
Parsed int: 123
Wrapper object: 123
String form: 123
Comparison result: -1
Explanation:
- String str1 = "123";
- A string variable is created that contains numeric characters.
- Note that "123" is stored as text, not a number.
- int num = Integer.parseInt(str1);
- The parseInt() method converts the string "123" into a primitive integer value.
- After this line, num holds the value 123 as a primitive int.
- This is a simple conversion — no object involved yet.
- Integer obj = Integer.valueOf(num);
- The valueOf() method converts the primitive int 123 into an Integer object.
- Here, autoboxing happens — Java automatically wraps the primitive value into its wrapper class.
- Now obj is an Integer object that represents the number 123.
- String str2 = obj.toString();
- The toString() method converts the wrapper object back into a string form.
- After this, str2 stores "123" again, but as text.
- This shows the reverse process — converting from wrapper to string.
- int compare = obj.compareTo(200);
- The compareTo() method compares the Integer object (obj = 123) with another integer (200).
- It returns:
- 0 if both are equal
- 1 (or any positive number) if the first is greater
- -1 (or a negative number) if the first is smaller
- Since 123 < 200, it returns -1.
- Print statements:
- These display all the converted values and the comparison result.
Key Points
- All wrapper classes (Integer, Double, Boolean, etc.) have useful methods for conversion.
- parseXxx() → String to primitive
- valueOf() → String to wrapper object
- toString() → Object to String
- compareTo() → Compare wrapper objects
- xxxValue() → Convert wrapper to primitive
Summary:
- Wrapper methods make it simple to convert, compare, and format data in Java.
- They bridge the gap between Strings, objects, and primitive values, which is very useful when handling user input, data parsing, and numeric processing in real-world applications.