There are times when we want to convert data from primitive data types into a string, for instance when we want to format output on the screen or simply mixing it with other string. Using a various static method String.valueOf()
we can get a string value of them.
Here is the code sample:
package org.kodejava.lang;
public class StringValueOfExample {
public static void main(String[] args) {
boolean b = false;
char c = 'c';
int i = 100;
long l = 100000;
float f = 3.4f;
double d = 500.99;
String u = String.valueOf(b);
String v = String.valueOf(c);
String w = String.valueOf(i);
String x = String.valueOf(l);
String y = String.valueOf(f);
String z = String.valueOf(d);
}
}
When called with boolean argument the String.valueOf()
method return true
or false
string depending on the boolean argument value. When called with char argument, a 1 length sized string returned.
For int
, long
, float
, double
the results are the same as calling Integer.toString()
, Long.toString()
, Float.toString()
and Double.toString()
respectively.
Latest posts by Wayan (see all)
- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024