Java 17 Recipes
-11. Converting Strings to Numeric Values
Download 3.2 Mb. Pdf ko'rish
|
Java 17 Recipes
- Bu sahifa navigatsiya:
- How It Works
3-11. Converting Strings to Numeric Values
Problem You want to have the ability to convert any numeric values that are stored as strings into integers. Solution 1 Use the Integer.valueOf() helper method to convert strings to int data types. The following is an example. String one = "1"; String two = "2"; int result = Integer.valueOf(one) + Integer.valueOf(two); As you can see, both string variables are converted into integer values. After that, they perform an addition calculation and then stored into an int. The final result is 3. Chapter 3 StringS 94 Note a technique known as autoboxing is used in this example. autoboxing is a feature of the Java language that automates converting primitive values to their appropriate wrapper classes. For instance, this occurs when you assign an int value to an integer. Similarly, unboxing automatically occurs when you convert in the opposite direction, from a wrapper class to a primitive. Solution 2 Use the Integer.parseInt() helper method to convert strings to int data types. The following is an example. String one = "1"; String two = "2"; int result = Integer.parseInt(one) + Integer.parseInt(two); System.out.println(result); The final result is 3. How It Works The Integer class contains the valueOf() and parseInt() methods, which convert strings or int types into integers. There are two different forms of the Integer class’s valueOf() type that can convert strings into integer values. Each of them differs by the number of arguments that they accept. The first valueOf() method accepts only a string argument. This string is then parsed as an integer value if possible, and then an integer holding the value of that string is returned. If the string does not convert into an integer correctly, then the method throws NumberFormatException. The second version of the valueOf() method accepts two arguments: a string argument parsed as an integer and an int representing the radix used for the conversion. Note Many Java-type classes contain valueOf() methods that can be used to convert different types into that class’s type. Such is the case with the String class because it contains many different valueOf() methods that can be used for conversion. Chapter 3 StringS 95 There are also two different forms of the Integer class’s parseInt() method. One of them accepts one argument: the string you want to convert into an integer. The other form accepts two arguments: the string that you want to convert to an integer and the radix. The first format is the most widely used, and it parses the string argument as a signed decimal integer. A NumberFormatException is thrown if a parsable unsigned integer is not contained within the string. The second format, which is less widely used, returns an Integer object holding the value represented by the string argument in the given radix, given a parsable unsigned integer is contained within that string. Note One of the biggest differences between parseInt() and valueOf() is that parseInt() returns an int and valueOf() returns an integer from the cache. Download 3.2 Mb. Do'stlaringiz bilan baham: |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling