Java-basic-operators

提供:Dev Guides
移動先:案内検索

Java-基本的な演算子

Javaは、変数を操作するための豊富な演算子セットを提供します。 私たちはすべてのJava演算子を次のグループに分けることができます-

  • 算術演算子
  • 関係演算子
  • ビット演算子
  • 論理演算子
  • 割り当て演算子
  • その他の演算子

算術演算子

算術演算子は、代数で使用されるのと同じ方法で数式で使用されます。 次の表は、算術演算子を示しています-

整数変数Aが10を保持し、変数Bが20を保持すると仮定します-

リンク:/java/java_arithmatic_operators_examples [例を表示]

Operator Description Example
PLUS (Addition) Adds values on either side of the operator. A PLUS B will give 30
- (Subtraction) Subtracts right-hand operand from left-hand operand. A - B will give -10
AST (Multiplication) Multiplies values on either side of the operator. A AST B will give 200
/(Division) Divides left-hand operand by right-hand operand. B/A will give 2
% (Modulus) Divides left-hand operand by right-hand operand and returns remainder. B % A will give 0
PLUSPLUS (Increment) Increases the value of operand by 1. BPLUSPLUS gives 21
 — (Decrement) Decreases the value of operand by 1. B-- gives 19

関係演算子

Java言語でサポートされている関係演算子は次のとおりです。

変数Aが10を保持し、変数Bが20を保持すると仮定します-

リンク:/java/java_relational_operators_examples [例を表示]

Operator Description Example
== (equal to) Checks if the values of two operands are equal or not, if yes then condition becomes true. (A == B) is not true.
!= (not equal to) Checks if the values of two operands are equal or not, if values are not equal then condition becomes true. (A != B) is true.
> (greater than) Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. (A > B) is not true.
< (less than) Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. (A < B) is true.
>= (greater than or equal to) Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. (A >= B) is not true.
⇐ (less than or equal to) Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. (A ⇐ B) is true.

ビット演算子

Javaでは、整数型、long、int、short、char、およびbyteに適用できるいくつかのビット演算子を定義しています。

ビット演算子はビットに対して機能し、ビットごとの操作を実行します。 a = 60お​​よびb = 13であると仮定します。今バイナリ形式では、次のようになります-

a = 0011 1100

b = 0000 1101

a&b = 0000 1100

a | b = 0011 1101

a ^ b = 0011 0001

〜a = 1100 0011

次の表は、ビットごとの演算子を示しています-

整数変数Aが60を保持し、変数Bが13を保持すると仮定します-

リンク:/java/java_bitwise_operators_examples [例を表示]

[cols="^,,^",options="header",]
|===
|Operator |Description |Example |& (bitwise and) |Binary AND Operator copies a bit to the result if it exists in both operands. |(A & B) will give 12 which is 0000 1100 || (bitwise or) |Binary OR Operator copies a bit if it exists in either operand. |(A | B) will give 61 which is 0011 1101 |^ (bitwise XOR) |Binary XOR Operator copies the bit if it is set in one operand but not both. |(A ^ B) will give 49 which is 0011 0001 |~ (bitwise compliment) |Binary Ones Complement Operator is unary and has the effect of 'flipping' bits. |(~A ) will give -61 which is 1100 0011 in 2's complement form due to a signed binary number. |<< (left shift) |Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand. |A << 2 will give 240 which is 1111 0000 |>> (right shift) |Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand. |A >> 2 will give 15 which is 1111 |>>> (zero fill right shift) |Shift right zero fill operator. The left operands value is moved right by the number of bits specified by the right operand and shifted values are filled up with zeros. |A >>>2 will give 15 which is 0000 1111
|===

=== 論理演算子

次の表は、論理演算子を示しています-

ブール変数Aがtrueを保持し、変数Bがfalseを保持すると仮定します-

リンク:/java/java_logical_operators_examples [例を表示]

[cols="^,,^",options="header",]
|===
|Operator |Description |Example |&& (logical and) |Called Logical AND operator. If both the operands are non-zero, then the condition becomes true. |(A && B) is false ||| (logical or) |Called Logical OR Operator. If any of the two operands are non-zero, then the condition becomes true. |(A || B) is true |! (logical not) |Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false. |!(A && B) is true
|===

=== 割り当て演算子

以下は、Java言語でサポートされている割り当て演算子です-

リンク:/java/java_assignment_operators_examples [例を表示]

[cols="^,,^",options="header",]
|===
|Operator |Description |Example |= |Simple assignment operator. Assigns values from right side operands to left side operand. |C = A &plus; B will assign value of A &plus; B into C |&plus;= |Add AND assignment operator. It adds right operand to the left operand and assign the result to left operand. |C &plus;= A is equivalent to C = C &plus; A |-= |Subtract AND assignment operator. It subtracts right operand from the left operand and assign the result to left operand. |C -= A is equivalent to C = C – A |&ast;= |Multiply AND assignment operator. It multiplies right operand with the left operand and assign the result to left operand. |C &ast;= A is equivalent to C = C &ast; A |/= |Divide AND assignment operator. It divides left operand with the right operand and assign the result to left operand. |C/= A is equivalent to C = C/A |%= |Modulus AND assignment operator. It takes modulus using two operands and assign the result to left operand. |C %= A is equivalent to C = C % A |<<= |Left shift AND assignment operator. |C <<= 2 is same as C = C << 2 |>>= |Right shift AND assignment operator. |C >>= 2 is same as C = C >> 2 |&= |Bitwise AND assignment operator. |C &= 2 is same as C = C & 2 |^= |bitwise exclusive OR and assignment operator. |C ^= 2 is same as C = C ^ 2 ||= |bitwise inclusive OR and assignment operator. |C |= 2 is same as C = C | 2
|===

=== その他の演算子

Java言語でサポートされている他の演算子はほとんどありません。

==== 条件演算子(? : )

条件演算子は、*三項演算子*とも呼ばれます。 この演算子は3つのオペランドで構成され、ブール式を評価するために使用されます。 オペレーターの目標は、変数に割り当てる値を決定することです。 演算子は次のように書かれています-

[source,result,notranslate]
----
variable x = (expression) ? value if true : value if false
----

以下は例です-

*例*

[source,prettyprint,notranslate]
----
public class Test {

   public static void main(String args[]) {
      int a, b;
      a = 10;
      b = (a == 1) ? 20: 30;
      System.out.println( "Value of b is : " +  b );

      b = (a == 10) ? 20: 30;
      System.out.println( "Value of b is : " + b );
   }
}
----

これは、次の結果を生成します-

*出力*

[source,result,notranslate]
----
Value of b is : 30
Value of b is : 20
----

==== instanceof演算子

この演算子は、オブジェクト参照変数にのみ使用されます。 オペレーターは、オブジェクトが特定のタイプ(クラスタイプまたはインターフェースタイプ)であるかどうかをチェックします。 instanceof演算子は次のように記述されます-

[source,result,notranslate]
----
( Object reference variable ) instanceof  (class/interface type)
----

演算子の左側の変数によって参照されるオブジェクトが右側のクラス/インターフェイスタイプのIS-Aチェックに合格した場合、結果はtrueになります。 以下は例です-

*例*

[source,prettyprint,notranslate]
----
public class Test {

   public static void main(String args[]) {

      String name = "James";

     //following will return true since name is type of String
      boolean result = name instanceof String;
      System.out.println( result );
   }
}
----

これは、次の結果を生成します-

*出力*

[source,result,notranslate]
----
true
----

比較されるオブジェクトが右側の型と互換性のある割り当てである場合、この演算子は依然としてtrueを返します。 以下はもう一つの例です-

*例*

[source,prettyprint,notranslate]
----
class Vehicle {}

public class Car extends Vehicle {

   public static void main(String args[]) {

      Vehicle a = new Car();
      boolean result =  a instanceof Car;
      System.out.println( result );
   }
}
----

これは、次の結果を生成します-

*出力*

[source,result,notranslate]
----
true
----

=== Javaオペレーターの優先順位

演算子の優先順位は、式内の用語のグループ化を決定します。 これは、式の評価方法に影響します。 特定の演算子は、他の演算子よりも優先順位が高くなっています。たとえば、乗算演算子は加算演算子よりも優先順位が高い-

たとえば、x = 7&plus; 3&ast; 2;ここでは、演算子&ast;が20であるため、xは20ではなく13に割り当てられます&plus;よりも優先順位が高いため、最初に3が乗算されます&ast; 2から7に追加します。

ここでは、優先順位が最も高い演算子が表の上部に表示され、優先順位が最も低い演算子が下部に表示されます。 式内では、優先順位の高い演算子が最初に評価されます。

[cols=",,",options="header",]
|===
|Category |Operator |Associativity |Postfix |expression++ expression-- |Left to right |Unary |++expression –-expression +expression –expression ~ ! |Right to left |Multiplicative | */% |Left to right |Additive |+ - |Left to right |Shift |<< >> >>> |Left to right |Relational |< > <= >= instanceof |Left to right |Equality |== != |Left to right |Bitwise AND |& |Left to right |Bitwise XOR |^ |Left to right |Bitwise OR || |Left to right |Logical AND |&& |Left to right |Logical OR ||| |Left to right |Conditional |?: |Right to left |Assignment |= += -=* =/= %= ^= |= <<= >>= >>>= |Right to left
|===

=== 次は何ですか?

次の章では、Javaプログラミングのループ制御について説明します。 この章では、さまざまなタイプのループと、これらのループがJavaプログラム開発でどのように使用されるか、およびそれらがどのような目的で使用されるかについて説明します。