運算符
- 算術運算符 + - * / % ++ --
package operator; public class Demo01 { public static void main(String[] args) { //二進制運算符 //ctrl + D 複制目前到下一行 int a = 10; int b = 20; int c = 5; int d = 15; System.out.println(a+b); System.out.println(a-b); System.out.println(a*b); System.out.println(a/(double)b);//除法會有小數 需要進行強制轉換 } }
package operator; public class Demo02 { public static void main(String[] args) { long a = 121231231231232L; int b = 123; short c = 10; byte d = 8; System.out.println(a+b+c+d);// 裡面有long 就按long類型輸出 System.out.println(b+c+d);// 沒有long 按int類型輸出 System.out.println(c+d); } }
package operator; public class Demo04 { public static void main(String[] args) { //++ -- 自增 自減 一進制運算符 int a = 3; int b = a++; // a++ a = a + 1 先給b指派,再自增相當于在代碼後一行 a = a + 1 int c = ++a; // ++a a = a + 1 先自增,再把值給c指派 相當于再代碼前一行 執行 a = a + 1 System.out.println(a); System.out.println(b); System.out.println(c); // 幂運算 2^3 2×2×2 會使用工具類進行運算操作 double pow = Math.pow(3, 2); System.out.println(pow); } }
- 指派運算符 =
-
package operator; public class Demo03 { public static void main(String[] args) { //關系運算符傳回值;布爾值 int a = 10; int b = 20; int c = 21; System.out.println(a>b); System.out.println(a<b); System.out.println(a==b); System.out.println(a!=b); System.out.println(c%a);// 取餘數 模運算 } }