In Java, handling data types effectively is crucial for building robust applications. One common requirement is converting between String and other data types. Whether you’re dealing with numeric data, dates, or other primitive types, Java provides multiple methods to convert between String and other data types seamlessly.
String to Other Data Types
Converting a String to another type is known as parsing. Java provides several methods to convert String to other primitive types or objects.
- String to Integer: To convert a
Stringto anint, you can useInteger.parseInt()orInteger.valueOf().
|
1 2 3 |
String numberStr = "123"; int number = Integer.parseInt(numberStr); System.out.println(number); // Output: 123 |
Integer.parseInt() throws a NumberFormatException if the string cannot be parsed as an integer.
- String to Double: For converting a
Stringto adouble, you can useDouble.parseDouble().
|
1 2 3 |
String doubleStr = "3.14"; double pi = Double.parseDouble(doubleStr); System.out.println(pi); // Output: 3.14 |
Similarly, if the string doesn’t represent a valid double, a NumberFormatException is thrown.
- String to Boolean: To convert a
Stringto aboolean, useBoolean.parseBoolean().
|
1 2 3 |
String booleanStr = "true"; boolean isTrue = Boolean.parseBoolean(booleanStr); System.out.println(isTrue); // Output: true |
This method returns false if the string is not "true" (case insensitive).
- String to Date: To convert a
Stringto aDate, you need to use theSimpleDateFormatclass, as it allows for parsing dates in different formats.
|
1 2 3 4 5 6 7 |
import java.text.SimpleDateFormat; import java.util.Date; String dateStr = "2025-02-09"; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Date date = sdf.parse(dateStr); System.out.println(date); // Output: Sat Feb 09 00:00:00 GMT 2025 |
This will throw a ParseException if the string does not match the expected date format.
Other Data Types to String
Converting from other data types to a String is usually straightforward in Java. Java provides methods to convert numeric types, booleans, and other objects into their String representations.
- Integer to String: To convert an integer to a
String, you can useString.valueOf()orInteger.toString().
|
1 2 3 |
int number = 123; String numberStr = String.valueOf(number); System.out.println(numberStr); // Output: "123" |
or
|
1 2 |
String numberStr2 = Integer.toString(number); System.out.println(numberStr2); // Output: "123" |
- Double to String: For converting a
doubleto aString, you can useString.valueOf()orDouble.toString().
|
1 2 3 |
double pi = 3.14; String piStr = String.valueOf(pi); System.out.println(piStr); // Output: "3.14" |
- Boolean to String: Converting a boolean to a
Stringis simple withString.valueOf()orBoolean.toString().
|
1 2 3 |
boolean isTrue = true; String boolStr = String.valueOf(isTrue); System.out.println(boolStr); // Output: "true" |
- Date to String: Converting a
Dateobject to aStringcan be done usingSimpleDateFormat. You can specify the format for how the date should appear in the string.
|
1 2 3 4 5 6 7 |
import java.text.SimpleDateFormat; import java.util.Date; Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String dateStr = sdf.format(date); System.out.println(dateStr); // Output: "2025-02-09" |