Utility Methods of Wrapper Classes in Java

Prerequisite: Wrapper Classes
The objective of the Wrapper class is to define several utility methods which are required for the primitive types. There are 4 utility methods for primitive type which is defined by the Wrapper class:
1. valueOf() method:
We can use the valueOf() method to create a Wrapper object for a given primitive or String. There are 3 types of valueOf() methods:
A. Wrapper valueOf(String s): Every wrapper class except Character class contains a static valueOf() method to create Wrapper class object for a given String.
Syntax:
public static Wrapper valueOf(String s);
Java
// Java program to illustrate valueof() Method class GFG { public static void main(String[] args) { Integer I = Integer.valueOf("10"); System.out.println(I); Double D = Double.valueOf("10.0"); System.out.println(D); Boolean B = Boolean.valueOf("true"); System.out.println(B); // Here we will get RuntimeException Integer I1 = Integer.valueOf("ten"); }} |
Output:
10 10.0 true Exception in thread "main" java.lang.NumberFormatException: For input string: "ten"
B. Wrapper valueOf(String s, int radix): Every Integral Wrapper class Byte, Short, Integer, Long) contains the following valueOf() method to create a Wrapper object for the given String with specified radix. The range of the radix is 2 to 36.
Syntax:
public static Wrapper valueOf(String s, int radix)
Java
// Java program to illustrate valueof() Method class GFG { public static void main(String[] args) { Integer I = Integer.valueOf("1111", 2); System.out.println(I); Integer I1 = Integer.valueOf("1111", 4); System.out.println(I1); }} |
15 85
3. Wrapper valueOf(primitive p): Every Wrapper class including the Character class contains the following method to create a Wrapper object for the given primitive type.
Syntax:
public static Wrapper valueOf(primitive p);
Java
// Java program to illustrate valueof() Method class GFG { public static void main(String[] args) { Integer I = Integer.valueOf(10); Double D = Double.valueOf(10.5); Character C = Character.valueOf('a'); System.out.println(I); System.out.println(D); System.out.println(C); }} |
10 10.5 a
2. xxxValue() Method
We can use xxxValue() methods to get the primitive for the given Wrapper Object. Every number type Wrapper class( Byte, Short, Integer, Long, Float, Double) contains the following 6 methods to get primitive for the given Wrapper object:
- public byte byteValue()
- public short shortValue()
- public int intValue()
- public long longValue()
- public float floatValue()
- public float doubleValue()
3. parseXxx() Method
We can use parseXxx() methods to convert String to primitive. There are two types of parseXxx() methods:
A. primitive parseXxx(String s): Every Wrapper class except the character class contains the following parseXxx() method to find primitive for the given String object.
Syntax:
public static primitive parseXxx(String s);
Java
// Java program to illustrate parseXxx() Method class GFG { public static void main(String[] args) { int i = Integer.parseInt("10"); double d = Double.parseDouble("10.5"); boolean b = Boolean.parseBoolean("true"); System.out.println(i); System.out.println(d); System.out.println(b); }} |
10 10.5 true
B. parseXxx(String s, int radix): Every Integral type Wrapper class (Byte, Short, Integer, Long) contains the following parseXxx() method to convert specified radix String to primitive.
Syntax:
public static primitive parseXxx(String s, int radix);
Java
// Java program to illustrate parseXxx() Method class GFG { public static void main(String[] args) { int i = Integer.parseInt("1000", 2); long l = Long.parseLong("1111", 4); System.out.println(i); System.out.println(l); }} |
8 85
4. toString() Method
We can use the toString() method to convert the Wrapper object or primitive to String. There are a few forms of the toString() method:
A. public String toString(): Every wrapper class contains the following toString() method to convert Wrapper Object to String type.
Syntax:
public String toString();
Java
// Java program to illustrate toString() Method class GFG { public static void main(String[] args) { Integer I = new Integer(10); String s = I.toString(); System.out.println(s); }} |
Output:
10
B. toString(primitive p): Every Wrapper class including the Character class contains the following static toString() method to convert primitive to String.
Syntax:
public static String toString(primitive p);
Java
// Java program to illustrate toString() class GFG { public static void main(String[] args) { String s = Integer.toString(10); System.out.println(s); String s1 = Character.toString('a'); System.out.println(s1); }} |
10 a
C. toString(primitive p, int radix): Integer and Long classes contain the following toString() method to convert primitive to specified radix String.
Syntax:
public static String toString(primitive p, int radix);
Java
// Java program to illustrate toString() Method class GFG { public static void main(String[] args) { String s = Integer.toString(15, 2); System.out.println(s); String s1 = Long.toString(11110000, 4); System.out.println(s1); }} |
1111 222120121300



