STRING STATIC METHODS
Java String valueOf() method
String.valueOf() converts values of different types to text. It works with numbers, Booleans, characters, objects and char arrays.
Converting a number to text
int taskai = 42;
String tekstas = String.valueOf(taskai);
System.out.println(tekstas);
System.out.println(tekstas + " taškai");42
42 taškai
- The variable
taskaiisintnumber. String.valueOf(taskai)creates a string representation of this number.- The result is stored in
Stringvariabletekstas. - From then on, the value can be joined with other text as an ordinary
String.
Why is valueOf() a static method?
valueOf() is called through the class, String, because it creates text from a supplied value and needs no existing String object:
String tekstas = String.valueOf(123);
On the left, String is the result type. String before the dot is the class that owns the static method.
Method syntax
| Syntax | Purpose |
|---|---|
valueOf(int i) | Converts an integer to text. |
valueOf(long l) | Converts a larger integer to text. |
valueOf(float f) | Converts a float value to text. |
valueOf(double d) | Converts a double value to text. |
valueOf(boolean b) | Returns text true or false. |
valueOf(char c) | Converts one character into a one-character string. |
valueOf(Object obj) | Uses the object's toString() result or the text null. |
valueOf(char[] data) | Creates text from an entire character array. |
valueOf(char[] data, int offset, int count) | Creates text from a selected part of a character array. |
Despite the many overloads, the central idea is simple: the supplied value becomes a new String.
Converting primitive values
boolean aktyvus = true;
char raide = 'J';
double kaina = 3.99;
System.out.println(String.valueOf(aktyvus));
System.out.println(String.valueOf(raide));
System.out.println(String.valueOf(kaina));true
J
3.99
booleanvalue becomes the text true or false.charvalue becomes a one-characterString.- A number becomes its string representation, but this is not a number-formatting method.
Converting an object to text
class Vartotojas {
private final String vardas;
Vartotojas(String vardas) {
this.vardas = vardas;
}
@Override
public String toString() {
return "Vartotojas: " + vardas;
}
}
Vartotojas vartotojas = new Vartotojas("Ieva");
String tekstas = String.valueOf(vartotojas);
System.out.println(tekstas);Vartotojas: Ieva
When passed an object, String.valueOf(objektas) uses the object's toString() result. It is therefore worth overriding toString()in classes whose objects will appear in text, logs or diagnostics.
Converting null to text
Object pasirinkimas = null;
String tekstas = String.valueOf(pasirinkimas);
System.out.println(tekstas);null
If Object value is null, String.valueOf() does not cause a NullPointerException. Instead, it returns the text null. This differs from directly calling pasirinkimas.toString(), which would not work on a null reference.
Converting a char array to text
char[] raides = {'J', 'a', 'v', 'a'};
String tekstas = String.valueOf(raides);
System.out.println(tekstas);Java
With char[] array, valueOf() behaves like copyValueOf(): characters are copied into a new immutable String.
Converting only part of a char array
char[] zinute = {'K', 'u', 'r', 's', 'a', 's', ':', ' ', 'J', 'a', 'v', 'a'};
String dalis = String.valueOf(zinute, 8, 4);
System.out.println(dalis);Java
- The array contains the text Kursas: Java.
offsetvalue8specifies starting at the characterJ.countvalue4specifies taking four characters.- The result is the new text Java.
valueOf() and concatenating an empty string
int lygis = 5;
String suTusciuTekstu = "" + lygis;
String suValueOf = String.valueOf(lygis);
System.out.println(suTusciuTekstu);
System.out.println(suValueOf);5
5
Both lines give the same visible result, but String.valueOf(lygis) makes the conversion intention clearer. Empty-string concatenation often appears in quick examples, but an explicit method is easier to read in teaching or team code.
valueOf() is not formatting
String.valueOf() creates a simple string representation. To control decimal places, alignment, percentages or locale, use String.format().
String.valueOf(3.5) returns 3.5, while String.format("%.2f", 3.5) can return 3.50.
Common mistakes
char[] raides = {'J', 'a', 'v', 'a'};
String tekstas = String.valueOf(raides, 2, 5);
System.out.println(tekstas);IndexOutOfBoundsException
- With
char[]portion,offset + countexceeds the array bounds. - A negative
offsetorcountis invalid. - Expecting special number formatting when
valueOf()only converts a value to text. - Assuming that
nullobject becomes empty text, although the result is null. - The object lacks a meaningful
toString(), producing a technical class name and hash value.
When should you use String.valueOf()?
String.valueOf() Use it to explicitly convert a value to a String: preparing output, combining values with text, storing a simple string form or safely handling a possible null object.