Java String valueOf() Method

Java string valueof() Method

String in java

In Java, a String is an object that represents a sequence of characters. Strings are immutable, meaning that once they are created, their values cannot be changed.

  • Strings are used frequently in Java programs to represent text, such as user input, program output, and data stored in text files.

To know more about String Class in java read the complete article.

String valueOf () Method:

The valueOf() method is a static method of the java.lang.String class. It returns the string representation of the passed argument. The argument can be of any type, such as a primitive type, an object, or an array.

  1. valueOf(boolean b) – Returns the string representation of the boolean argument.
  2. valueOf(char c) – Returns the string representation of the char argument.
  3. valueOf(int i) – Returns the string representation of the int argument.
  4. valueOf(long l) – Returns the string representation of the long argument.
  5. valueOf(float f) – Returns the string representation of the float argument.
  6. valueOf(double d) – Returns the string representation of the double argument.
  7. valueOf(char[] data) – Returns the string representation of the char array argument. 
  8. public static String valueOf(Object obj) – Returns the string representation of the Object argument.

The Syntax of this method :

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

Let’s look at a string-related Java program where the Java String valueOf() Method is used to perform an operation on the given string.

Example 1 : Java String valueOf() Method

Run
public class Main {
  public static void main(String[] args) {
    int i = 100;
    long l = 1223344445555L;
    float f = 3.14f;
    double d = 1.234;
    boolean b = true;
    char c = 'A';
    Object obj = new Object();

    String str = String.valueOf(i);
    System.out.println(str);  // prints "100"

    str = String.valueOf(l);
    System.out.println(str);  // prints "1223344445555"

    str = String.valueOf(f);
    System.out.println(str);  // prints "3.14"

    str = String.valueOf(d);
    System.out.println(str);  // prints "1.234"

    str = String.valueOf(b);
    System.out.println(str);  // prints "true"

    str = String.valueOf(c);
    System.out.println(str);  // prints "A"

    str = String.valueOf(obj);
    System.out.println(str);  // prints "java.lang.Object@hashcode"
  }
}

Output

100
1223344445555
3.14
1.234
true
A
java.lang.Object@2ff4acd0

Example 2 : Java string valueOf() Method

Run
public class Main{
   public static void main(String[] args) {
      String str = "PrepInsta has its own compiler";
      Object objVal = str;
      System.out.println("Value = " + str.valueOf(objVal));
   }
}

Output

Value = PrepInsta has its own compiler

Example 3: Java String ValueOf Method

Run

public class Main{
   public static void main(String args[]){
      double d = 090909.7997;
      boolean b = true;
      long l = 797979;
      char[] arr = {'P', 'r', 'e', 'p', 'i', 'n','s','t','a', };
      System.out.println("Return Value : " + String.valueOf(d) );
      System.out.println("Return Value : " + String.valueOf(b) );
      System.out.println("Return Value : " + String.valueOf(l) );
      System.out.println("Return Value : " + String.valueOf(arr) );
   }
}

Output

Return Value : 90909.7997
Return Value : true
Return Value : 797979
Return Value : Prepinsta

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription