valueOf() method

Let us understand valueOf() method


valueOf() is used to convert different types of values to String type.

String class has overloaded valueOf() methods for all the primitive types and for object type

Signature of overloaded valueOf() methods


public static String valueOf(boolean b)

public static String valueOf(char c)

public static String valueOf(char[] c)

public static String valueOf(char[] c,int offset,int count)

public static String valueOf(int i)

public static String valueOf(long l)

public static String valueOf(float f)

public static String valueOf(double d)

public static String valueOf(Object o)


Example for integer to String

1
2
3
4
5
6
7
8
9
public class StringValueOfExample1{  
public static void main(String args[]){  
int num=30;  
String numStr=String.valueOf(num);  
System.out.println(numStr);
System.out.println(numStr.length()); 
 
}
}  
public class StringValueOfExample1{  
public static void main(String args[]){  
int num=30;  
String numStr=String.valueOf(num);  
System.out.println(numStr);
System.out.println(numStr.length()); 

}
}  



Example for Double to String

1
2
3
4
5
6
7
8
9
public class StringValueOfExample2{  
public static void main(String args[]){  
double num=30.5;  
String numStr=String.valueOf(num);  
System.out.println(numStr);
System.out.println(numStr.length()); 
 
}
}  
public class StringValueOfExample2{  
public static void main(String args[]){  
double num=30.5;  
String numStr=String.valueOf(num);  
System.out.println(numStr);
System.out.println(numStr.length()); 

}
}  



Example for char[] to String with specified count and offset

1
2
3
4
5
6
7
public class StringValueOfExample3{  
public static void main(String args[]){  
char[] data={'j','a','v','a','i','n','s','i','m','p','l','e','w','a','y'};  
String str=String.valueOf(data,6,9);  
System.out.println(str); 
}
}  
public class StringValueOfExample3{  
public static void main(String args[]){  
char[] data={'j','a','v','a','i','n','s','i','m','p','l','e','w','a','y'};  
String str=String.valueOf(data,6,9);  
System.out.println(str); 
}
}  


About the Author

Karibasappa G C (KB)
Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on