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);//合格
}
}