Formatting data types to String and vice versa

suggest change

Data types to string formatting

Data types like int, float, double, long, boolean can be formatted to string using String.valueOf().

String.valueOf(1); //Output -> "1"
String.valueOf(1.0); //Output -> "1.0"
String.valueOf(1.2345); //Output -> "1.2345"
String.valueOf(true); //Output -> "true"

The other direction parses a string into a value:

Integer.parseInt("1"); //Output -> 1
Float.parseFloat("1.2"); //Output -> 1.2
Boolean.parseBoolean("true"); //Output -> true

Boolean.parseBoolean returns true only for the word true, ignoring case. Any other text returns false. Integer.parseInt and Float.parseFloat throw NumberFormatException when the text is not a number.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents