valueOf() Stirng Example

Many methods exist in java.lang.String class used for different string manipulations like checking the availability of a character, to extract a part of a string or to convert a data type to string etc. One method boon to Developers is valueOf() overloaded 9 times taking different parameters.

String.valueof() converts all primitive data types and objects into string form and returns. Original primitive data type is not changed. The returned value is equivalent string representation to data type. See the notes after screenshot.

valueOf() method is overloaded 9 times in String class. All are static methods. All return type is string. Following are method signatures.

  1. public static String valueOf(boolean b): Returns a string representing boolean value b. Otherway to say, converts b into string and returns. If the parameter passed is boolean value true, true is returned as string and if false is passed, returns false as string.
  2. public static String valueOf(char ch): Converts char ch into string and returns. As the argument takes a single character, the returned string is of length of 1.
  3. public static String valueOf(int x): Converts int x into string and returns. To this method, byte and short values also can be passed. They are casted to int and evaluated.
  4. public static String valueOf(long x): Converts long x into string and returns.
  5. public static String valueOf(float x): Converts float x into string and returns.
  6. public static String valueOf(double x): Converts double x into string and returns.
  7. public static String valueOf(Object obj): Converts any Java object obj into string and returns.
  8. public static String valueOf(char ch[]): Converts the char array ch into string and returns. After returning, if the array elements are modified, the changes will not be reflected in the string.
  9. public static String valueOf(char ch[], int offset, int count): In the previous method, whole character array is converted into string. But here is an option. We can obtain a string by converting a few elements of the array (need not be full array). offset gives from what element and count gives number of elements. See the notes.
Following example illustrates the usage of valueOf() all the 9 overloaded methods.
public class StringMethodDemo
{
  public static void main(String args[])
  {                            // data types
   boolean b = true;  	       // similarly, byte and short also can be passed
   char ch = 'A';  	
   int x = 10;  	
   long y = 25L;  	
   float k = 5.6F;  	
   double m = 14.8; 
                               // objects 	
   Date d1 = new Date();  	
   char ch1[]  = { 'A', 'B', 'C', 'D', 'E', 'F','G' };

   String s1 = String.valueOf(b);
   String s2 = String.valueOf(ch);
		
   System.out.println("boolean true in string form: " + s1);
   System.out.println("char 'A' in string form: " + s2);
   System.out.println("int 10 in string form: " + String.valueOf(x));
   System.out.println("long 25L in string form: " + String.valueOf(y));
   System.out.println("float 5.6F in string form: " + String.valueOf(k));
   System.out.println("double 14.8 in string form: " + String.valueOf(m));

   System.out.println("\nDate d1 in string form: " + String.valueOf(d1));
   System.out.println("\nAll elements of char array ch1 in string form: " + String.valueOf(ch1));
   System.out.println("Few elements of char array ch1 in string form: " + String.valueOf(ch1, 2, 4));
  }
}


valueOf()
Output screen of different ways of using valueOf() method

long y = 25L;
float k = 5.6F;

In our primitive data types discussion, we know a long value is to be suffixed with L (or lowercase l) but is not mandatory. But it is mandatory to suffix with F (or lowercase f) a float value.

char ch1[] = { 'A', 'B', 'C', 'D', 'E', 'F','G' };
System.out.println(“Few elements of char array ch1 in string form: ” + String.valueOf(ch1, 2, 4));

In the above overloaded char array argument method, 2 indicates starting index number in the array from which string representation is required. 2 indicates character C and 4 indicates number of characters starting from C; it is obviously CDEF.

Like this many methods of String class decreases your code of string maniupulation. Same in C/C++ languages require to write your own laborious code with many for loops and flags. String (with other classes like Math, Character, GregorianCalendar, Observer and Obervable etc.) makes you to feel happy that you are lucky in Java language coding.

1 thought on “valueOf() Stirng Example”

  1. Lochan kumar khuntia

    This is tutorial really very help full to me, it is really very deep concept of String class.

    Thanking you sir, for sharing your this tutorial with us.

Leave a Comment

Your email address will not be published.