閱讀目錄:java
class Demo01 { public static void main(String[] args) { //算術運算符。 + - * / %(取餘,模運算,若是左邊小於右邊就獲得左邊的數:2%5=2,3%5=3) // ++ (自增:在原有數據基礎上+1,再賦給原有的數據。)
int x = 6370; x = x / 1000 * 1000; System.out.println(x);
System.out.println(3+"2"); // 32 不是32,是3和2連一塊兒。
int a = 4, b=5;
System.out.println("a="+a+",b="+b); //a=4.b=5
int a = 3;
a++; //a = a+1;
System.out.println("a="+a);
} }
注意:spa
1.JAVA是的強烈型語言code
2.任何數據和字符串相加,得出的結果都是相連。blog
3.運算中,通常地變量會先參與其餘的運算,在運行自身自增的動做。內存
內存原理:"把右邊運算完之後,在賦值給右邊"字符串
分析: 開始a=3,它會先創建一個a=3的空間,它會先參與其它的運算,a++=3+1=4, 自身a=4, 以後再把原來a=3的內容值3賦值給右邊b,因此b=3. string
class Demo02 { public static void main(String[] args) { //賦值運算符, = += *= /= %= //int a,b,c // a = b = c =4; //int a = 4; //a +=2; // a = a + 2; short s = 3; // short 自己可存 2個字節,賦值運算,s要進行檢查 // s += 4; //成功,它是一次運算,+=動做時候它在裏面作了強轉,即:自動轉換。 //s = (short) (s+4) 這個能夠表明: s = s+4 . s = s + 4; //失敗 它是兩次運算,由於s是不肯定的值,因此檢查不出來。失敗 System.out.println("s+="+s) //分析: //1個編譯失敗的緣由:由於實現自動類型的提高,因此類型已經不能賦給低空間的類型,會丟失精度 //1個編譯成功的緣由:由於它裏面作了類型的強轉。 } }
class Demo03 { public static void main(string[] args) { /* 比較運算符,運算完的結果必須是true或者false */ System.out.println(3>2); //true System.out.println(3==2); //false } }
class Demo03 { public static void main(string[] args) { /* 比較運算符,運算完的結果必須是true或者false */ //System.out.println(3>2); //true //System.out.println(3==2); //false //2<x<5 java中這種形式要拆分: x>2 x<5 /* 邏輯運算符有什麼用? 答: 用於鏈接兩個布爾型的表達式 & : 與 | : 或 &:符號的運算特色: true & true = true; true & false = false; false & true = false; false & false = false; &:運算規律: &運算的兩邊只有一個false,結果確定是false, 只有兩邊都爲true,結果纔是true。 |:運算特色: true | true = true; true | false = true; false | ture = true; false | false = false; ||運算規律: |運算的兩邊只要有一個是true,結果確定是true, 只有兩邊都爲false,結果都是false。 ^ 異或 : 和 「或」 有點不同, true | true = false; true | false = true; false | ture = true; false | false = false; ^ 異或的運算規律: ^ 符號的兩邊結果若是相同,結果是false 兩邊的結果不一樣,結果是true。 !: 非運算,判斷事物的另外一面。 !true = false 非真爲假 !false = true; 非假爲真 !!true = true; 非非真爲真 && 雙與: int x = 6 x > 2 && x<5 真與與假爲假 x > 2 & x<5 真與假爲假 int x = 1; x>2 % x<5 假與真爲假 x>2 && x<5 雙與一旦左邊爲假,右邊就不比較了。雙與比單與高效。 可作運費運算。 && 雙與的特色: 和& 運算的結果是同樣的,可是有點小區別。 單& :不管左邊的運算結果是什麼,右邊都參與運算。 雙&&:當左邊爲false時,右邊是不參與運算的。 ||: 和| 運算的結果是同樣的,可是有點小區別。 單| :不管左邊的運算結果是什麼,右邊都參與運算。 雙||:當左邊爲true時,右邊是不參與運算的。 */ int x = 6; System.out.println(x>2&x<5) //與:左邊爲真,右邊爲假,整體結果就爲假 System.out.println(x>2|x<5) //或:左邊爲真,右邊爲假,整體結果就爲真 } }
移位運算符:編譯
格式: 1.(條件表達式)? 表達式1:表達式2; 2.若是條件爲true,運算後的結果是表達式1; 3.若是條件爲false,運算後的結果是表達式2; 示例: 1.獲取兩個數中較大的數。 2. int x =3 ,y=4,z; 3. z = (x>y)?x:y; // z變量存儲的就是兩個數中較大的數。
示例:class
class Demo { public static void main(String[] args) { int x = 0;y; y = (x>1)?100:200; System.out.println("y="+y); int a,b; int max = a>b?a:b; int o,p,q; int temp = o>p?o:p; int max2 = temp>q?temp:q; } }