Home page

Saturday, June 24, 2023

Convert String to different data types in JAVA

In Java, you can convert a String to different data types using various methods provided by the respective wrapper classes or built-in conversion methods. Here are some common data types and their conversion methods:

Converting to Integer:

  • Using Integer.parseInt() method: int intValue = Integer.parseInt(stringValue);
  • Using Integer.valueOf() method: Integer intValue = Integer.valueOf(stringValue);

Converting to Double:

  • Using Double.parseDouble() method: double doubleValue = Double.parseDouble(stringValue);
  • Using Double.valueOf() method: Double doubleValue = Double.valueOf(stringValue);

Converting to Boolean:

  • Using Boolean.parseBoolean() method: boolean booleanValue = Boolean.parseBoolean(stringValue);
  • Using Boolean.valueOf() method: Boolean booleanValue = Boolean.valueOf(stringValue);

Converting to Character:

  • Getting the first character of the string: char charValue = stringValue.charAt(0);

Converting to Long:

  • Using Long.parseLong() method: long longValue = Long.parseLong(stringValue);
  • Using Long.valueOf() method: Long longValue = Long.valueOf(stringValue);

Note that when converting to numeric types (Integer, Double, Long), if the String cannot be parsed into a valid number format, it will throw a NumberFormatException. For converting to Boolean, the string is considered true if it equals ignoring case "true" or "1"; otherwise, it is considered false.

Here's an example that demonstrates how to convert a String to different data types:

public class StringConversionExample {

    public static void main(String[] args) {

        String intValueString = "123";

        int intValue = Integer.parseInt(intValueString);

        System.out.println("Converted Integer value: " + intValue);


        String doubleValueString = "3.14";

        double doubleValue = Double.parseDouble(doubleValueString);

        System.out.println("Converted Double value: " + doubleValue);


        String booleanValueString = "true";

        boolean booleanValue = Boolean.parseBoolean(booleanValueString);

        System.out.println("Converted Boolean value: " + booleanValue);


        String charValueString = "A";

        char charValue = charValueString.charAt(0);

        System.out.println("Converted Character value: " + charValue);


        String longValueString = "9876543210";

        long longValue = Long.parseLong(longValueString);

        System.out.println("Converted Long value: " + longValue);

    }

}

Output:
Converted Integer value: 123
Converted Double value: 3.14
Converted Boolean value: true
Converted Character value: A
Converted Long value: 9876543210

In this example, we convert different String values to Integer, Double, Boolean, Character, and Long data types using their respective conversion methods.

Make sure to handle exceptions appropriately, such as NumberFormatException, when performing string-to-numeric conversions to handle invalid input gracefully.

No comments:

Post a Comment