运算符
1. 算术运算符
算术运算符用于执行基本的数学运算,像加、减、乘、除等。
运算符 | 描述 | 示例 |
---|---|---|
+ | 加法 | int a = 5 + 3; // a 的值为 8 |
- | 减法 | int b = 5 - 3; // b 的值为 2 |
* | 乘法 | int c = 5 * 3; // c 的值为 15 |
/ | 除法 | int d = 6 / 3; // d 的值为 2 |
% | 取模(取余) | int e = 7 % 3; // e 的值为 1 |
++ | 自增 | int f = 5; f++; // f 的值为 6 |
-- | 自减 | int g = 5; g--; // g 的值为 4 |
2. 赋值运算符
赋值运算符用于给变量赋值。
运算符 | 描述 | 示例 |
---|---|---|
= | 简单赋值 | int a = 5; // a 的值为 5 |
+= | 加后赋值 | int b = 5; b += 3; // b 的值为 8 |
-= | 减后赋值 | int c = 5; c -= 3; // c 的值为 2 |
*= | 乘后赋值 | int d = 5; d *= 3; // d 的值为 15 |
/= | 除后赋值 | int e = 6; e /= 3; // e 的值为 2 |
%= | 取模后赋值 | int f = 7; f %= 3; // f 的值为 1 |
3. 比较运算符
比较运算符用于比较两个值,返回布尔类型(true
或 false
)。
运算符 | 描述 | 示例 |
---|---|---|
== | 等于 | int a = 5; int b = 5; boolean c = (a == b); // c 的值为 true |
!= | 不等于 | int d = 5; int e = 3; boolean f = (d != e); // f 的值为 true |
> | 大于 | int g = 5; int h = 3; boolean i = (g > h); // i 的值为 true |
< | 小于 | int j = 5; int k = 7; boolean l = (j < k); // l 的值为 true |
>= | 大于等于 | int m = 5; int n = 5; boolean o = (m >= n); // o 的值为 true |
<= | 小于等于 | int p = 5; int q = 7; boolean r = (p <= q); // r 的值为 true |
4. 逻辑运算符
逻辑运算符用于组合布尔表达式。
运算符 | 描述 | 示例 |
---|---|---|
&& | 逻辑与 | boolean a = true; boolean b = false; boolean c = (a && b); // c 的值为 false |
|| | 逻辑或 | boolean d = true; boolean e = false; boolean f = (d||e); //f 的值为 true` |
! | 逻辑非 | boolean g = true; boolean h = !g; // h 的值为 false |
5. 位运算符
位运算符用于对二进制位进行操作。
运算符 | 描述 | 示例 |
---|---|---|
& | 按位与 | int a = 5; int b = 3; int c = a & b; // c 的值为 1 |
| | 按位或 | `int d = 5; int e = 3; int f = d |
^ | 按位异或 | int g = 5; int h = 3; int i = g ^ h; // i 的值为 6 |
~ | 按位取反 | int j = 5; int k = ~j; // k 的值为 -6 |
<< | 左移 | int l = 5; int m = l << 1; // m 的值为 10 |
>> | 右移 | int n = 5; int o = n >> 1; // o 的值为 2 |
>>> | 无符号右移 | int p = -5; int q = p >>> 1; // q 的值为一个很大的正数 |
6. 三元运算符
三元运算符是一种简洁的条件判断语句。
条件表达式 ? 表达式1 : 表达式2;
int a = 5;
int b = 3;
int max = (a > b) ? a : b; // max 的值为 5
运算符优先级
运算符优先级决定了表达式中运算符的计算顺序。优先级高的运算符先计算,相同优先级的运算符按照从左到右的顺序计算。以下是 Java 运算符的优先级,从高到低排列:
- 后缀运算符:
() [] .
- 一元运算符:
++ -- + - ! ~
- 乘除模运算符:
* / %
- 加减运算符:
+ -
- 移位运算符:
<< >> >>>
- 关系运算符:
< <= > >= instanceof
- 相等运算符:
== !=
- 按位与运算符:
&
- 按位异或运算符:
^
- 按位或运算符:
|
- 逻辑与运算符:
&&
- 逻辑或运算符:
||
- 三元运算符:
? :
- 赋值运算符:
= += -= *= /= %= &= ^= |= <<= >>= >>>=
在编写表达式时,可以使用括号 ()
来明确指定计算顺序,提高代码的可读性。例如:
int result = (2 + 3) * 4; // 先计算括号内的加法,再计算乘法
+
运算符除了用于数值的加法运算外,还能用于字符串的连接操作
字符串连接基本规则
当 +
运算符的操作数中有一个是字符串类型时,Java 会将其他操作数转换为字符串,然后进行连接操作。具体规则如下:
1. 字符串与基本数据类型连接
当字符串与基本数据类型(如 int
、double
、boolean
等)使用 +
运算符时,基本数据类型会自动转换为字符串,然后与另一个字符串进行连接。
public class StringConcatenation {
public static void main(String[] args) {
// 字符串与 int 类型连接
int num = 10;
String str1 = "The number is: " + num;
System.out.println(str1);
// 字符串与 double 类型连接
double d = 3.14;
String str2 = "The value of pi is approximately: " + d;
System.out.println(str2);
// 字符串与 boolean 类型连接
boolean isTrue = true;
String str3 = "The condition is: " + isTrue;
System.out.println(str3);
}
}
在上述代码中,num
、d
和 isTrue
分别是 int
、double
和 boolean
类型,当它们与字符串使用 +
运算符时,会自动转换为字符串进行连接。
多个字符串与其他类型混合连接
+
运算符是从左到右依次进行计算的,当多个字符串和其他类型混合使用 +
运算符时,会按照顺序依次进行连接。
public class MultipleStringConcatenation {
public static void main(String[] args) {
int a = 5;
int b = 3;
String result = "The sum of " + a + " and " + b + " is: " + (a + b);
System.out.println(result);
}
}
在上述代码中,首先将 "The sum of "
与 a
转换后的字符串连接,然后依次连接后续的字符串和变量。需要注意的是,(a + b)
用括号括起来是为了先进行数值加法运算,再将结果转换为字符串进行连接。如果没有括号,a
会先与前面的字符串连接,而不是先进行加法运算。
字符串连接中的类型转换顺序
当表达式中包含多个 +
运算符时,会按照从左到右的顺序进行计算。一旦遇到字符串类型的操作数,后续的操作数都会被转换为字符串进行连接。
public class ConversionOrder {
public static void main(String[] args) {
int x = 1;
int y = 2;
String s = "Result: ";
System.out.println(s + x + y); // 先将 s 与 x 转换后的字符串连接,再与 y 转换后的字符串连接
System.out.println(x + y + s); // 先进行 x + y 的数值加法运算,再将结果与 s 连接
}
}
在上述代码中,第一个 println
语句先将 s
与 x
转换后的字符串连接,再与 y
转换后的字符串连接,结果为 "Result: 12"
;第二个 println
语句先进行 x + y
的数值加法运算,得到 3
,再将 3
转换为字符串与 s
连接,结果为 "3Result: "
。
综上所述,+
运算符在 Java 中用于字符串连接时,会根据操作数的类型自动进行类型转换,按照从左到右的顺序依次进行连接操作。在编写代码时,需要注意运算符的优先级和类型转换顺序,以确保得到预期的结果。
自动类型转换
自动类型转换,也称为隐式类型转换,是指在某些情况下,Java 编译器会自动将一种数据类型转换为另一种数据类型,而无需程序员进行额外的操作。自动类型转换需要满足以下两个条件:
- 目标类型的范围大于源类型的范围:即目标类型能够容纳源类型的所有可能值。
- 数据类型兼容:两种数据类型必须是兼容的,例如数值类型之间可以进行自动类型转换。
以下是 常见的自动类型转换示例:
public class AutomaticTypeConversion {
public static void main(String[] args) {
// 1. 从 byte 到 short
byte byteValue = 10;
short shortValue = byteValue;
System.out.println("byte 转换为 short: " + shortValue);
// 2. 从 short 到 int
short anotherShortValue = 20;
int intValue = anotherShortValue;
System.out.println("short 转换为 int: " + intValue);
// 3. 从 int 到 long
int anotherIntValue = 30;
long longValue = anotherIntValue;
System.out.println("int 转换为 long: " + longValue);
// 4. 从 int 到 float
int yetAnotherIntValue = 40;
float floatValue = yetAnotherIntValue;
System.out.println("int 转换为 float: " + floatValue);
// 5. 从 long 到 double
long anotherLongValue = 50L;
double doubleValue = anotherLongValue;
System.out.println("long 转换为 double: " + doubleValue);
}
}
在上述代码中,分别展示了从 byte
到 short
、short
到 int
、int
到 long
、int
到 float
以及 long
到 double
的自动类型转换。由于目标类型的范围大于源类型的范围,编译器会自动进行转换。
强制类型转换
强制类型转换,也称为显式类型转换,是指当需要将一个范围大的数据类型转换为范围小的数据类型时,程序员需要手动进行类型转换。强制类型转换可能会导致数据丢失,因为目标类型可能无法容纳源类型的所有可能值。
目标类型 变量名 = (目标类型) 源类型变量;
public class ExplicitTypeConversion {
public static void main(String[] args) {
// 1. 从 double 到 int
double doubleValue = 3.14;
int intValue = (int) doubleValue;
System.out.println("double 转换为 int: " + intValue);
// 2. 从 int 到 byte
int intValue2 = 130;
byte byteValue = (byte) intValue2;
System.out.println("int 转换为 byte: " + byteValue);
// 3. 从 long 到 short
long longValue = 32768L;
short shortValue = (short) longValue;
System.out.println("long 转换为 short: " + shortValue);
}
}
在上述代码中,分别展示了从 double
到 int
、int
到 byte
以及 long
到 short
的强制类型转换。需要注意的是,强制类型转换可能会导致数据丢失,例如 double
类型的小数部分会被截断,int
或 long
类型的值可能会超出目标类型的范围而导致溢出。
综上所述,自动类型转换是编译器自动完成的,而强制类型转换需要程序员手动进行。在进行强制类型转换时,需要谨慎处理,避免数据丢失。
小于 int
类型的数据类型(如 byte
、short
和 char
)在进行运算时,通常都会自动转换为 int
类型。
原因
- 保证运算精度 :
byte
、short
和char
的取值范围相对较小,在运算过程中结果可能会超出它们的表示范围,转换为int
类型可以避免溢出问题,确保运算结果的准确性。 - 简化指令集 :Java 虚拟机(JVM)的指令集是为处理
int
类型设计的。将小于int
的数据类型转换为int
类型,可以统一处理整数运算,简化 JVM 的指令集和运算规则,提高运算效率。
示例代码
public class SmallerThanIntConversion {
public static void main(String[] args) {
// byte 类型运算
byte byte1 = 10;
byte byte2 = 20;
// byte1 和 byte2 会自动转换为 int 类型进行运算
int byteResult = byte1 + byte2;
System.out.println("byte 运算结果: " + byteResult);
// short 类型运算
short short1 = 30;
short short2 = 40;
// short1 和 short2 会自动转换为 int 类型进行运算
int shortResult = short1 + short2;
System.out.println("short 运算结果: " + shortResult);
// char 类型运算
char char1 = 'A';
char char2 = 'B';
// char1 和 char2 会自动转换为 int 类型进行运算
int charResult = char1 + char2;
System.out.println("char 运算结果: " + charResult);
}
}
代码解释
byte
类型运算 :byte1
和byte2
是byte
类型,但在执行byte1 + byte2
运算时,它们会自动转换为int
类型,最终结果也是int
类型。short
类型运算 :short1
和short2
是short
类型,在运算时同样会自动转换为int
类型,结果为int
类型。char
类型运算 :char
类型在 Java 中本质上是无符号的 16 位整数。char1
和char2
在运算时会自动转换为int
类型,结果也是int
类型。
如果要将运算结果赋值给小于 int
类型的变量,需要进行强制类型转换,但要注意可能会出现数据溢出的问题。