java基础-1 : 运算符
一.算数运算符
包括(+,-,*,/,%)
数据类型转换:
1.1隐式转换
范围小到范围大
- byte<short<int<long<float<double
- 范围小的与范围大的进行计算时,范围小的会自动提升为范围大的
- byte,short,char在进行计算时,会自动提升为int
(一)byte<short<int<long<float<double
1字节等于8比特位,也就是8个二进制位
byte:1字节,-128-127
short:2字节,
char:2字节
int:4字节
long:8字节
float:4字节
double:8字节
public class test {public static void main(String[] args) {
// byte1字节,short2字节,int4字节,long8字节,float4字节,double8字节,char2字节,boolean1字节
// 1字节等于8比特位,也就是8个二进制位int a = 200;//00000000 00000000 00000000 11001000int b = 10;//00000000 00000000 00000000 00001010System.out.println(a & b);//00000000 00000000 00000000 00001000=8 逻辑或:当上下都为1时结果为1,否则为0System.out.println(a | b);//00000000 00000000 00000000 11001010=206 逻辑与:当上下都为0时结果为0,否则为1
// 左移<< ,右移>>,无符号右移>>>System.out.println(a << 1);//00000000 00000000 00000001 1001000 0=400 左移一位,相当于乘2,右移两次,相当于除两次2}
}
(二)范围小的数据类型会变为范围大的数据类型
public class test {public static void main(String[] args) {int a = 10;double b = 20.0;System.out.println(a+b);}
}
30.0
(三)byte,short,char等在计算时会自动提升为int
public class test {public static void main(String[] args) {byte a = 10;byte b = 20;byte c = (byte)(a+b);System.out.println(c);}
}
注:因为byte在计算时会提升为int所以a+b为int类型,要接收byte需要进行强转
30
1.2强制转换
范围大的数据类型到范围小的数据类型时,可能会出现数据溢出问题
public class test {public static void main(String[] args) {int a = 300;byte b = (byte)a;System.out.println(b);}
}
注:数据出错,因为byte是-128-127,300超出了范围
44
2.+号
1.字符拼接
会先将字符变为ACSLL码,然后再进行计算
public class test {public static void main(String[] args) {char a = 'a';System.out.println(a);System.out.println(a+0);}
}
注:字符在进行计算时,会变为ACSLL码,a对应的ACSLL码为97
a
97
2.字符串拼接
从左到右依次拼接,当与字符串进行拼接时,会变为字符串
public class test {public static void main(String[] args) {String A = "123";String B = "123";System.out.println(A+B);System.out.println(3.7+"asb");System.out.println(1+1+"asb"+2);System.out.println("asv"+true);}
}
123123
3.7asb
2asb2
asvtrue
test
public class test {public static void main(String[] args) {
// 整数的乘除只会得到整数,要想得到小数,需要将其中一个变为小数System.out.println(10/3);System.out.println(10.0/3);System.out.println(10/3.0);
// 取模System.out.println(10%3);//1System.out.println(10%3.0);//1.0}
}
3
3.3333333333333335
3.3333333333333335
1
1.0
二.自增自减运算符
包括(++,--)
- 前置自增(++a):先执行自增操作,再使用变量值
- 后置自增(a++):先使用当前值,再执行自增操作
public class test {public static void main(String[] args) {int a = 10;int b = a++;int c = ++a;System.out.println(a);System.out.println(b);System.out.println(c);}
}
注:
1.因为在输出a时,已经经过了两次自增操作,所以a为10。
2.因为b是先执行后自增,所以b先赋值为10a再自增,所以b为10。
3.因为c为先自增后执行,所以a先自增为12再赋值给c,所以c为12。
12
10
12
三.赋值运算符
- 包含:
+=,
-=,
*=,
/=,
%=
- 自动强制转换机制:运算结果类型与左侧变量类型自动匹配
- 示例:
s += 2
实际执行(short)(s + 2)
public class test {public static void main(String[] args) {int a = 10;double b = 20.0;a+=b;System.out.println(a);}
}
注:因为a为int类型,所以在加double时,会进行强制转换
30
四.关系运算符
- 包含:
==,
!=,
>,
<,
>=,
<=
- 运算结果始终为boolean类型(true/false)
- 示例:
a > b
比较数值大小
public class test {public static void main(String[] args) {int a = 10;double b = 10.0;System.out.println(a==b);}
}
true
五.逻辑运算符
& 逻辑与(全真为真)
| 逻辑或(有真为真)
^ 异或(不同为真)
! 非(取反)
&& 短路与(左假即停)
|| 短路或(左真即停)
public class test {public static void main(String[] args) {int A = 10;int B = 20;boolean result1 = ++A < 5 && ++B < 10;System.out.println(result1);System.out.println(A);System.out.println(B);}
}
注:先自增后判断,左边为假,右边不判断了,所以b=20
false
11
20
六.三元表达式
三元表达式: 关系表达式?表达式1:表达式2; 当关系表达式为true时,返回表达式1,否则返回表达式2
public class test {public static void main(String[] args) {int a = 10;int b = 20;int max = a > b ? a : b;System.out.println(max);}
}
注:当a>b时,返回a,当a<b时,返回b
20
test
import java.util.Scanner;public class test {public static void main(String[] args){
//输入两个数字,当其中有一个为6时,输出True,当他们的和为6的倍数时,也为trueScanner sc = new Scanner(System.in);System.out.println("输入第一个数字");int a = sc.nextInt();System.out.println("输入第二个数字");int b = sc.nextInt();boolean result = a==6 || b==6 || (a+b)%6 ==0;System.out.println(result);
// 输入两个数字,当两个数字相同时,输出相同,否则输出不同boolean result1 = a==b;System.out.println(result1? "相同":"不同");}
}