Java基礎-賦值運算符Assignment Operators與條件運算符Condition Operators
阿新 • • 發佈:2018-04-02
class .com under spa ava static AR assign ati
Java基礎-賦值運算符Assignment Operators與條件運算符Condition Operators
作者:尹正傑
版權聲明:原創作品,謝絕轉載!否則將追究法律責任。
一.賦值運算符
表達式的數據類型要與左邊變量的類型兼容
1>.常規賦值
1 /* 2 @author :yinzhengjie 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/ 4 EMAIL:[email protected] 5 */6 7 public class Assignment{ 8 public static void main(String[] args){ 9 //1>.賦值 10 int x = 123; 11 12 x = 123 + 5; 13 14 int y = x / 2; 15 16 // int z = 3.1415926; //類型不兼容。 17 18 System.out.println(x); //128 19 System.out.println(y); //64 20 } 21 }
2>.符合賦值,自反賦值
1 /* 2 @author :yinzhengjie 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/ 4 EMAIL:[email protected] 5 */ 6 7 public class Assignment2{ 8 public static void main(String[] args){ 9 //復合賦值隱含著強類型轉換10 11 byte a = 10; 12 13 a += 5; //相當於 a = (byte)(a + 5) 14 15 System.out.println(a); //15 16 } 17 }
二.條件運算符
條件運算符也叫三元運算符。語法格式:“(條件)?表達式1:表達式2”,如果條件成立,整個表達式的值就是表達式1的值,如果條件不成立,整個表達式的值就是表達式2的值。
Java基礎-賦值運算符Assignment Operators與條件運算符Condition Operators