Home > Java > javaTutorial > JAVA entry tutorial | Chapter 5 Operation coincidence set

JAVA entry tutorial | Chapter 5 Operation coincidence set

黄舟
Release: 2017-02-25 09:56:56
Original
1185 people have browsed it

Java Operators

One of the most basic uses of a computer 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 operators

  • Other operators


Arithmetic operators

Arithmetic operators are used in mathematical expressions and their functions are the same as 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 - Phase The value on both sides of the addition operator A + B is equal to 30
- Subtraction - left operand minus right operand A – B is equal to -10
* Multiplication - the values ​​on both sides of the multiplication operator A * B is equal to 200
/ Division - left operand divided by right operand B/A equals 2
% Modulo - the remainder of the left operand divided by the right operand B%A is equal to 0
++ Auto-increment: The value of the operand increases by 1 B++ or ++B is equal to 21 (see the difference below)
-- Decrement: the value of the operand is reduced by 1 B-- or --B is equal to 19 (see the difference below)

Example

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("a--   = " +  (a--) );
     // 查看  d++ 与 ++d 的不同
     System.out.println("d++   = " +  (d++) );
     System.out.println("++d   = " +  (++d) );
  }
}
Copy after login


Auto-increment and self-decrement operators


1. Auto-increment (++) and self-decrement (--) operators Is a special arithmetic operator that requires two operands to perform operations, while the increment and decrement operator requires one operand.

public class selfAddMinus{
    public static void main(String[] args){
        int a = 3;//定义一个变量;
        int b = ++a;//自增运算
        int c = 3;
        int d = --c;//自减运算
        System.out.println("进行自增运算后的值等于"+b);
        System.out.println("进行自减运算后的值等于"+d);
    }
}
Copy after login


The running result is:

进行自增运算后的值等于4进行自减运算后的值等于2
Copy after login

Analysis:

  • int b = ++a; 拆分运算过程为: a=a+1=4; b=a=4, 最后结果为b=4,a=4
    int d = --c; 拆分运算过程为: c=c-1=2; d=c=2, 最后结果为d=2,c=2
    Copy after login

2. Prefix auto-increment and auto-subtraction method (++a,--a): First perform the self-increment or self-decrement operation, and then perform the expression operation.


3. Suffix auto-increment and auto-subtraction (a++,a--): First perform expression operation, then perform auto-increment or Example of auto-decrement operation:

public class selfAddMinus{
    public static void main(String[] args){
        int a = 5;//定义一个变量;
        int b = 5;
        int x = 2*++a;
        int y = 2*b++;
        System.out.println("自增运算符前缀运算后a="+a+",x="+x);
        System.out.println("自增运算符后缀运算后b="+b+",y="+y);
    }
}
Copy after login

The operation result is:

自增运算符前缀运算后a=6,x=12自增运算符后缀运算后b=6,y=10
Copy after login

Relational operators

The following table shows the relational operators supported by Java

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

OperatorDescriptionExample
==Check if Whether the values ​​of the two operands are equal, if so, the condition is true. (A == B) is false (not true).
!=Checks if the values ​​of the two operands are equal, if the values ​​are not equal then 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
> =Checks 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.

实例

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

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

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

a == b = falsea != b = truea > b = falsea < b = trueb >= a = trueb <= a = false
Copy after login

位运算符

Java定义了位运算符,应用于整数类型(int),长整型(long),短整型(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
Copy after login

下表列出了位运算符的基本运算,假设整数变量A的值为60和变量B的值为13:

##<< Bitwise left shift operator. The left operand is shifted left by the number of bits specified by the right operand.##>>> Bitwise right shift zero-fill operator. The value of the left operand is shifted to the right by the number of bits specified by the right operand, and the resulting vacancies are filled with zeros. A>>>2 gets 15 which is 0000 1111


实例

复制并粘贴下面的Java程序并保存为TestA.java文件,然后编译并运行这个程序:


public class TestA {
  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;     /* 15 = 1111 */
     System.out.println("a >> 2  = " + c );
  
     c = a >>> 2;     /* 15 = 0000 1111 */
     System.out.println("a >>> 2 = " + c );
  }
}
Copy after login

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

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

逻辑运算符

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

OperatorDescriptionExample
&If corresponding If the bits are all 1, the result is 1, otherwise it is 0 (A&B), and you get 12, which is 0000 1100
|If they match If the corresponding bits are all 0, the result is 0, otherwise it is 1(A | B) to get 61, which is 0011 1101
^ If the corresponding bit values ​​are the same, the result is 0, otherwise it is 1(A ^ B) to get 49, that is, 0011 0001
~The bitwise complement operator flips each bit of the operand, that is, 0 becomes 1, and 1 becomes 0. (~A) gets -61, which is 1100 0011
A << 2 gets 240, which is 1111 0000
>> Bitwise right shift operator. The left operand is shifted bitwise right by the number of bits specified by the right operand. A >> 2 gets 15, which is 1111
OperatorDescriptionExample
&& is called logic AND operator. A condition is true if and only if both operands are true. (A && B) is false.
| | is called the logical OR operator. The condition is true if either of the two operands is true. (A | | B) is true.
! is called the logical NOT operator. Used to invert the logical state of the operand. If the condition is true, the logical NOT operator will get false. ! (A && B) is true.

实例

复制并粘贴下面的Java程序并保存为TestA.java文件,然后编译并运行这个程序:

public class TestA {
  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 = falsea || b = true!(a && b) = true
Copy after login

短路逻辑运算符

当使用与逻辑运算符时,在两个操作数都为true时,结果才为true,但是当得到第一个操作为false时,其结果就必定是false,这时候就不会再判断第二个操作了。

public class LuoJi{
    public static void main(String[] args){
        int a = 5;//定义一个变量;
        boolean b = (a<4)&&(a++<10);
        System.out.println("使用短路逻辑运算符的结果为"+b);
        System.out.println("a的结果为"+a);
    }
}
Copy after login

运行结果为:

使用短路逻辑运算符的结果为falsea的结果为5
Copy after login

解析: 该程序使用到了短路逻辑运算符(&&),首先判断 a<4 的结果为 false,则 b 的结果必定是 false,所以不再执行第二个操作 a++<10 的判断,所以 a 的值为 5。


赋值运算符

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

##<< =Left shift assignment operatorC << = 2 is equivalent to C = C << 2##>> =##& =Bitwise AND assignment operatorC&= 2 is equivalent to C = C&2^ =Bitwise XOR assignment operatorC ^ = 2 is equivalent to C = C ^ 2
OperatorDescriptionExample
=Simple assignment Operator, assigns the value of the right operand to the left operand C = A + B will assign the value obtained by A + B to C
+ = Additional assignment operator, which adds the left operand and right operand and assigns the value to the left operandC + = A is equivalent to C = C + A
- =Subtraction and assignment operator, which subtracts the left operand and the right operand and assigns them to the left operandC - = A is equivalent to C = C -
A
* =Multiplication and assignment operator, which multiplies the left operand and the right operand and assigns them to the left operandC * = A is equivalent to C = C * A
/ = division and assignment operator, which divides the left operand and the right operand and assigns them to the left operation The number C / = A is equivalent to C = C / A
(%)= modulus and assignment operator, which takes the left The operand and the right operand are modulo and assigned to the left operand C% = A is equivalent to C = C%A
Right shift assignment operatorC >> = 2 is equivalent to C = C >> 2
| =Bitwise OR assignment operatorC | = 2 is equivalent to C = C | 2

实例

复制并粘贴下面的Java程序并保存为TestA.java文件,然后编译并运行这个程序:

public class TestA {
  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 = 30c += a  = 40c -= a = 30c *= a = 300c /= a = 1c %= a  = 5c <<= 2 = 20c >>= 2 = 5c >>= 2 = 1c &= a  = 0c ^= a   = 10c |= 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;
      // 如果 a 等于 1 成立,则设置 b 为 20,否则为 30
      b = (a == 1) ? 20 : 30;
      System.out.println( "Value of b is : " +  b );
 
      // 如果 a 等于 10 成立,则设置 b 为 20,否则为 30
      b = (a == 10) ? 20 : 30;
      System.out.println( "Value of b is : " + b );
   }
}
Copy after login

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

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

instanceof 运算符

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

instanceof运算符使用格式如下:

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

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

下面是一个例子:

String name = "James";boolean result = name instanceof String; // 由于 name 是 String 类型,所以返回真
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。

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

##Press Bitwise AND&left to rightbitwise XOR^left to right ##Logical AND&&Left to rightLogical OR| |Left to rightCondition? :From right to leftAssignment= + = - = * = / =%= >> = << =&= ^ = | =right to left##comma The above is the JAVA tutorial | Chapter 5 Operation Conformity Set. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!
Category Operator Associativity
Suffix () [] . (dot operator) left to right
一元 + + -!~ Right to left
Multiplicative * /% Left to right
Additive + - Left to right
Shift >> > >> << Left to right
Relationship >> = << = Left to right
Equal == !=Left to right
Bitwise OR|Left to right
, left to right


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