The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
Frequently, a program ends up with numeric data in a string objecta value entered by the user, for example.通常,程序以字符串对象中的数字数据结束例如,用户输入的值。
The 包装基本数字类型(Number
subclasses that wrap primitive numeric types ( Byte
, Integer
, Double
, Float
, Long
, and Short
) each provide a class method named valueOf
that converts a string to an object of that type.Byte
、Integer
、Double
、Float
、Long
和Short
)的Number
子类都提供了一个名为valueOf
的类方法,该类方法将字符串转换为该类型的对象。Here is an example, 以下是ValueOfDemo
, that gets two strings from the command line, converts them to numbers, and performs arithmetic operations on the values:ValueOfDemo
示例,它从命令行获取两个字符串,将其转换为数字,并对值执行算术运算:
public class ValueOfDemo { public static void main(String[] args) { // this program requires two // arguments on the command line if (args.length == 2) { // convert strings to numbers float a = (Float.valueOf(args[0])).floatValue(); float b = (Float.valueOf(args[1])).floatValue(); // do some arithmetic System.out.println("a + b = " + (a + b)); System.out.println("a - b = " + (a - b)); System.out.println("a * b = " + (a * b)); System.out.println("a / b = " + (a / b)); System.out.println("a % b = " + (a % b)); } else { System.out.println("This program " + "requires two command-line arguments."); } } }
The following is the output from the program when you use 以下是使用4.5
and 87.2
for the command-line arguments:4.5
和87.2
作为命令行参数时程序的输出:
a + b = 91.7 a - b = -82.7 a * b = 392.4 a / b = 0.0516055 a % b = 4.5
Number
subclasses that wrap primitive numeric types also provides a parseXXXX()
method (for example, parseFloat()
) that can be used to convert strings to primitive numbers.Number
子类还提供一个parseXXXX()
方法(例如,parseFloat()
),可用于将字符串转换为基元数字。parseFloat()
method is more direct than the valueOf()
method.parseFloat()
方法比valueOf()
方法更直接。ValueOfDemo
program, we could use:ValueOfDemo
程序中,我们可以使用:
float a = Float.parseFloat(args[0]); float b = Float.parseFloat(args[1]);
Sometimes you need to convert a number to a string because you need to operate on the value in its string form.有时需要将数字转换为字符串,因为需要对字符串形式的值进行操作。There are several easy ways to convert a number to a string:有几种简单的方法可以将数字转换为字符串:
int i; // Concatenate "i" with an empty string; conversion is handled for you. String s1 = "" + i;
or或
// The valueOf class method. String s2 = String.valueOf(i);
Each of the 每个Number
subclasses includes a class method, toString()
, that will convert its primitive type to a string.Number
子类都包含一个类方法toString()
,该方法将其原语类型转换为字符串。For example:例如:
int i; double d; String s3 = Integer.toString(i); String s4 = Double.toString(d);
The ToStringDemo
example uses the toString
method to convert a number to a string.ToStringDemo
示例使用toString
方法将数字转换为字符串。The program then uses some string methods to compute the number of digits before and after the decimal point:然后,程序使用一些字符串方法计算小数点前后的位数:
public class ToStringDemo { public static void main(String[] args) { double d = 858.48; String s = Double.toString(d); int dot = s.indexOf('.'); System.out.println(dot + " digits " + "before decimal point."); System.out.println( (s.length() - dot - 1) + " digits after decimal point."); } }
The output of this program is:该程序的输出为:
3 digits before decimal point. 2 digits after decimal point.