1. 程式人生 > 實用技巧 >java 運算子

java 運算子

運算子是一種特殊的符號,表示資料的運算、賦值和比較

算術運算、賦值運算、比較運算、邏輯運算、位運算(開發用的少,原始碼中有存在)、三元運算

運算子 運算 類別 結果
+ +3 3
- b=4;-b -4
+ 5+5 10
- 6-4 2
* 3*4 12
/ 5/5 1
% 取模(取餘數) 7%5 2
++ 前加加自增在前,先運算後取值 a=2;b=++a a=3;b=3
++ 後加加自增在後,先取值後運算 a=2;b=a++ a=3;b=2
-- 前減減自減在前,先運算後取值 a=2;b=--a a=1;b=1
-- 後減減自減在後,先取值後運算 a=2;b=a-- a=1;b=2
+ 字元連線運算 "He"+"llo" "Hello"

加減乘除的示例

class  AriTest
{
	public static void main(String[] args) 
	{
		int num1 = 12;
		int num2 = 5;
		int result1 = num1 / num2;  //除法運算
		System.out.println(result1);
		int result2 = num1 - num2;  //減法
		System.out.println(result2);
		int result3 = num1 * num2 ;  //乘
		System.out.println(result3);
		int result4 = num1 + num2;   // 加
		System.out.println(result4);
	}
}
編譯後執行
---------- java ----------
2
7
60
17

輸出完成 (耗時 0 秒) - 正常終止

  示例2,使計算過為浮點型

class  AriTest
{
	public static void main(String[] args) 
	{
		int num1 = 12;
		int num2 = 5;
		int result1 = num1 / num2;  //除法運算
		System.out.println(result1);
		int result2 = num1 - num2;  //減法
		System.out.println(result2);
		int result3 = num1 * num2 ;  //乘
		System.out.println(result3);
		int result4 = num1 + num2;   // 加
		System.out.println(result4);
		double result5 = (double)num1 / num2;   //浮點型結果為2.41
		System.out.println (result5);
	}
}
---------- java ----------
2
7
60
17
2.4

輸出完成 (耗時 0 秒) - 正常終止

  取模

class  AriTest
{
	public static void main(String[] args) 
	{
		int num1 = 12;
		int num2 = 5;
		int result1 = num1 / num2;  //除法運算
		System.out.println(result1);
		int result2 = num1 - num2;  //減法
		System.out.println(result2);
		int result3 = num1 * num2 ;  //乘
		System.out.println(result3);
		int result4 = num1 + num2;   // 加
		System.out.println(result4);
		double result5 = (double)num1 / num2;   //浮點型結果為2.41
		System.out.println (result5);
		int result6 = num1 % num2;     //	取模
		System.out.println (result6);
	
	}
}
---------- java ----------
2
7
60
17
2.4
2

輸出完成 (耗時 0 秒) - 正常終止

  前加加;後加加

class  AriTest
{
	public static void main(String[] args) 
	{
		int num1 = 12;
		int num2 = 5;
		int result1 = num1 / num2;  //除法運算
		System.out.println(result1);
		int result2 = num1 - num2;  //減法
		System.out.println(result2);
		int result3 = num1 * num2 ;  //乘
		System.out.println(result3);
		int result4 = num1 + num2;   // 加
		System.out.println(result4);
		double result5 = (double)num1 / num2;   //浮點型結果為2.41
		System.out.println (result5);
		int result6 = num1 % num2;     //	取模
		System.out.println (result6);
		int a1 = 10;
		int b1 = ++a1;// 前加加,先運算後賦值
		System.out.println(b1+","+a1);
		int a2 = 10;
		int b2 = a2++;// 後加加,先賦值後運算
		System.out.println(b2 +"," + a2);
	
	}
}
---------- java ----------
2
7
60
17
2.4
2
11,11
10,11

輸出完成 (耗時 0 秒) - 正常終止

  注意點

class  AriTest
{
	public static void main(String[] args) 
	{
		int num1 = 12;
		int num2 = 5;
		int result1 = num1 / num2;  //除法運算
		System.out.println(result1);
		int result2 = num1 - num2;  //減法
		System.out.println(result2);
		int result3 = num1 * num2 ;  //乘
		System.out.println(result3);
		int result4 = num1 + num2;   // 加
		System.out.println(result4);
		double result5 = (double)num1 / num2;   //浮點型結果為2.41
		System.out.println (result5);
		int result6 = num1 % num2;     //	取模
		System.out.println (result6);
		int a1 = 10;
		int b1 = ++a1;// 前加加,先運算後賦值
		System.out.println(b1+","+a1);
		int a2 = 10;
		int b2 = a2++;// 後加加,先賦值後運算
		System.out.println(b2 +"," + a2);
		//注意
		short s1 =  10;
		s1 = s1+1;// 編譯不過
		
	    System.out.println(s1);
	}
}
---------- javac ----------
AriTest.java:27: 錯誤: 不相容的型別: 從int轉換到short可能會有損失
		s1 = s1+1;// 編譯不過
		       ^
1 個錯誤

輸出完成 (耗時 0 秒) - 正常終止

  建議一下兩種方式

class  AriTest
{
	public static void main(String[] args) 
	{
		int num1 = 12;
		int num2 = 5;
		int result1 = num1 / num2;  //除法運算
		System.out.println(result1);
		int result2 = num1 - num2;  //減法
		System.out.println(result2);
		int result3 = num1 * num2 ;  //乘
		System.out.println(result3);
		int result4 = num1 + num2;   // 加
		System.out.println(result4);
		double result5 = (double)num1 / num2;   //浮點型結果為2.41
		System.out.println (result5);
		int result6 = num1 % num2;     //	取模
		System.out.println (result6);
		int a1 = 10;
		int b1 = ++a1;// 前加加,先運算後賦值
		System.out.println(b1+","+a1);
		int a2 = 10;
		int b2 = a2++;// 後加加,先賦值後運算
		System.out.println(b2 +"," + a2);
		//注意
		short s1 =  10;
		//s1 = s1+1;// 編譯不過
		s1 = (short)(s1+1);
	    System.out.println(s1);
	}
}
---------- java ----------
2
7
60
17
2.4
2
11,11
10,11
11

輸出完成 (耗時 0 秒) - 正常終止

  或者

class  AriTest
{
	public static void main(String[] args) 
	{
		int num1 = 12;
		int num2 = 5;
		int result1 = num1 / num2;  //除法運算
		System.out.println(result1);
		int result2 = num1 - num2;  //減法
		System.out.println(result2);
		int result3 = num1 * num2 ;  //乘
		System.out.println(result3);
		int result4 = num1 + num2;   // 加
		System.out.println(result4);
		double result5 = (double)num1 / num2;   //浮點型結果為2.41
		System.out.println (result5);
		int result6 = num1 % num2;     //	取模
		System.out.println (result6);
		int a1 = 10;
		int b1 = ++a1;// 前加加,先運算後賦值
		System.out.println(b1+","+a1);
		int a2 = 10;
		int b2 = a2++;// 後加加,先賦值後運算
		System.out.println(b2 +"," + a2);
		//注意
		short s1 =  10;
		//s1 = s1+1;// 編譯不過
		//s1 = (short)(s1+1);
		++s1;// 
	    System.out.println(s1);
	}
}
---------- java ----------
2
7
60
17
2.4
2
11,11
10,11
11

輸出完成 (耗時 0 秒) - 正常終止

  或者

class  AriTest
{
	public static void main(String[] args) 
	{
		int num1 = 12;
		int num2 = 5;
		int result1 = num1 / num2;  //除法運算
		System.out.println(result1);
		int result2 = num1 - num2;  //減法
		System.out.println(result2);
		int result3 = num1 * num2 ;  //乘
		System.out.println(result3);
		int result4 = num1 + num2;   // 加
		System.out.println(result4);
		double result5 = (double)num1 / num2;   //浮點型結果為2.41
		System.out.println (result5);
		int result6 = num1 % num2;     //	取模
		System.out.println (result6);
		int a1 = 10;
		int b1 = ++a1;// 前加加,先運算後賦值
		System.out.println(b1+","+a1);
		int a2 = 10;
		int b2 = a2++;// 後加加,先賦值後運算
		System.out.println(b2 +"," + a2);
		//注意
		short s1 =  10;
		//s1 = s1+1;// 編譯不過
		//s1 = (short)(s1+1);
		s1++;// 這樣寫不會改變他的資料型別
	    System.out.println(s1);
	}
}

  前減減;後減減

class  AriTest
{
	public static void main(String[] args) 
	{
		int num1 = 12;
		int num2 = 5;
		int result1 = num1 / num2;  //除法運算
		System.out.println(result1);
		int result2 = num1 - num2;  //減法
		System.out.println(result2);
		int result3 = num1 * num2 ;  //乘
		System.out.println(result3);
		int result4 = num1 + num2;   // 加
		System.out.println(result4);
		double result5 = (double)num1 / num2;   //浮點型結果為2.41
		System.out.println (result5);
		int result6 = num1 % num2;     //	取模
		System.out.println (result6);
		int a1 = 10;
		int b1 = ++a1;// 前加加,先運算後賦值
		System.out.println(b1+","+a1);
		int a2 = 10;
		int b2 = a2++;// 後加加,先賦值後運算
		System.out.println(b2 +"," + a2);
		//注意
		short s1 =  10;
		//s1 = s1+1;// 編譯不過
		//s1 = (short)(s1+1);
		s1++;// 這樣寫不會改變他的資料型別
	    System.out.println(s1); 
		int s2 = 15;
		int s3 = --s2;
		System.out.println(s3);
		int s4 = 15;
		int s5 = s4--;
		System.out.println(s5);
	}
}
---------- java ----------
2
7
60
17
2.4
2
11,11
10,11
11
14
15

輸出完成 (耗時 0 秒) - 正常終止