javaプログラミングの式と演算子

javaプログラミングの式と演算子
目次

式と演算子

式 (expression) は、式は、変数、定数、演算子、関数呼び出しなどを使用して構成されます。
例えば、2 + 3 や x * y などが式です。
演算子 (operator) は、式の中で値や変数に対して何らかの操作を行う記号です。
例えば、+ や * などが演算子です。

算術演算子(Arithmetic Operators)

ロボ君

算術演算子は、数値の演算を行うための演算子デス。

演算子説明
+加算
-減算
*乗算
/除算
%剰余

算術式 (Arithmetic Expressions)

数値の計算に使用されます。

   int x = 5;
   int y = 3;
   int addition = x + y;  // 加算
   int subtraction = x - y;  // 減算
   int multiplication = x * y;  // 乗算
   int division = x / y;  // 除算

文字列結合 (String Concatenation)

文字列を連結するために使用されます。

   String str1 = "Hello";
   String str2 = "World";
   String result = str1 + " " + str2;  // 文字列結合

比較演算子(Comparison Operators)

ロボ君

比較演算子は、2つの値を比較するための演算子デス。

演算子説明
==等しい
!=等しくない
>大きい
<小さい
>=大きいか等しい
<=小さいか等しい

比較式 (Comparison Expressions)

値の比較に使用されます。結果は真(true)または偽(false)です。

// 比較演算子
int x = 10;
int y = 20;

System.out.println(x == y); // false
System.out.println(x != y); // true
System.out.println(x > y); // false
System.out.println(x < y); // true
System.out.println(x >= y); // false
System.out.println(x <= y); // true

論理演算子(Logical Operators)

ロボ君

論理演算子は、複数の条件を組み合わせて評価するための演算子デス。

演算子説明
&&論理積(AND)
||論理和(OR)
!否定

論理積 A && B

論理和  A || B

否定  !A

否定
// 論理演算子
boolean a = true;
boolean b = false;

System.out.println(a && b); // false
System.out.println(a || b); // true
System.out.println(!a); // false

代入演算子

代入演算子を使用して、変数に値を代入します。

演算子説明
=値の代入
+=加算代入
-=減算代入
*=乗算代入
/=除算代入
%=剰余代入
// 代入演算子
int x = 10;

x += 10;
System.out.println(x); // 20

まとめ

種類説明
評価結果を生成する10 + 20
演算子式の評価を行う+, -, *, /, %
算術演算子数値の計算を行う+, -, *, /, %
比較演算子2 つの値の大小関係を比較する==, !=, >, <, >=, <=
論理演算子論理積や論理和の評価を行う&&, ||,!
代入演算子変数に値を代入する=, +=, -=, *=, /=, %=
public class Main {

	public static void main(String[] args) {
		
		// 算術演算子
		   int x = 10;
		   int y = 5;
		   int addition = x + y;  // 加算
		   int subtraction = x - y;  // 減算
		   int multiplication = x * y;  // 乗算
		   int division = x / y;  // 除算
		   System.out.println(addition);
		   System.out.println(subtraction);
		   System.out.println(multiplication);	
		   System.out.println(division);
				   
		// 文字列結合
		   String str1 = "Hello";
		   String str2 = "World";
		   String result = str1 + " " + str2; 
		   System.out.println(result);
		   
		// 比較演算子
		   int x2 = 10;
		   int y2 = 20;

		   System.out.println(x2 == y2); // false
		   System.out.println(x2 != y2); // true
		   System.out.println(x2 > y2); // false
		   System.out.println(x2 < y2); // true
		   System.out.println(x2 >= y2); // false
		   System.out.println(x2 <= y2); // true
		   
		// 論理演算子
		   boolean x3 = true;
		   boolean y3 = false;

		   System.out.println(x3 && y3); // false
		   System.out.println(x3 || y3); // true
		   System.out.println(!x3); // false
		
		// 代入演算子
		   int x4 = 10;

		   x4 += 10;
		   System.out.println(x); // 20

	}

}

教科書

Pythonの教科書は「やさしいPython」です。

Javaの教科書は「スッキリわかるJava入門 実践編 第3版」です。

著:中山清喬, 著:国本大悟, 監修:株式会社フレアリンク
¥2,680 (2024/04/28 18:03時点 | Amazon調べ)

SQLの教科書は「スッキリわかるSQL入門第3版」です。

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!
目次