.valueOf()
Published Oct 31, 2022
Contribute to Docs
The .valueOf()
method returns the string representation of a given value.
Syntax
String.valueOf(value)
The .valueOf()
method is static and is called directly from the String
class.
The value
parameter can be one of the following data types:
int
long
float
double
boolean
char
Example
The following is an example of the .valueOf()
method being applied to values of different data types:
// Main.javapublic class Main {public static void main(String[] args) {// Concatenating an integer with stringint x = 20;String str = "22";System.out.println(String.valueOf(x) + str);// String representation of boolean argumentboolean b = true;System.out.println(String.valueOf(b));// Concatenating string with floatfloat f = 4.66f;System.out.println(str + String.valueOf(f));// Char to Stringchar ch = 'A';System.out.println(String.valueOf(ch));}}
This will produce the following output:
2022true224.66A
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Java on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn Java
Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.Beginner Friendly17 hours