The valueOf() method in Java is a static method of the String class. It is used to convert different types of data (like numbers, characters, objects, or booleans) into a String. This method is particularly useful when you need to create string representations of various data types.


Java String ValueOf() Method

Syntax

public static String valueOf(dataType data)

Supported Data Types

The valueOf() method supports the following types:

  1. Primitive data types (int, float, double, char, boolean, etc.)
  2. Arrays
  3. Objects

If the argument is null, the valueOf() method returns the string "null".


Common Variants

  1. valueOf(int i)
    Converts an integer to a string.
  2. valueOf(double d)
    Converts a double to a string.
  3. valueOf(boolean b)
    Converts a boolean to a string ("true" or "false").
  4. valueOf(char c)
    Converts a single character to a string.
  5. valueOf(Object obj)
    Converts an object to a string by calling the object’s toString() method.

Examples

Example 1: Convert Primitive Types to Strings

public class ValueOfExample1 {
public static void main(String[] args) {
int num = 100;
double d = 45.67;
boolean flag = true;
String strNum = String.valueOf(num);
String strDouble = String.valueOf(d);
String strFlag = String.valueOf(flag);
System.out.println("String value of num: " + strNum); // "100"
System.out.println("String value of double: " + strDouble); // "45.67"
System.out.println("String value of flag: " + strFlag); // "true"
}
}

Example 2: Convert Character to String

public class ValueOfExample2 {
public static void main(String[] args) {
char ch = 'A';

String strChar = String.valueOf(ch);

System.out.println("String value of char: " + strChar); // "A"
}
}


Example 3: Convert Object to String

public class ValueOfExample3 {
public static void main(String[] args) {
Object obj = new String("Java");

String strObj = String.valueOf(obj);

System.out.println("String value of object: " + strObj); // "Java"
}
}


Example 4: Convert Null to String

public class ValueOfExample4 {
public static void main(String[] args) {
Object obj = null;

String strObj = String.valueOf(obj);

System.out.println("String value of null object: " + strObj); // "null"
}
}


Example 5: Convert Array to String

public class ValueOfExample5 {
public static void main(String[] args) {
char[] charArray = {'H', 'e', 'l', 'l', 'o'};
String strArray = String.valueOf(charArray);

System.out.println("String value of char array: " + strArray); // "Hello"
}
}


Java String ValueOf() Method

Key Points

  1. Null Handling:
    • If the argument is null and it is an object, valueOf() returns the string "null".
    • For primitive types, it converts the value directly.
  2. Usage in Debugging:
    • The valueOf() method is helpful when converting data to strings for logging or debugging.
  3. Efficient Conversion:
    • It is optimized for converting primitives, making it a better choice than concatenation for readability and performance.

Difference Between toString() and valueOf()

Aspect toString() valueOf()
Null Argument Handling Throws NullPointerException. Returns the string "null".
Static/Instance Method Instance method (called on objects). Static method (called on String class).
Primary Use Converts an object to a string. Converts any data type to a string.
 

valueOf() is a static method present in the String class. It is used to convert any data type, such as int, double, char, or char array, to its string representation by creating a new String object. We can also use valueOf() to convert a char array or a subarray of a char array to a String.

Related Posts

Leave a Reply