Site icon DataFlair

Java String valueOf() Method

java string valueof()

Get Job-ready: Java Course with 45+ Real-time Projects! - Learn Java

Java’s valueOf() method is a public static method in the String class. It returns the string representation of the argument passed to it.

The main purpose of this method is to convert different types of values, such as integer, float, boolean, object, etc., into human-readable string representations. This enables us to output these values in a readable manner.

Many overloaded variants of the valueOf() method are available to handle the conversion of different data types, such as int, float, char, boolean, etc.

Syntax of Java valueOf() Method

Here are some of the commonly used variants of the Java valueOf() method:

These methods accept arguments of the specified data type and convert them into string representations.

Return Values of valueOf() method in Java

The return value of the valueOf() method depends on the argument passed to it:

Internal Working of Java valueOf() Method

Here is a code snippet showing the internal working of the valueOf(Object obj) method:

public static String valueOf(Object obj) {
  return (obj == null) ? "null" : obj.toString(); 
}

As we can see, it checks if the passed object is null. If yes, it returns the string “null”. Otherwise, it calls the toString() method on the object to convert it to a string representation.

Examples of Java valueOf() Method

Example 1: valueOf() with integer and float

public class ValueOfExample {
    public static void main(String[] args) {
        int iNum = 30;
        float fNum = 4.56789f;

        String str1 = String.valueOf(iNum);
        String str2 = String.valueOf(iNum) + 3091;
        String str3 = String.valueOf(fNum);
        String str4 = String.valueOf(fNum) + 914.56789;

        System.out.println(str1); // Prints "30"
        System.out.println(str2); // Prints "303091"
        System.out.println(str3); // Prints "4.56789"
        System.out.println(str4); // Prints "4.5678914.56789"
    }
}

Output:
30
303091
4.56789
4.5678914.56789

Example 2: valueOf() with char arrays

public class CharArrayToString {
    public static void main(String[] args) {
        char[] charArray = {'D', 'A', 'T', 'A', 'F', 'L', 'A', 'I', 'R'};

        String str1 = String.valueOf(charArray);
        String str2 = String.valueOf(charArray, 0, 4); // Modify the start and length
        String str3 = String.valueOf(charArray, 4, 5); // Modify the start and length

        System.out.println(str1); // Prints "DATAFLAIR"
        System.out.println(str2); // Prints "DATA"
        System.out.println(str3); // Prints "FLAIR"
    }
}

Output:
DATAFLAIR
DATA
FLAIR

Example 3: valueOf() with boolean

public class BooleanValueOfExample {
    public static void main(String[] args) {
        String str = "Hello, world!";
        boolean sta = str.contains("Hello");
        String str2 = String.valueOf(sta);

        System.out.println(str2); // Prints "true"
    }
}

Output:
true

Conclusion

In conclusion, the valueOf() method in Java’s String class is a versatile tool for converting various data types into human-readable string representations. It provides multiple overloaded variants to handle integers, floats, booleans, characters, objects, and more. The return value of this method depends on the argument passed to it, ensuring flexibility in string conversion.

The internal working of the valueOf() method involves checking whether the input object is null and then calling the toString() method to create the string representation.

This method is an essential part of Java programming. It enables developers to easily transform different data types into strings, making data output more accessible and user-friendly. Understanding how to use valueOf() effectively can be valuable for Java developers.

Exit mobile version