目录
一、一些简单的 Java 程序
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int x = 7;
System.out.println(x);
}
}
class ComputeArea {
public static void main(String[] args) {
double radius = 20;
double area;
area = radius * radius * 3.14159;
System.out.println("The area of the circle is " + area + ".");
}
}
class Input {
public static void main(String[] args) {
Scanner input = new Scanner(System.in); // 从控制台输入
// 信息放入 System.in 静态变量中
int anInt = input.nextInt();
System.out.println("anInt = " + anInt);
System.out.println("aDouble = " + input.nextDouble());
}
}
二、标识符、变量、常量
→ 标志符:由字母、数组、下划线、美元符组成
【注意】标志符不能是保留字,不能以数字开头
→ 变量:具有类型的标志符
【注意】变量的值可以变化,需要先声明后使用,在函数中要先赋初值
int x = 7;
System.out.println(x);
→ 常量:使用 final 关键字定义,有类型,右侧不能是变量表达式,用等号赋值
【注意】具有特定的含义,有利于程序的修改和维护
class TestConstantValue {
public static void main(String[] args) {
final int CONSTANT_VALUE = 23;
System.out.println(CONSTANT_VALUE);
// CONSTANT_VALUE = 24; [错误]
final char CONSTANT = 'x';
int x = 2, y = 3;
final int VALUE = x + y;
}
}
三、数值数据类型与直接量
1. 数值数据类型
① byte:8位带符号数
② short:16位带符号数
③ int:32位带符号数
④ long:64位带符号数
⑤ float:32位带符号数
⑥ double:64位带符号数
【注意】以上的数据类型都是带符号的,并且按照 range 非降序排列
System.out.println(5 / 2);
System.out.println(5 / 2.0);
System.out.println(5.0 / 2);
System.out.println(-5 % 2);
System.out.println(5 % -2);
System.out.println(-5 % -2);
2. 数值型直接量
int i = 34;
long x = 1000000;
double d = 5.0;
// 数值型直接量
→ 如果整型直接量与整型变量相匹配,就可以将整型直接量赋值给该整型变量
【注意】如果直接量太大,超出该变量的存储范围,就会出现编译错误
→ 整型直接量默认为是int型的,取值范围在 -2^31 与 2^31 - 1 之间
→ 为了表示一个 long 型的整型直接量,需要在其后追加字母 L 或 l
long a = 2147483648L;
long b = 2147483648; // [错误] 应该在直接量后加上 L
int c = 2147483648; // [错误] 直接量太大,导致溢出
→ 浮点型直接量带小数点,默认情况下是 double 型
float d = 5.0; // [错误] 5.0 是 double 型直接量
float e = 5.0f;
double f = 5.0;
double g = 5.0f;
【注意】double 型比 float 型更精确
→ 科学计数法:浮点型直接量也可以用科学记数法表示,借助字母 E 或 e 表示指数
四、运算符与表达式等
【注意】/ 运算后得到的数据的精度以及 % 号运算后的符号
【实验】钟表小程序
class Clock {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter an integer for seconds: ");
int seconds = scanner.nextInt();
int minutes = seconds / 60;
int remainingSeconds = seconds % 60;
System.out.println(seconds + " second(s) is " + minutes + " minute(s) and " + remainingSeconds + " second(s)");
}
}
→ 幂运算
class Pow {
public static void main(String[] args) {
System.out.println(Math.pow(4, 0.5));
System.out.println(Math.pow(2.5, -2));
}
}
→ 运算符与优先级与 C++ 类似;前置++与后置++与 C++ 类似
五、数值类型转换
1. 转换规则
① 如果其中一个操作数为 double 型,另外一个被转换成 double 型
② 否则,如果其中一个操作数为 float 型,另外一个被转换成 float 型
③ 否则,如果其中一个操作数为 long 型,另外一个被转换成 long 型
④ 否则,两个数都被转换成 int 型
System.out.println(1.1 + 2147483648L);
System.out.println(2147483648L + 19);
System.out.println(1.2d + 21474214748383648L);
2. 强制类型转换
double d = 3; // 隐式转换(类型拓宽)
int i = (int) 3.9; // 显式转换(类型缩窄,小数部分被截去)
int j = 5 / 2.0; // [错误]
int k = 5.1f; // [错误]
【注意】简捷赋值表达式中的强制类型转换
→ 在 Java 中,x op= y 形式的简捷赋值表达式,执行为 x = (T) (x op y),这里 T 是 x 的类型
int sum = 1;
sum += 4.5;
System.out.println(sum);
sum = 1;
sum = (int) (sum + 4.5);
System.out.println(sum);
六、常见错误以及陷阱
1. 未声明、未初始化的变量和未使用的变量
int x;
System.out.println(x); // 编译错误
2. 整数溢出
int val = 1 << 31; // 整数溢出
System.out.println(val); // 输出 -2147483648
3. 取整错误
System.out.println(350 * 1.4); // 输出 489.99999999999994
4. 超出预期的整数除法
int a = 1;
int b = 2;
double c = (a + b) / 2; // [错误] 为了精确 应写成 2.0
System.out.println(c);
5. 简捷赋值表达式中的强制类型转换