[Java Basics - Java Operators]

 

 

 

Knowledge points: 1. Various operators 2. Operator precedence

1. Arithmetic operators

  1. Arithmetic operators: 

 

 

   Another simple way of writing

    For example: j = j + i ; can be written as j += i; similarly j -= i ; ......

1.2 Relational Operators

  Relational operators are used to compare operands. Assuming variables a=20 and b=10, then:

 

1.3 Logical Operators

Logical operators are used to describe AND, OR, and non- logical relationships. Assuming that variables a=true and b=false, then:

 

1.4 Bitwise operators

Bitwise operators can be applied to integer types, longs, ints, shorts, characters and bytes. It operates on bits, and performs bit-by-bit operations. The binary bit operation is detailed: how does binary perform bit operation
Assuming that the integer variable A=60 (0011 1100) and the variable B=13 (0000 1101), then:

 

 1.5 Assignment operator

The assignment operator is used for variable assignment, as follows:

1.6 Other operators_conditional operators

The conditional operator, also known as the ternary operator, can be used as a very special kind of assignment operator, which determines which values ​​should be assigned to a variable. Syntax:
variable x = (expression) ? value if true : value if false

The left side of the "?" is the conditional expression true or false. If true, assign the value on the left side of ":" to the variable on the left side of "="; if false, assign the value on the right side of ":" to the left side of "=" side variables.

 

Example:

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 );//结果:Value of b is : 30

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

1.7 Other operators _instanceof operator

The instanceof operator is only used for object reference variables, checking whether the object is of a specific type (class or interface type). Syntax:
( Object reference variable ) instanceof (class/interface type)

The result is true if the type of the value on the left matches the type of the class/interface on the right (including superclasses).

Example:

 

public class Test {

    public static void main(String args[]){
        String name = "James";
        boolean result = name instanceof String;
        System.out.println(result);//Result: true
    }
}

 

 

 

Example 2:

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);//Result: true
  }
}

 

 

2. Priority

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325256934&siteId=291194637