Java学习之路--operator--运算符的使用

发布时间 2023-09-20 14:05:09作者: 寂灭无言
package com.chao.operator;

public class Demo01 {
public static void main(String[] args) {
//二元运算符
//Ctrl + D :复制当前行到下一行
int a = 10;
int b = 20;
int c = 30;


System.out.println(a+b);
System.out.println(a-b);
System.out.println(a*b);
System.out.println((double)a/b);
}
}

//
//
package com.chao.operator;

import com.chao.base.*;//导入base包里所有的类
import com.chao.base.Demo01;//导入base包里Demo01这个类
/*
在Idea中创建包时建立分级包结构
点击左边项目结构右上方的小齿轮,
取消勾选Compact Middle Packages选项,
此时你建立一个com.dong.package包,就会显示为com包下dong包,dong包下package包!
*/

public class Demo02 {
public static void main(String[] args) {
long a = 1231213131313131L;
int b = 123;
short c = 10;
byte d = 8;

System.out.println(a+b+c+d);//返回输出类型是Long
System.out.println(b+c+d);//返回输出类型是Int
System.out.println(c+d);//返回输出类型是Int
}
}

//
//
package com.chao.operator;

public class Demo03 {
public static void main(String[] args) {
//关系运算符返回的结果:正确、错误、布尔值
int a = 10;
int b = 20;
int c = 21;

System.out.println(c%b);//取余,输出结果1,在Java中叫模运算

System.out.println(a>b);//输出false
System.out.println(a<b);//输出true
System.out.println(a==b);//输出false
System.out.println(a!=b);//输出true
}
}

//
//
package com.chao.operator;

public class Demo04 {
public static void main(String[] args) {
//关系运算符:++(自增),--(自减) 一元运算符
int a = 3;

int b = a++;//先给b赋值,再自增
//a++即a = a + 1;
System.out.println(a);//输出4
System.out.println(b);//输出3

int c = ++a;//先自增,再给c赋值
System.out.println(a);//输出5
System.out.println(c);//输出5

//幂运算2^3 2*2*2 很多运算,使用一些工具类来操作
Double pow = Math.pow(3,2);//3的平方
System.out.println(pow);
}
}

//
//
package com.chao.operator;

public class Demo05 {
public static void main(String[] args) {
//逻辑运算符:与(and),或(or),非(取反)
boolean a = true;
boolean b = false;

System.out.println("a && b: " + (b&&a));//逻辑与运算,两个变量都为真,结果才为true,输出结果false
System.out.println("a || b: " + (a||b));//逻辑或运算,两个变量有一个为真,结果才为true,输出结果true
System.out.println("! (a&&b) : " + !(a&&b));//如果是真,则变为假,如果是假则变为真,输出结果true

//短路运算
int c = 5;
boolean d = (c<4)&&(c++<4);
System.out.println(d);//输出false
System.out.println(c);//输出是5
}
}

//
//
package com.chao.operator;

public class Demo06 {
public static void main(String[] args) {
//位运算
/*
A = 0011 1100
B = 0000 1101

A&B = 0000 1100
A|B = 0011 1101
A^B = 0011 0001
A~B = 1100 0010

<< *2,左移乘2
2<<3,二进制表示2中数字1向左移3位
0000 0000 0
0000 0001 1
0000 0010 2
0000 0011 3
0000 0100 4
0000 0101 5
0000 0110 6
0000 0111 7
0000 1000 8
0001 0000 16
>> /2,右移除2
*/
System.out.println(2<<3);//输出16,相当于2*2*2*2
}
}

//
//
package com.chao.operator;

public class Demo07 {
public static void main(String[] args) {
int a = 10;
int b = 20;
a += b;//相当于a = a+b
a -= b;//相当于a = a-b
System.out.println(a);

//字符串连接符 +(加号),看在什么位置,输出是否有字符串“”
System.out.println("" + a + b);//输出的是a拼接b的结果1020
System.out.println(a + b + "");//输出的是a+b的和30

}
}

//
//
package com.chao.operator;

public class Demo08 {
public static void main(String[] args) {
//三元运算符 x ? y : z
//如果x = true,则结果为y,否则结果为z

int score = 50;
int score1 = 70;
String type = score < 60 ? "不及格" : "及格";//成绩是50,判断50小于60(true),输出不及格
String type1 = score1 < 60 ? "不及格" : "及格";//成绩是70,判断70不小于60(false),输出及格
System.out.println(type);
System.out.println(type1);
}
}