2.1关键字:class,public, int 合法的:applet, Applet, $4, apps, x, y, radius 不合法的:a++, --a, 4#R, #442, class,public, int。
2.2
double miles = 100;
final double KILOMETERS_PER_MILE = 1.609;
double kilometers = KILOMETERS_PER_MILE * miles;
System.out.println(kilometers);
2.3使用常量有三个好处:(1)你不重复键入相同的值;(2)如果必要的话,值只需在一个地方改动(3)程序易于阅读。 final int SIZE = 20;
2.4
a = 46 / 9; => a = 5
a = 46 % 9 + 4 * 4 - 2; => a = 1 + 16 – 2 = 15
a = 45 + 43 % 5 * (23 * 3 % 2); => a = 45 + 3 * (1) = 48
a %= 3 / a + 3; => a %= 3 + 3; a % = 6 => a = a % 6 = 1;
d = 4 + d * d + 4; => 4 + 1.0 + 4 = 9.0
d += 1.5 * 3 + (++a); => d += 4.5 + 2; d += 6.5; => d = 7.5
d -= 1.5 * 3 + a++; => d -= 4.5 + 1; => d = 1 – 5.5 = -4.5
2.5 2, 2, -4, -4, 0, 1
2.6(2 + 100) % 7 = 4 所以是星期四。
2.7
byte, from -128 to 127
short, from -32768 to 32767
int, from -2147483648 to 2147483647
long, from -9223372036854775808 to 9223372036854775807.
float, the smallest positive float is 1.40129846432481707e-45 and the largest float is 3.40282346638528860e+38.
double, the smallest positive double is 4.94065645841246544e-324 and the largest double is 1.79769313486231570e+308d.
2.8 25 / 4 is 6 25.0 / 4.0, 25.0 / 4, or 25 / 4.0.
2.9 25 / 4 is 6 25 / 4.0 is 6.25 3 * 2 / 4 is 1 3.0 * 2 / 4 is 1.5
2.10 4.0 / (3.0 * (r + 34)) – 9 * (a + b * c) + (3.0 + d * (2 + a)) / (a + b * d)
2.11 1.0 * m * (r * r)
2.12 2和3 True
2.13 所有的都是
2.14 line2:main前缺少ststic,是String不是string, line3:没初始化就在line5使用了, line4:k是int型,不能用double型的值 lines 7-8: The string cannot be broken into two lines
2.15
1 public class ceshi { 2 public static void main(String[] args){ 3 long totalMills = System.currentTimeMillis(); 4 totalMills = totalMills / 1000 / 60 % 60; 5 System.out.println(totalMills); 6 } 7 }
2.16能在一起计算。
2.17double型的小数部分被截断,类型转换不会改变被转换变量的值。
2.18
f is 12.5
i is 12
2.19
System.out.println((int)'1');
System.out.println((int)'A');
System.out.println((int)'B');
System.out.println((int)'a');
System.out.println((int)'b');
System.out.println((char)40);
System.out.println((char)59);
System.out.println((char)79);
System.out.println((char)85);
System.out.println((char)90);
System.out.println((char)0X40);
System.out.println((char)0X5A);
System.out.println((char)0X71);
System.out.println((char)0X72);
System.out.println((char)0X7A);
2.20 '\ u345dE'是错误的。它必须有四个十六进制数字。
2.21
System.out.println('\\' );
System.out.println('\"');
2.22
49
99
97
Z
2.23
char c = 'A';
i = (int)c; // i becomes 65
float f = 1000.34f;
int i = (int)f; // i becomes 1000
double d = 1000.34;
int i = (int)d; // i becomes 1000
int i = 97;
char c = (char)i; // c becomes 'a'
2.24 b, c, -2
2.25
1 public class ceshi { 2 public static void main(String[] args){ 3 System.out.println("1" + 1); 4 System.out.println('1' + 1); 5 System.out.println("1" + 1 + 1); 6 System.out.println("1" + (1 + 1)); 7 System.out.println('1' + 1 + 1); 8 9 } 10 } 11 12 /*output: 13 11 14 50 15 111 16 12 17 51 18 */
2.26
1 public class ceshi { 2 public static void main(String[] args){ 3 System.out.println(1 + "welcome " + 1 + 1 ); 4 System.out.println(1 + "welcome " + (1 + 1)); 5 System.out.println(1 + "welcome " + ('\u0001' + 1)); 6 System.out.println(1 + "welcome " + 'a' + 1); 7 } 8 } 9 10 /* 11 output: 12 1welcome 11 13 1welcome 2 14 1welcome 2 15 1welcome a1 16 */
2.27
类名:每个名字的首字母大写。Test
变量名和方法名:小写字母的第一个字,在随后的所有词的第一个字母大写。read, readInt
常量:大写所有字母。MAX_VALUE
2.28
1 public class Test { 2 /** Main method */ 3 public static void main(String[] args) { 4 // Print a line 5 System.out.println("2 % 3 = " + 2 % 3); 6 } 7 }
2.29 Compilation errors are detected by compilers. Runtime errors occur during execution of the program. Logic errors results in incorrect results.
2.30 Math class 已经在java.lang package中,任何在java.lang package中的类都直接被导入,所有不用再导入。
2.31
1 public class Test { 2 public static void main(String[] args){ 3 String s = JOptionPane.showInputDialog("请输入一个整数"); 4 int i = Integer.parseInt(s); 5 int output = i; 6 JOptionPane.showMessageDialog(null, output); 7 } 8 }
2.32
30. int i = Integer.parseInt(s);
double s = Double.parseDouble(s);