A data type is a set of values and a set of operations defined on them. (数据类型包括值和对应的操作符) For example, we are familiar with numbers and with operations defined on them such as addition and multiplication. There are eight different built-in types of data in Java, mostly different kinds of numbers. We use the system type for strings of characters so frequently that we also consider it here.
Terminology. We use the following code fragment to introduce some terminology(专有名词):
int a, b, c; a = 1234; b = 99; c = a + b;
The first line is a declaration statement that declares the names of three variables using the identifiers a, b, and c and their type to be int. The next three lines are assignment statements that change the values of the variables, using the literals(文字量) 1234 and 99, and the expression a + b, with the end result that c has the value 1333.
Characters and strings. A char is an alphanumeric character (字母数字字符) or symbol, like the ones that you type. We usually do not perform any operations on characters other than assigning values to variables. A String is a sequence of characters. The most common operation that we perform on strings is known as concatenation(连接): given two strings, chain them together to make a new string. For example, consider the following Java program fragment:
String a, b, c; a = "Hello,"; b = " Bob"; c = a + b;
The first statement declares three variables to be of type String. The next three statements assign values to them, with the end result that c has the value "Hello, Bob". Using string concatenation, Ruler.java prints the relative lengths of the subdivisions on a ruler. (链接中的每一份代码,都应该打开看懂、编译并运行通过。)
Integers. An int is an integer (whole number) between −231 and 231 − 1 (−2,147,483,648 to 2,147,483,647). We use ints frequently not just because they occur frequently in the real world, but also they naturally arise when expressing algorithms. Standard arithmetic operators for addition, multiplication, and division, for integers are built into Java, as illustrated in IntOps.java and the following table: (整数的运算,重点关注/和%,其他的无特殊)
Floating-point numbers. The double type is for representing floating-point numbers, e.g., for use in scientific applications. The internal representation is like scientific notation, so that we can compute with real numbers in a huge range. We can specify a floating point number using either a string of digits with a decimal point, e.g., 3.14159 for a six-digit approximation to the mathematical constant pi, or with a notation like scientific notation, e.g., 6.022E23 for Avogadro's constant 6.022 × 1023. Standard arithmetic operators for addition, multiplication, and division, for doubles are built in to Java, as illustrated in DoubleOps.java and the following table: (浮点数的运算规则,和C语言一致)
Quadratic.java shows the use of doubles in computing the two roots (根) of a quadratic (二次方程式) equation using the quadratic formula. (再次提醒:代码点开看懂、编译并运行)
Booleans. The boolean type has just two values: true or false. The apparent simplicity is deceiving—booleans lie at the foundation of computer science. The most important operators defined for the boolean are for and, or, and not. (与、或、非:和C语言一致,无特殊)
and: a && b is true if both a and b are true, and false otherwise.
or: a || b is true if either a or b is true (or both are true), and false otherwise
not: !a is true if a is false, and false otherwise.
Although these definitions are intuitive (直观的) and easy to understand, it is worthwhile to fully specify each possibility for each operation in a truth table.
Comparisons. The comparison operators are mixed-type operations that take operands of one type (e.g., int or double) and produce a result of type boolean. These operations play a critical role in the process of developing more sophisticated(复杂的) programs.
LeapYear.java tests whether an integer corresponds to a leap year in the Gregorian calendar(公历年). (计算闰年:四年一闰、百年不闰、四百年再闰)
Library methods and APIs.
Many programming tasks involve using Java library methods in addition to the built-in operators. An application programming interface is a table summarizing the methods in a library. (API就是一系列的方法接口)
Printing strings to the terminal window. (打印)
Converting strings to primitive types. (转换字符串为int,double,long)
Mathematical functions. (数学函数)
You can call a method by typing its name followed by arguments, enclosed in parentheses(圆括号) and separated by commas. Here are some examples: (调用示例)
Type conversion.
We often find ourselves converting data from one type to another using one of the following approaches.
Explicit type conversion(显式类型转换). Call methods such as Math.round(), Integer.parseInt(), and Double.parseDouble().
Automatic type conversion(自动类型转换). For primitive numeric types, the system automatically performs type conversion when we use a value whose type has a larger range of values than expected.
Explicit casts(显式强制转换). Java also has some built-in type conversion methods for primitive types that you can use when you are aware that you might lose information, but you have to make your intention using something called a cast. RandomInt.java reads an integer command-line argument n and prints a "random" integer between 0 and n−1.
Automatic conversions for strings(自动转换). The built-in type String obeys special rules. One of these special rules is that you can easily convert any type of data to a String by using the + operator. (用加法运算转数字为字符串)









