In this guide, we will discuss one of the most commonly used method in Java. The valueOf()
method is available in several wrapper classes and other classes such as String class. This method is mostly used in the conversion of types, for example, converting int to Integer, double to Double etc.
ValueOf() method usage In Wrapper Classes
In wrapper classes, the valueOf() method is used for converting primitive types such as int, float double to Wrapper class objects such as Integer, Float, Double etc.
Integer.valueOf()
Converts a primitive int
or a String
to an Integer
object.
public class ValueOfExample {
public static void main(String[] args) {
// From int to Integer
Integer intObj1 = Integer.valueOf(101);
// From String to Integer
Integer intObj2 = Integer.valueOf("200");
System.out.println(intObj1); // Output: 101
System.out.println(intObj2); // Output: 200
}
}
Double.valueOf()
Converts a primitive double
or a String
to a Double
object.
public class ValueOfExample {
public static void main(String[] args) {
// From double to Double
Double doubleObj1 = Double.valueOf(10.55);
// From String to Double
Double doubleObj2 = Double.valueOf("20.55");
System.out.println(doubleObj1); // Output: 10.55
System.out.println(doubleObj2); // Output: 20.55
}
}
Boolean.valueOf()
Converts a primitive boolean
or a String
to a Boolean
object.
public class ValueOfExample {
public static void main(String[] args) {
// From boolean to Boolean
Boolean boolObj1 = Boolean.valueOf(true);
// From String to Boolean
Boolean boolObj2 = Boolean.valueOf("false");
System.out.println(boolObj1); // Output: true
System.out.println(boolObj2); // Output: false
}
}
ValueOf() method In the String
Class
In the String class, the valueOf()
method is used to convert various data types into their string representation.
Example of conversion from different data type to String
public class ValueOfStringExample {
public static void main(String[] args) {
// int to String Conversion
String str1 = String.valueOf(100);
System.out.println(str1); // Output: "100"
// double to String Conversion
String str2 = String.valueOf(10.5);
System.out.println(str2); // Output: "10.5"
// boolean to String Conversion
String str3 = String.valueOf(true);
System.out.println(str3); // Output: "true"
// char array to String Conversion
char[] charArray = {'J', 'a', 'v', 'a'};
String str4 = String.valueOf(charArray);
System.out.println(str4); // Output: "Java"
// Object to String Conversion
Object obj = new Object();
String str5 = String.valueOf(obj);
System.out.println(str5);
}
}
Points to Note:
- Overloading of valueOf() Method: The
valueOf()
method is overloaded in wrapper classes andString
class to handle different input types (e.g., primitive types and strings). - Used for Conversion: It provides an easy way to convert primitive data types to Wrapper objects as well as other data types to Strings.
- Handles Null Input: In case of wrapper classes, if the input is null, it throws
NumberFormatException
. In case ofString
class,String.valueOf(null)
returns the string “null” if the input data is null.
Leave a Reply