Java String valueOf() Method

Java String valueOf() Method | The Java String class contains the valueOf() method which returns the value of the given object. There are nine variations in the valueOf() method they are as follows:-

1. valueOf(boolean bool): It returns the string representation of the boolean argument.
2. valueOf(char c):- It returns the string representation of the character argument.
3. valueOf(char[ ] ch):- It returns the string representation of the character array argument.
4. valueOf(char[ ] ch, int offset, int count):- It returns the string representation of the specific element in the character array.
5. valueOf(double d):- returns the string representation of the double.
6. valueOf(float floats):-returns the string representation of the float.
7. valueOf(int i):- returns the string representation of the integer.
8. valueOf(long l):- returns the string representation of the Long.
9. valueOf(Object object):- returns the string representation of the Object.

String.valueOf() Java Example

Let us see some examples of the Java String class valueOf() method. Using the valueOf() method we are able to convert primitive type and object values to string type.

public class Main {
   public static void main(String args[]) {
      double d = 7896523.2145994;
      boolean bool = true;
      long lng = 56987412;
      char[] array = { 'P', 'r', 'o', 'g', 'r', 'a', 'm' };

      System.out.println("Return Value: " + String.valueOf(d));
      System.out.println("Return Value: " + String.valueOf(bool));
      System.out.println("Return Value: " + String.valueOf(lng));
      System.out.println("Return Value: " + String.valueOf(array));
   }
}

Output:-

Return Value: 7896523.2145994
Return Value: true
Return Value: 56987412
Return Value: Program

The String.valueOf() method is also useful to convert an array of characters to a string object. We can either convert the complete array to a string object or some portion of the array to a string object.

Java String valueOf() Example

The below Java program demonstrates how to convert a boolean to a String in Java using the String.valueOf() method.

public class Main {
   public static void main(String[] args) {
      boolean bool = true;
      boolean bool2 = false;
      String str1 = String.valueOf(bool);
      String str2 = String.valueOf(bool2);
      System.out.println(str1);
      System.out.println(str2);
   }
}

Output:-

true
false

Java String.valueOf() Example

Using the String.valueOf() method we can convert a character into a String. For example, if we want to concatenate some character into a string then we can take the help of the valueOf() method to convert character to string. Later we can concatenate the string with a string using the concatenation operator.

public class Main {
   public static void main(String[] args) {
      char ch1 = 'A';
      char ch2 = 'Z';
      String str1 = String.valueOf(ch1);
      String str2 = String.valueOf(ch2);
      System.out.println(str1);
      System.out.println(str2);

      char ch3 = 's';
      String str3 = "Example";
      String str4 = str3 + String.valueOf(ch3);
      System.out.println(str4);
   }
}

Output:-

A
Z
Examples

String valueOf() Java Example

public class Main {
   public static void main(String[] args) {
      float f = 45.3f;
      double d = 102.25;
      String str1 = String.valueOf(f);
      String str2 = String.valueOf(d);
      System.out.println(str1);
      System.out.println(str2);
   }
}

Output:-

45.3
102.25

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *