基本运算符,自增自减运算符,初识Math类
instance of 也是运算符,面向对象 时再说
package operator;
public class Demo01 {
public static void main(String[] args) {
//二元运算符
//Ctrl + D : 复制当前行到下一行
int a = 10;
int b = 20;
int c = 30;
int d = 40;
System.out.println(a+b);
System.out.println(a-b);
System.out.println(a*b);
System.out.println(a/b);
System.out.println(a/(double)b);
}
}
"D:\IDEA java\IntelliJ IDEA 2019.3.5\jbr\bin\java.exe" "-javaagent:D:\IDEA java\IntelliJ IDEA 2019.3.5\lib\idea_rt.jar=57045:D:\IDEA java\IntelliJ IDEA 2019.3.5\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\L12\Desktop\Note\Demo1\out\production\Demo1 operator.Demo01
30
-10
200
0
0.5
Process finished with exit code 0
不能把 int 类型转换为String,说明(c+d)也是int类型
package operator;
public class Demo02 {
public static void main(String[] args) {
long a = 123123123123123L;
int b = 123;
short c = 10;
byte d = 8;
//如果多个操作数中,有一个数为long,那么这个结果类型也为long
// 如果多个操作数中,有一个数为double,那么这个结果类型也为double
//如果没有long的时候,结果都为int
System.out.println(a+b+c+d);//Long
System.out.println(b+c+d);//Int
System.out.println(c+d);//Int
System.out.println((double)c+d);
//System.out.println((string)(c+d));
}
}
"D:\IDEA java\IntelliJ IDEA 2019.3.5\jbr\bin\java.exe" "-javaagent:D:\IDEA java\IntelliJ IDEA 2019.3.5\lib\idea_rt.jar=56218:D:\IDEA java\IntelliJ IDEA 2019.3.5\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\L12\Desktop\Note\Demo1\out\production\Demo1 operator.Demo02
123123123123264
141
18
18.0
Process finished with exit code 0
package operator;
public class Demo03 {
public static void main(String[] args) {
//关系运算符返回的结果:正确,错误 布尔值
//会大量的和 if 使用
int a = 10;
int b = 20;
int c = 21;
//取余,模运算
System.out.println(c%a);//c/a 21/10=2...1
System.out.println(a>b);
System.out.println(a<b);
System.out.println(a==b);
System.out.println(a!=b);
}
}
"D:\IDEA java\IntelliJ IDEA 2019.3.5\jbr\bin\java.exe" "-javaagent:D:\IDEA java\IntelliJ IDEA 2019.3.5\lib\idea_rt.jar=60751:D:\IDEA java\IntelliJ IDEA 2019.3.5\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\L12\Desktop\Note\Demo1\out\production\Demo1 operator.Demo03
1
false
true
false
true
Process finished with exit code 0
自增,自减运算符 重点会用在循环
快捷键
package operator;
public class Demo04 {
public static void main(String[] args) {
//++ -- 自增,自减 一元运算符
int a = 3;
int b = a++;//执行完这行代码后,先给b赋值,再自增
//a=a+1
System.out.println(a);//说明a++执行完的下一句,a才会加1
//a=a+1
int c = ++a;//++a a=a+1//执行完这行代码前,先自增,再给c赋值
System.out.println(a);
System.out.println(b);
System.out.println(c);
/* for (int i = 0; i < ; i++) {
}
a++ + ++b
*/
//运算符扩展 初识Math类
//幂运算2^3 2*2*2 = 8 借助一些工具类 很多运算,我们会使用一些工具类来操作!
double pow = Math.pow(2, 3);
System.out.println(pow);
}
}
"D:\IDEA java\IntelliJ IDEA 2019.3.5\jbr\bin\java.exe" "-javaagent:D:\IDEA java\IntelliJ IDEA 2019.3.5\lib\idea_rt.jar=61179:D:\IDEA java\IntelliJ IDEA 2019.3.5\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\L12\Desktop\Note\Demo1\out\production\Demo1 operator.Demo04
4
5
3
5
8.0
Process finished with exit code 0