ZMedia Purwodadi

Java String valueOf() method explained | technical-arbaab

Table of Contents

Java String valueOf() method explained


It is a predefined function which is present in the Java.lang.String package. It is used to convert different types of values into string like float to string, integer to string, double to string, character array to string, long to string, boolean to string and object to string, and this function returns the string representation of different values, let us understand each of these with some examples-

Types of valueOf() method in Java

Based upon the parameter it takes, this function is classified into various types which are listed below-

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

Example 01 of valueOf(int i) method

 public class ValueOf {  
      public static void main(String[] args)  
      {  
           int s1 = 45;  
           String s2 = null;   
           s2=s2.valueOf(s1);  
           System.out.println(s2+20);  
      }       
 }  
Output:
 4520  

Example 02 of valueOf(float f) method

 public class ValueOf {  
      public static void main(String[] args)  
      {  
           float s1 = 4.5f;  
           String s2 = null;  
           s2=s2.valueOf(s1);  
           System.out.println(s2+20);  
      }       
 }  
Output:
 4.520  

Example 03 of valueOf(long l) method

 public class ValueOf {  
      public static void main(String[] args)  
      {  
           long s1 = 4555555555l;  
           String s2 = null;  
           s2=s2.valueOf(s1);  
           System.out.println(s2+20);  
      }       
 }  
Output:
 455555555520  

Example 04 of valueOf(double d) method

 public class ValueOf {  
      public static void main(String[] args)  
      {  
           double s1 = 8.999d;  
           String s2 = null;  
           s2=s2.valueOf(s1);  
           System.out.println(s2+10);  
      }       
 }  
Output:
 8.99910  

Example 05 of valueOf(char c) method

 public class ValueOf {  
      public static void main(String[] args)  
      {  
           char s1 = 'a';  
           String s2 = null;  
           s2=s2.valueOf(s1);  
           System.out.println(s2+10);  
      }       
 }  
Output:
 a10  

Example 06 of valueOf(char data[]) method

 public class ValueOf {  
      public static void main(String[] args)  
      {  
           char s1[] = {'m','a','r','v','e','l'};  
           String s2 = null;  
           s2=s2.valueOf(s1);  
           System.out.println(s2+10);  
      }       
 }  
Output:
 marvel10  

Example 07 of valueOf(boolean b) method

 public class ValueOf {  
      public static void main(String[] args)  
      {  
           boolean s1 = true;  
           String s2 = null;  
           s2=s2.valueOf(s1);  
           System.out.println(s2+10);  
      }       
 }  
Output:
 true10  

here, we have the boolean value true, is first converted into string and then we concat this string value with 10 as you have seen clearly so the output simply concat 10 with this string value true and gives you true10.

Example 08 of valueOf(Object obj) method

 public class ValueOf {  
      public static void main(String[] args) throws InterruptedException  
      {  
      ValueOf v = new ValueOf();  
      String s2 = null;   
      s2=s2.valueOf(v);  
      System.out.println("without concat the value is:"+s2+"\n");  
      System.out.println("after concat the value is:"+s2+20);  
      }       
 }  
Output:
 without concat the value is:code4blog.strings.ValueOf@43a25848  
 after concat the value is:code4blog.strings.ValueOf@43a2584820  

here, we have first printed the string representation of object directly and then we concat this string representation of object with 20 and outputs it simply. As it is seen clearly in the above code output that 20 has been concatenated successfully with the first output value.

Example 09 all valueOf() methods in one program

 public class ValueOf {  
      public static void main(String[] args)   
      {  
           String s1 = null;  
           int i = 10;  
           long l = 45l;  
           float f = 5.6f;  
           double d = 78.9d;  
           char c = 'a';  
           char array[] = {'m','a','r','v','e','l'};  
           boolean b = true;  
           ValueOf v = new ValueOf();  
           System.out.println(s1.valueOf(i));  
           System.out.println(s1.valueOf(l));  
           System.out.println(s1.valueOf(f));  
           System.out.println(s1.valueOf(d));  
           System.out.println(s1.valueOf(c));  
           System.out.println(s1.valueOf(array));  
           System.out.println(s1.valueOf(b));  
           System.out.println(s1.valueOf(v));  
      }       
 }  
Output:
 10  
 45  
 5.6  
 78.9  
 a  
 marvel  
 true  
 code4blog.strings.ValueOf@7f63425a  

Imp.note: The last valueOf() method which takes the object as a parameter actually, gives you all the details of the location of itself. For example here, code4blog.strings is the package which contains the class ValueOf and inside the ValueOf class we have a main method in which the object of ValueOf class has actually created (hope it is clear). If you don't believe me you can try to print the object of valueOf class directly and see what it outputs as shown below:

 package code4blog.strings;  
 public class ValueOf {  
      public static void main(String[] args)   
      {  
           ValueOf v= new ValueOf();  
           System.out.println(v);  
      }       
 }  

Output:

 code4blog.strings.ValueOf@43a25848  

If you find this article helpful let us know in the comment section below and share it with your friends who are java lovers.

Arbaab khan
Arbaab khan FEAR has two meanings "forget everything and run" "Face everything and rise" The choice is yours Welcome u in our tech blog

إرسال تعليق