《数据类型及运算符》
目录
一、Java基本元素
标识符
释义:标识符是赋给类、方法或变量的名字。一个标识符可以是字母、数字、下划线、美元符号的任意顺序组合,但不能以一个数字开始。
-
合法标识符
user1、$much、it_is_good、Alice
-
非法标识符
2a、good-luck、ab/cd
命名规范
- 驼峰式命名:userName,userAge,userPhoneNumber
- 项目名首字母大写
- 包名全部小写
- 类名首字母大写
- 变量、方法名开头字母小写
- 常量所有字母大写
字符集
java使用Unicode标准字符集,最多可以识别65536个字符,Unicode字符集的前128个字符是ASCII码表。
保留关键字(具有专门的意义和用途,不能当作一般的标识符使用)
释义:具有专门的意义和用途,不能当作一般的标识符使用,这些标识符称为保留关键字(reserved word)。
- 关键字
abstract break byte boolean catch case class char const continue default double do else extends false final float for finally if import implements int interface instanceof long native new null package private protected public return super switch synchronized short static try true this throw throws transient void while
- 保留词(无含义但不能使用)
goto, const
二、Java数据类型
Java语言是强类型语言
- 每个变量每个表达式都有类型
- 参数传递都要进行类型相容性检查
分类
-
基本数据类型(byte、short、int、long、float、double、boolean、char)
-
复合数据类型(数组、类类型、接口类型、枚举类型等等)
八大基本数据类型
数据类型 | 类型名称 | 字节数 | 取值范围 |
---|---|---|---|
byte | 字节型 | 1 | -27~27-1 |
short | 短整型 | 2 | -215~215-1 |
int | 整型 | 4 | -231~231-1 |
long | 长整型 | 8 | -263~263-1 |
float | 浮点型(单精度) | 4 | -3.4E+38~3.4E+38 |
double | 小数型(双精度) | 8 | -1.7E-308~1.7E+308 |
boolean | 布尔型 | 1 | true/false |
char | 字符型 | 2 | 0 ~65535 |
代码示例:
public class Main {
public static void main(String[] args) {
byte a = 1;
short b = 12;
int c = 123;
long d = 1234;
float e = 1.23f;
double f = 3.14;
boolean g = true;
char h = '学';
}
}
混合运算优先级(低——>高:byte, short, char > int > long > float > double)
- 隐式转换(低——>高)
long i = 123;
float j = x;
- 强制类型转换(高——>低)
long i = 123;
int k = (int) i;
- 表达式类型转换(以最高精度为准)
byte值 + short值 + int值 = int值;
int值 + float值 + double值 = double值;
三、运算符
赋值运算符(=)
int x = 10;
int y = 3;
int z = x + y;
算数运算符(+,-,*,/,%,++,–)
- 二元运算符(两个操作变量)
int x = 10;
int y = 3;
System.out.println("x+y=" + (x + y)); //x+y=13
System.out.println("x-y=" + (x - y)); //x-y=7
System.out.println("x*y=" + (x * y)); //x*y=30
System.out.println("x/y=" + (x / y)); //x/y=3
System.out.println("x%y=" + (x % y)); //x%y=1
- 一元运算符(一个操作变量)
int x = 10;
int y = 3;
System.out.println("x=" + (x++)); //x=10
System.out.println("x=" + x); //x=11
System.out.println("y=" + (++y)); //y=4
System.out.println("y=" + y); //y=4
比较运算符(>,<,>=,<=,==,!=)
int x = 10;
int y = 3;
System.out.println(x > y); //true
System.out.println(x < y); //false
System.out.println(x >= y); //true
System.out.println(x <= y); //false
System.out.println(x == y); //false
System.out.println(x != y); //true
逻辑运算符(&,|,!,^,&&,||)
System.out.println(3 > 2 & 2 < 1); //false
System.out.println(3 > 2 | 2 < 1); //true
System.out.println(!(3 > 2)); //false
System.out.println(3 > 2 ^ 2 < 1); //true
- 短路与 &&:遇false停,不判断下一表达式;
- 短路或 || :遇true停,不判断下一表达式。
复合赋值运算符(+=,-=,/=,%=)
运算符 | 表达式 | 计算 | 结果(假设 X=10) |
---|---|---|---|
+= | X += 5 | X = X + 5 | 15 |
-= | X -= 5 | X = X - 5 | 5 |
*= | X *= 5 | X = X * 5 | 50 |
/= | X /= 5 | X = X / 5 | 2 |
%= | X %= 5 | X = X % 5 | 0 |
注意:复合赋值运算为“右结合”,即从右往左依次计算。
int a1 = 10;
int a2 = 5;
int a3 = 2;
System.out.println(a1 += a2 -= a3 *= 3); //9
位运算符(>>,<<)
System.out.println(10 >> 2); //2
System.out.println(10 << 2); //40
运算符优先级
算术运算符 > 比较运算符 > 逻辑运算符 > 赋值运算符。
总结
重点
- 合法标识符书写规则;
- ASCII码英文字母值范围;
- 运算符及结合顺序优先级。
难点
- 运算符及结合顺序优先级。