C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Tip: We must use the equals method to compare the Strings. This compares their contents, not just references.
Java program that uses valueOf
public class Program {
public static void main(String[] args) {
// Convert 100 to a String and test it.
String value = String.valueOf(100);
if (value.equals("100")) {
System.out.println(true);
}
// Convert "false" to a String.
String literal = String.valueOf(false);
if (literal.equals("false")) {
System.out.println(true);
}
}
}
Output
true
true
Note: The copyValueOf method is the same as the String constructor that receives a char array.
Java program that uses copyValueOf
public class Program {
public static void main(String[] args) {
char[] letters = { 'a', 'b', 'c' };
// Use copyValueOf to convert char array to String.
String result = String.copyValueOf(letters);
System.out.println(result);
}
}
Output
abc