Typically in a graphical user interface, there will be text fields that are expecting the user to enter in a numerical value. This number value will end up in a String object which doesn't really help your program if you want to do some arithmetic. Fortunately, there are wrapper classes that provide methods for converting those String values into numbers and the String class has a method to convert them back again.
通常,在图形用户界面中 ,会有一些文本字段,希望用户输入数字值。 如果您想进行一些算术运算,则此数字值最终将出现在String对象中,该对象实际上对程序没有帮助。 幸运的是,有一些包装器类提供将这些String值转换为数字的方法,而String类具有一种将其再次转换回的方法。
包装类 ( Wrapper Classes )
The primitive data types that deal with numbers (i.e, byte, int, double, float, long, and short) all have class equivalents. These classes are known as wrapper classes as they take a primitive data type, and surround it with the functionality of a class. For example, the Double class will have a double value as its data and provide methods for manipulating that value.
处理数字(即字节,整数,双精度数,浮点数,长整数和短整数)的原始数据类型都具有类等效项。 这些类被称为包装器类,因为它们采用原始数据类型,并以类的功能将其包围。 例如,Double类将有一个double值作为其数据,并提供用于操纵该值的方法。
All of these wrapper classes have a method called valueOf. This method takes a String as an argument and returns an instance of the wrapper class. For example, let's say we have a String with the value of ten:
所有这些包装器类都有一个称为valueOf的方法。 此方法将String作为参数,并返回包装类的实例。 例如,假设我们有一个值为十的字符串:
String number
Having this number as a String is no use to us so we use the Integer class to convert it into an Integer object:
使用此数字作为String对我们没有用,因此我们使用Integer类将其转换为Integer对象:
Integer convertedNumber = Integer
Now the number can be used as a number and not a String:
现在,该数字可用作数字而不是字符串:
convertedNumber = converte
You can also make the conversion go straight to a primitive data type:
您还可以将转换直接转换为原始数据类型:
int convertedNumber = Integer.valueOf(nu
For other primitive data types, you just slot in the correct wrapper class—Byte, Integer, Double, Float, Long Short.
对于其他原始数据类型,您只需要插入正确的包装类即可—字节,整数,双精度,浮点型,长短型。
Note: You must make sure the String can be parsed into the appropriate data type. If it can't you will end up with a runtime error. For example, trying to covert "ten" into an integer:
注意:您必须确保可以将String解析为适当的数据类型。 如果不能,则将导致运行时错误。 例如,尝试将“十”转换为整数:
String number = "ten";
int convertedNumber = Integer.valueOf(nu
will produce a NumberFormatException because the compiler has no idea "ten" is supposed to be 10.
将产生NumberFormatException,因为编译器不知道“十”应该为10。
More subtly the same error will occur if you forget that an 'int' can only hold whole numbers:
如果您忘记“ int”只能容纳整数,则将更细微地发生相同的错误:
String number = "10.5";
int convertedNumber = Integer.valueOf(nu
The compiler won't truncate the number it will just think that it doesn't fit into an 'int' and that it's time to throw a NumberFormatException.
编译器不会截断该数字,只会认为它不适合'int'并且应该抛出NumberFormatException。
将数字转换为字符串 ( Converting Numbers to Strings )
To make a number into a String follows the same sort of pattern as the String class has a valueOf method too. It can take any of the primitive data type numbers as an argument and produce a String:
将数字转换为String的方式与String类也具有valueOf方法的方式相同。 它可以将任何原始数据类型数字作为参数并产生一个String:
int numberTwenty = 20;
int numberTwenty = 20;
String converted = String.valueOf(numberTwenty);
已转换的字符串= String.valueOf(numberTwenty);
which puts "20" as the String value of co nverted.
将“ 20”作为转换后的字符串值。
or you can use the toString method of any of the wrapper classes:
或者,您可以使用任何包装类的toString方法:
String converted = Integer.toStri
The toString method is common to all object types—most of the time it is just a description of the object. For wrapper classes, this description is the actual value they contain. In this direction, the conversion is a bit more robust. If the Double class was to be used instead of the Integer:
toString方法对所有对象类型都是通用的-大多数情况下,它只是对象的描述。 对于包装器类,此描述是它们所包含的实际值。 在这个方向上,转换会更可靠。 如果要使用Double类而不是Integer:
String converted = Double.toStri
the result would not cause a runtime error. The converted variable would contain the String "20.0".
结果不会导致运行时错误 。 转换后的变量将包含字符串“ 20.0”。
There is also a more subtle way to convert numbers when you are concatenating Strings. If a String was to be built like:
连接String时,还有一种更巧妙的方法来转换数字。 如果要像这样构建字符串:
String aboutDog = "My dog is " + numberTwenty + "
the conversion of the int numberTwenty is automatically done.
int number20的转换是自动完成的。
翻译自: https://www.thoughtco.com/converting-strings-to-numbers-and-vice-versa-2034313