String Class Tutorials

public static String valueOf(boolean b)
public static String valueOf(char c)
public static String valueOf(char[] data)
public static String valueOf(char[] data, int offset, int count)
public static String valueOf(double d)
public static String valueOf(float f)
public static String valueOf(int i)
public static String valueOf(long l)
public static String valueOf(Object obj)

The valueOf() method is a great tool for converting primitive data types and character arrays into string values. The valueOf() method is overloaded and has nine different signatures. I will use each of the various flavors of this method in the code example, but I will explain how it works using the valueOf(int i) overload. All of the valueOf() overloaded methods are static; a static method can be directly invoked without creating an instance by using the ClassName.methodName() syntax. You can create an instance of the string class to invoke the method if you like, but it is much easier to get the return value by invoking the method directly.
For example,
int a = 956, b = 1045;
String title = String.valueOf(a+b) + ": A Space Odyssey";
System.out.println(title);
// 2001: A Space Odyssey



Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md StringValueOf
C:\Java>cd StringValueOf
C:\Java\StringValueOf>Notepad StringValueOf.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


class StringValueOf {
    public static void main(String args[]) {
        int a = 956, b = 1045;
        String title = String.valueOf(a+b) + ": A Space Odyssey";
        System.out.println(title); // 2001: A Space Odyssey 

        boolean t = true;
        char upperA = 65;
        char silly[] = { '8', '6', '7', '5', '3', '0', '9' };
        double pi = 3.1415926535D;
        float normal = 98.6f;
        long sun = 94_510_000;
        
        String temp = "";

        temp = String.valueOf(t);
        System.out.println(temp);

        temp = String.valueOf(upperA);
        System.out.println(temp);

        temp = "Jenny, don\'t change your number, " + String.valueOf(silly) + " ...";
        System.out.println(temp);

        temp = "Jenny, don\'t change your number, " + String.valueOf(silly, 0, 3) + "-" + String.valueOf(silly, 3, 4) + " ...";
        System.out.println(temp);

        temp = "pi is just about equal to " + String.valueOf(pi);
        System.out.println(temp);

        temp = "A normal adult temperature in fahrenheit is: " + String.valueOf(normal);
        System.out.println(temp);

        temp = "The Earth follows an elliptical path around the Sun, \nat the aphelion (most distant point), it is " + String.valueOf(sun) + " miles away.";
        System.out.println(temp);

        temp = String.valueOf(new StringValueOf());
        System.out.println(temp);
    }	

    @Override
    public String toString() {
        return "Simple value returned by invoking the toString() method";
    }
}


Now switch back to the command prompt (CMD) and type in javac StringValueOf.java and press Enter.
Now type in java StringValueOf and press Enter.


C:\Java\StringValueOf>javac StringValueOf.java
C:\Java\StringValueOf>java StringValueOf
2001: A Space Odyssey
true
A
Jenny, don't change your number, 8675309 ...
pi is just about equal to 3.141592535
A normal adult temperature in farenheit is: 98.6
The Earth follows an elliptical path around the sun,
at the aphelion (most distant point), it is 94510000 miles away.
Simple value returned by invoking the toString() method


Final thoughts

One strategy that I regularly use to solidify my knowledge of Java is to browse through the actual source code. Due to some language in their copyright file, I can't actually show the source code in my videos. The source code is contained inside of a file called src.zip located in the folder where your JDK is installed. I recommend using notepad++ to browse the class files.


Tutorials