Home > Java > javaTutorial > body text

[java tutorial] Java operators

黄舟
Release: 2016-12-26 11:36:25
Original
1506 people have browsed it

Java Operators

One of the most basic uses of computers is to perform mathematical operations. As a computer language, Java also provides a rich set of operators to manipulate variables. We can divide the operators into the following groups:

Arithmetic operators

Relational operators

Bitwise operators

Logical operators

Assignment Operator

Other Operators

Arithmetic Operators

Arithmetic operators are used in mathematical expressions, and they work the same as they do in mathematics. The following table lists all arithmetic operators.

The example in the table assumes that the value of integer variable A is 10 and the value of variable B is 20:


Operator

Description

Example


+ Addition - the values ​​on both sides of the addition operator A + B equals 30

- Subtraction - the left operand Subtract the right operand A – B equals -10

* Multiplication - multiply the values ​​on both sides of the operator A * B equals 200

/ Division - divide the left operand by the right operand B / A is equal to 2

% Modulo - the remainder of the right operand divided by the left operand B%A is equal to 0

+ + Autoincrement - the value of the operand is increased by 1 B + + is equal to 21

- Decrement - the value of the operand is reduced by 1 B - - equals 19

Example

The following simple example program demonstrates the arithmetic operators. Copy and paste the following Java program and save it as a Test.java file, then compile and run the program:

public class Test {

  public static void main(String args[]) {
     int a = 10;
     int b = 20;
     int c = 25;
     int d = 25;
     System.out.println("a + b = " + (a + b) );
     System.out.println("a - b = " + (a - b) );
     System.out.println("a * b = " + (a * b) );
     System.out.println("b / a = " + (b / a) );
     System.out.println("b % a = " + (b % a) );
     System.out.println("c % a = " + (c % a) );
     System.out.println("a++   = " +  (a++) );
     System.out.println("b--   = " +  (a--) );
     // Check the difference in d++ and ++d
     System.out.println("d++   = " +  (d++) );
     System.out.println("++d   = " +  (++d) );
  }
}
Copy after login

The above example compiles and runs the result as follows:

a + b = 30
a - b = -10
a * b = 200
b / a = 2
b % a = 0
c % a = 5
a++   = 10
b--   = 11
d++   = 25
++d   = 27
Copy after login

Relational operator

The following table shows the relational operators supported by Java

The value of the example integer variable A in the table is 10, and the value of variable B is 20:


Operator

Description

Example


== Check if the values ​​of the two operands are equal. If they are equal, the condition is real. (A == B) is false (not true).

!= Checks if the values ​​of the two operands are equal. If the values ​​are not equal, the condition is true. (A != B) is true.

> Check whether the value of the left operand is greater than the value of the right operand, if so then the condition is true. (A> B) is not true.

< Checks whether the value of the left operand is less than the value of the right operand, if so then the condition is true. (A

> = Check whether the value of the left operand is greater than or equal to the value of the right operand, if so then the condition is true. (A> = B) is false.

<= Checks whether the value of the left operand is less than or equal to the value of the right operand, if so then the condition is true. (A <= B) is true.

Example

The following simple example program demonstrates the relational operators. Copy and paste the following Java program and save it as a Test.java file, then compile and run the program:

public class Test {

  public static void main(String args[]) {
     int a = 10;
     int b = 20;
     System.out.println("a == b = " + (a == b) );
     System.out.println("a != b = " + (a != b) );
     System.out.println("a > b = " + (a > b) );
     System.out.println("a < b = " + (a < b) );
     System.out.println("b >= a = " + (b >= a) );
     System.out.println("b <= a = " + (b <= a) );
  }
}
Copy after login

The above example compilation and running results are as follows:

a == b = false
a != b = true
a > b = false
a < b = true
b >= a = true
b <= a = false
Copy after login

Bitwise operators

Java defines bitwise operators, which are applied to integer types (int), long integer (long), short integer (short), character type (char), and byte type (byte). The

bitwise operators operate on all bits and operate bitwise. Assume a = 60, and b = 13; their binary format representation will be as follows:

A = 0011 1100
B = 0000 1101
-----------------
A&b = 0000 1100
A | B = 0011 1101
^ B = 0011 0001
~A= 1100 0011
Copy after login

The following table lists the basic operations of bit operators, assuming the value of integer variable A is 60 and the value of variable B For 13:


Operator

Description

Example


& Bitwise AND operator, the result is 1 if and only if a certain bit of both operands is non-0. (A&B), get 12, that is, 0000 1100

| Bitwise OR operator, as long as one of the two operands has a non-0, the result bit will be 1. (A | B) gets 61, which is 0011 1101

^ 按位异或操作符,两个操作数的某一位不相同时候结果的该位就为1。 (A ^ B)得到49,即 0011 0001

〜 按位补运算符翻转操作数的每一位。 (〜A)得到-60,即1100 0011

<< 按位左移运算符。左操作数按位左移右操作数指定的位数。 A << 2得到240,即 1111 0000

>> 按位右移运算符。左操作数按位右移右操作数指定的位数。 A >> 2得到15即 1111

>>> 按位右移补零操作符。左操作数的值按右操作数指定的位数右移,移动得到的空位以零填充。 A>>>2得到15即0000 1111

实例

下面的简单示例程序演示了位运算符。复制并粘贴下面的Java程序并保存为Test.java文件,然后编译并运行这个程序:

public class Test {
  public static void main(String args[]) {
     int a = 60; /* 60 = 0011 1100 */ 
     int b = 13; /* 13 = 0000 1101 */
     int c = 0;
     c = a & b;       /* 12 = 0000 1100 */
     System.out.println("a & b = " + c );

     c = a | b;       /* 61 = 0011 1101 */
     System.out.println("a | b = " + c );

     c = a ^ b;       /* 49 = 0011 0001 */
     System.out.println("a ^ b = " + c );

     c = ~a;          /*-61 = 1100 0011 */
     System.out.println("~a = " + c );

     c = a << 2;     /* 240 = 1111 0000 */
     System.out.println("a << 2 = " + c );

     c = a >> 2;     /* 215 = 1111 */
     System.out.println("a >> 2  = " + c );
  
     c = a >>> 2;     /* 215 = 0000 1111 */
     System.out.println("a >>> 2 = " + c );
  }
}
Copy after login

以上实例编译运行结果如下:

a & b = 12
a | b = 61
a ^ b = 49
~a = -61
a << 2 = 240
a >> 15
a >>> 15
Copy after login

逻辑运算符

下表列出了逻辑运算符的基本运算,假设布尔变量A为真,变量B为假


操作符

描述

例子


&& 称为逻辑与运算符。当且仅当两个操作数都为真,条件才为真。 (A && B)为假。

| | 称为逻辑或操作符。如果任何两个操作数任何一个为真,条件为真。 (A | | B)为真。

! 称为逻辑非运算符。用来反转操作数的逻辑状态。如果条件为true,则逻辑非运算符将得到false。 !(A && B)为真。

实例

下面的简单示例程序演示了逻辑运算符。复制并粘贴下面的Java程序并保存为Test.java文件,然后编译并运行这个程序:

public class Test {
  public static void main(String args[]) {
     boolean a = true;
     boolean b = false;
     System.out.println("a && b = " + (a&&b));
     System.out.println("a || b = " + (a||b) );
     System.out.println("!(a && b) = " + !(a && b));
  }
}
Copy after login

以上实例编译运行结果如下:

a && b = false
a || b = true
!(a && b) = true
Copy after login

赋值运算符

下面是Java语言支持的赋值运算符:


操作符

描述

例子


= 简单的赋值运算符,将右操作数的值赋给左侧操作数 C = A + B将把A + B得到的值赋给C

+ = 加和赋值操作符,它把左操作数和右操作数相加赋值给左操作数 C + = A等价于C = C + A

- = 减和赋值操作符,它把左操作数和右操作数相减赋值给左操作数 C - = A等价于C = C -
A

* = 乘和赋值操作符,它把左操作数和右操作数相乘赋值给左操作数 C * = A等价于C = C * A

/ = 除和赋值操作符,它把左操作数和右操作数相除赋值给左操作数 C / = A等价于C = C / A

(%)= 取模和赋值操作符,它把左操作数和右操作数取模后赋值给左操作数 C%= A等价于C = C%A

<< = 左移位赋值运算符 C << = 2等价于C = C << 2

>> = 右移位赋值运算符 C >> = 2等价于C = C >> 2

&= 按位与赋值运算符 C&= 2等价于C = C&2

^ = 按位异或赋值操作符 C ^ = 2等价于C = C ^ 2

| = 按位或赋值操作符 C | = 2等价于C = C | 2

实例

面的简单示例程序演示了赋值运算符。复制并粘贴下面的Java程序并保存为Test.java文件,然后编译并运行这个程序:

public class Test {
  public static void main(String args[]) {
     int a = 10;
     int b = 20;
     int c = 0;
     c = a + b;
     System.out.println("c = a + b = " + c );
     c += a ;
     System.out.println("c += a  = " + c );
     c -= a ;
     System.out.println("c -= a = " + c );
     c *= a ;
     System.out.println("c *= a = " + c );
     a = 10;
     c = 15;
     c /= a ;
     System.out.println("c /= a = " + c );
     a = 10;
     c = 15;
     c %= a ;
     System.out.println("c %= a  = " + c );
     c <<= 2 ;
     System.out.println("c <<= 2 = " + c );
     c >>= 2 ;
     System.out.println("c >>= 2 = " + c );
     c >>= 2 ;
     System.out.println("c >>= a = " + c );
     c &= a ;
     System.out.println("c &= 2  = " + c );
     c ^= a ;
     System.out.println("c ^= a   = " + c );
     c |= a ;
     System.out.println("c |= a   = " + c );
  }
}
Copy after login

以上实例编译运行结果如下:

c = a + b = 30
c += a  = 40
c -= a = 30
c *= a = 300
c /= a = 1
c %= a  = 5
c <<= 2 = 20
c >>= 2 = 5
c >>= 2 = 1
c &= a  = 0
c ^= a   = 10
c |= a   = 10
Copy after login

条件运算符(?:)

条件运算符也被称为三元运算符。该运算符有3个操作数,并且需要判断布尔表达式的值。该运算符的主要是决定哪个值应该赋值给变量。

variable x = (expression) ? value if true : value if false
Copy after login

实例

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 );
   }
Copy after login

以上实例编译运行结果如下:

Value of b is : 30
Value of b is : 20
Copy after login

instanceOf 运算符

该运算符用于操作对象实例,检查该对象是否是一个特定类型(类类型或接口类型)。

instanceof运算符使用格式如下:

( Object reference variable ) instanceOf  (class/interface type)
Copy after login

如果运算符左侧变量所指的对象,是操作符右侧类或接口(class/interface)的一个对象,那么结果为真。

下面是一个例子:

String name = &#39;James&#39;;
boolean result = name instanceOf String; // 由于name是Strine类型,所以返回真
Copy after login

如果被比较的对象兼容于右侧类型,该运算符仍然返回true。

看下面的例子:

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);
   }
}
Copy after login

以上实例编译运行结果如下:

true
Copy after login

Java运算符优先级

当多个运算符出现在一个表达式中,谁先谁后呢?这就涉及到运算符的优先级别的问题。在一个多运算符的表达式中,运算符优先级不同会导致最后得出的结果差别甚大。

例如,(1+3)+(3+2)*2,这个表达式如果按加号最优先计算,答案就是 18,如果按照乘号最优先,答案则是 14。

再如,x = 7 + 3 * 2;这里x得到13,而不是20,因为乘法运算符比加法运算符有较高的优先级,所以先计算3 * 2得到6,然后再加7。

下表中具有最高优先级的运算符在的表的最上面,最低优先级的在表的底部。


类别

操作符

关联性


后缀    () [] . (点操作符)    左到右    

一元    + + - !〜    从右到左    

乘性     * /%    左到右    

加性     + -    左到右    

移位     >> >>>  << 左到右

关系 >> = << = 左到右

相等 == != 左到右

按位与 & 左到右

按位异或 ^ 左到右

按位或 | 左到右

逻辑与 && 左到右

逻辑或 | | 左到右

条件 ?: 从右到左

赋值 = + = - = * = / =%= >> = << =&= ^ = | =    从右到左    

逗号    ,    左到右    

 以上就是【java教程】Java运算符的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template