常量final= VALUE:datatypeCONSTANTNAMEfinaldouble PI = 3.14159;finalint.SIZE =3 ;Liang,Introduction to Java Programming,EighthEdition,(c)2011Pearson Education,Inc.All16rightsreserved._0132130807
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 16 常量 final datatype CONSTANTNAME = VALUE; final double PI = 3.14159; final int SIZE = 3;
数值型数据类型NameRangestorage Sizeto 27-1(-128)(127)byte8-bit signed(-32768) to 215-1 (32767)short16-bit signed231(-2147483648) to 231-1 (2147483647) 32-bit signedint-263 to 263-1long64-bit signedi.e.-92233720368547758089223372036854775807)tofloat32-bitIEEE754Negative range:-3.4028235E+38to-1.4E-45Positive range:1.4E-45t03.4028235E+38double64-bitIEEE754Negative range:-1.7976931348623157E+308t0-4.9E-324Positive range:4.9E-324t01.7976931348623157E+308Liang,Introduction to Java Programming,EighthEdition,(c)2011Pearson Education,Inc.All17rights reserved.0132130807
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 17 数值型数据类型 Name Range Storage Size byte –2 7 (-128) to 27–1 (127) 8-bit signed short –2 15 (-32768) to 215–1 (32767) 16-bit signed int –2 31 (-2147483648) to 231–1 (2147483647) 32-bit signed long –2 63 to 263–1 64-bit signed (i.e., -9223372036854775808 to 9223372036854775807) float Negative range: 32-bit IEEE 754 -3.4028235E+38 to -1.4E-45 Positive range: 1.4E-45 to 3.4028235E+38 double Negative range: 64-bit IEEE 754 -1.7976931348623157E+308 to -4.9E-324 Positive range: 4.9E-324 to 1.7976931348623157E+308
数值运算符NameMeaningExampleResult35+Addition34+133.934.0-0.1Subtraction**300309000Multiplication//2.00.51.0Division2%20%3RemainderLiang,Introduction toJavaProgramming,EighthEdition,(c)2011PearsonEducation,Inc.All18rightsreserved.0132130807
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 18 数值运算符 Name Meaning Example Result + Addition 34 + 1 35 - Subtraction 34.0 – 0.1 33.9 * Multiplication 300 * 30 9000 / Division 1.0 / 2.0 0.5 % Remainder 20 % 3 2
整数除法-、*、/以及%5/2的结果是整数25.0/2的结果是double型值2.5(除法的余数5 % 2 的结果是1Liang,Introduction to Java Programming,EighthEdition,(c)2011Pearson Education,Inc.All19rights reserved.0132130807
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 19 整数除法 +、-、 * 、/以及% 5 / 2 的结果是整数 2 5.0 / 2 的结果是double型值2.5 5 % 2 的结果是1 (除法的余数)
余数运算符余数运算在程序设计中非常有用。例如:一个偶数%2结果总是0而奇数%2结果总是1。所以你可以使用这个特性来判定一个数是偶数还是奇数。假设今天是星期六,你和你的朋友们准备10天后在车站见面。那么10天后是星期几呢?你可以使用下面的表达式发现那一天是星期二Saturday is the 6thday in a weekAweekhas7days(6 + 10)is12nd day in a week is TuesdayTheAfter io daysLiang,Introduction to Java Programming,EighthEdition,(c)2011Pearson Education,Inc.All20rightsreserved.0132130807
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 20 余数运算符 余数运算在程序设计中非常有用。例如:一个 偶数%2结果总是0而奇数%2结果总是1。所以你 可以使用这个特性来判定一个数是偶数还是奇 数。假设今天是星期六,你和你的朋友们准备 10天后在车站见面。那么10天后是星期几呢? 你可以使用下面的表达式发现那一天是星期二: Saturday is the 6th day in a week A week has 7 days After 10 days The 2nd day in a week is Tuesday (6 + 10) % 7 is 2