java运算符的使用
运算符

二元运算符
public class Demo01 {
public static void main(String[] args) {
//二元运算符
//ctrl+D :复制当前行到下一行; IDEA中快捷键
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/(double)b);
}
}
运算的转换
public class Demo02 {
public static void main(String[] args) {
long a = 212121212L;
int b = 1234;
short c = 123;
byte d = 10;
float e = 10;
System.out.println( a+b+c+d); //Long
System.out.println(b+c+d); //Int
System.out.println(c+d); //Int 自动升形态 String(c+d)
System.out.println(e+d); //Float
}
}
关系运算符
public class Demo03 {
public static void main(String[] args) {
//关系运算符返回结果:正确 错误 布尔值
int a = 10;
int b = 20;
int c = 21;
System.out.println(c%a); //取余,也叫模运算 1
System.out.println(a>b);
System.out.println(a<b);
System.out.println(a==b);
System.out.println(a!=b);
}
}
++--运算
import jdk.nashorn.internal.ir.CallNode;
public class Demo04 {
public static void main(String[] args) {
//++ -- 自增 自减 一元运算符
int a = 3;
int b = a++;//先赋值,在运算
//a = a+1;
System.out.println(a);//4
//a = a+1;
int c = ++a;//先运算,在赋值
System.out.println(a);//5
System.out.println(b);//3
System.out.println(c);//5
//幂运算 2^3 2*2*2=8 很多运算,我们会使用一些工具类来操作
double pow = Math.pow(2, 3); //Math.pow(2,3).var
System.out.println(pow);
}
}
数值较大时候的溢出问题和数据转换!!!
public class Dome05 {
public static void main(String[] args) {
//操作比较大的数时,注意溢出问题
// JKD7 新特性,数字之间可以用下划线
int money = 10_0000_0000;
System.out.println(money);
int year = 20;
int total = year * money; //-1474836480 ,计算的时候已经溢出了
long total2 = year * money; //默认值是int,转换之前已经存在问题了?
//String(year * money) 已经为int类型 或者((long)year*money)强制在结果时转Long 结果才为200---
long total3 = year * ((long) money);//先把一个数转化位Long
System.out.println(total); //-1474836480
System.out.println(total2); //-1474836480
System.out.println(total3); //20000000000
}
逻辑运算符和短路运算
//逻辑运算符
public class Demo05 {
public static void main(String[] args) {
// 与 或 非
boolean a = true;
boolean b = false;
System.out.println("a && b:"+(a&&b));
System.out.println("a || b:"+(a||b));
System.out.println("!(a && b):"+!(a&&b));
//短路运算
int c = 5;
boolean d = (c<4)&&(c++<4);//第一个为假,没有执行后面C++
System.out.println(d);
System.out.println(c);
}
}
位运算(计算机初始运算)
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 异或^
~B = 1111 0010 取反~
2*8 = 16 2*2*2*2
位运算效率极高!!!
<<左移
>>右移
0000 0000 0
0000 0001 1
0000 0010 2
0000 0100 8
0001 0000 16
*/
System.out.println(2<<3); //16
}
}
字符串连接符 +
public class Dome07 {
public static void main(String[] args) {
//字符串连接符 +
int a = 10;
int b = 20;
System.out.println(a+b);
System.out.println(""+a+b); //当连接有String类型,都会转换成String类型如何连接 1020
System.out.println(a+b+"t"); //字符串在后面,前面依旧进行运算,如何连接
}
}
三元运算符!!
//三元运算符
public class Demo08 {
public static void main(String[] args) {
// x ? y : z
//如果x==ture,结果为y,否则结果为z
public class Demo08 {
public static void main(String[] args) {
// x ? y : z
//如果x==ture,结果为y,否则结果为z
int score = 80;
String type = score <60?"不及格":"及格"; //必须掌握!!!
//if
System.out.println(type);
String type = score <60?"不及格":"及格"; //必须掌握!!!
//if
System.out.println(type);
}
}
}
浙公网安备 33010602011771号