public class Demo5 {
public static void main(String[] args) {
int i = 128;
byte b = (byte)i; //内存溢出,循环
System.out.println(b);
System.out.println(i);
}
}
//b=-128
不能转换转换布尔值
类型要相干
大容量到低容量要强制转换
转换可能存在内存溢出或精度问题
char c = 'a';
int d = c+1;
System.out.println(d);//98
System.out.println((char) d);//b
public class Demo6 {
public static void main(String[] args) {
int money = 10_0000_0000;//JDK7以后数字见可用_分割,且不会输出
int year = 20;
int total1 = money*year;//int类型超过21亿溢出
System.out.println(total1);
long total2 = money* year;
System.out.println(total2);//乘法运算时已经出问题,再转换没用
long total3 = money*(long)year;
System.out.println(total3);//把其中一个转换成long
}
}
package Operator;
public class Demo01 {
public static void main(String[] args) {
//二元运算符
int a = 10;//快捷键CTRL+D复制当前行到下一行
int b = 20;
int c = 30;
int d = 30;
System.out.println(a+b);
System.out.println(a-b);
System.out.println(a*b);
System.out.println(a/b); //int
System.out.println((double)a/b);
}
}
package Operator;
public class Demo02 {
public static void main(String[] args) {
long a = 123123123123L;
int b = 123;
short c = 32767;
byte d = 127;
System.out.println(a+b+c+d);//Long
System.out.println(b+c+d);//int
System.out.println(c+d);//int
//有一个是Long,全是long,否则全是int。
}
}
package Operator;
public class Demo03 {
public static void main(String[] args) {
//大于小于返回结果是布尔值,true/false
int a = 10;
int b = 20;
int c = 21;
System.out.println(a>b); //false
System.out.println(a<b); //true
System.out.println(a==b); //false
System.out.println(a!=b); //true
System.out.println(c%a); //取模=1
}
}
自增自减(++ --)
a++ 相当于隐藏语句a=a+1 在a++所在语句执行后执行 b=a++//先赋值,再自增
++a 相当于隐藏语句a=a+1 在++a所在语句执行前执行 b=++a//先自增,再赋值
– 用法类似
幂运算
数学计算类库 Math
Math.pow
package Operator;
public class Dmeo04 {
public static void main(String[] args) {
//自增自减
int a = 3;
int b = a++;//a = 3, b=3;
//a=a+1;隐藏
System.out.println(a);//a=4;
System.out.println(b);//b=3;
//a=a+1;
int c = ++a; //a=5,c=5;
System.out.println(c);
int d = a--;//d=5
System.out.println(d);//a=4
int e = --a;//a=3,e=3;
System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println(d);
System.out.println(e);
//幂运算
double pow = Math.pow(2,3);
System.out.println(pow);
}
}
逻辑运算
与&&,两个都为T,则为T;
或||,一个为T,则为T;
非!,T变为F。
短路运算
与运算a&&b,若a为false,则b直接跳过,程序返回false。
package Operator;
//逻辑运算符 与或非 交并补
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);
System.out.println(d);
System.out.println(c);
}
}
位运算
//每位进行与或非
A = 0011 1100
B = 0000 1101
A&B=0000 1100
A|B=0011 1101
A^B=0011 0001 //异或,相同为0,不同为1
~B=1111 0010
package Operator;
public class Demo07 {
public static void main(String[] args) {
int a = 10;
int b = 20;
a+=b;//a=a+b;
System.out.println(a);//30
//a=30;b=20;
System.out.println(""+a+b);//3020
System.out.println(a+b+"");//50
}
}
条件运算符
x ? y : z
若x位true,则值为y,否则为z。
package Operator;
public class Demo08 {
public static void main(String[] args) {
//条件运算符
int y = 1;
int z = 2;
boolean x = true;
System.out.package Operator;
public class Demo08 {
public static void main(String[] args) {
//条件运算符
int score = 85;
String type = score > 60 ? "合格" : "不合格";
System.out.println(type);//合格
}
}