Java Basics - Operators

Table of contents

arithmetic operator

assignment operator

comparison operator

Logical Operators

conditional operator (?:)

instanceof operator

Java operator precedence

Some special symbols often appear in programs, such as +, -, *, =, >, etc. These special symbols are called operators. Operators are used to perform arithmetic operations, assignment operations, and comparison operations on data. In Java, operators can be divided into arithmetic operators, assignment operators, comparison operators, logical operators, etc.

arithmetic operator

The most common mathematical operations are addition, subtraction, multiplication, and division, known as the four arithmetic operations. Arithmetic operators in Java are the symbols used to deal with the four arithmetic operations. Arithmetic operators are the simplest and most commonly used operation symbols.

Points to note when using arithmetic operators:

  1. When performing self-increment (++) and self-decrement (--) operations, if the operator ++ or -- is placed in front of the operand, the self-increment or self-decrement operation is performed first, and then other operations are performed. Conversely, if the operator is placed after the operand, other operations are performed first and then the self-increment or self-decrement operation is performed.
  2. When performing division, when both the divisor and the dividend are integers, the result is also an integer. If a decimal is involved in the division operation, the result will be a decimal. For example, 2510/1000 belongs to the division between integers, and the decimal part is ignored, the result is 2, and the result of 2.5/10 is 0.25.
  3. When performing modulo (%) operation, the positive or negative of the operation result depends on the sign of the modulus (the number on the left of %), and has nothing to do with the sign of the modulus (the number on the right of %). For example, (-5)%3=-2 and 5%(-3)=2.

assignment operator

The role of the assignment operator is to assign the value of a constant, variable, or expression to a variable.

Precautions when using the assignment operator:

  1. In Java, you can assign values ​​to multiple variables through an assignment statement. The specific examples are as follows:
    int  x, y, z; 
    x = y = z = 5;	          // 为三个变量同时赋值
    

    In the above code, an assignment statement assigns the values ​​of variables x, y, and z to 5 at the same time. It should be noted that the following writing method is not allowed in Java.

    int  x = y = z = 5;		// 这样写是错误的
    
  2. Except for "=", all others are special assignment operators. Taking "+=" as an example, x += 3 is equivalent to x = x + 3. The expression will first perform the addition operation x+3, and then perform the operation The result is assigned to the variable x. -=, *=, /=, %= assignment operators can all be deduced by analogy.

comparison operator

Comparison operators are used to compare two values ​​or variables, and the result of the comparison operation is a Boolean value, namely true or false.

Considerations when using comparison operators:

  1. In comparison operations, the comparison operator "==" cannot be mistakenly written as the assignment operator "=".

Logical Operators

Logical operators are used to operate on Boolean data, and the result is still a Boolean value.

Issues to be aware of when using logical operators:

  1. Logical operators can be used to operate on expressions that evaluate to Boolean values. For example, x > 3 && y != 0it means that when xit is greater than 3 yand not equal to 0, the result of the entire expression is true (true).
  2. The operators &and &&both represent logical AND (AND) operations, and they are functionally the same, that is, the result is true only when both operands are true; otherwise, the result is false. But there are certain differences in their use. When using &an AND operation, the expression on the right is evaluated regardless of whether the left is true or false. When using &&the AND operation, when the expression on the left is false, the expression on the right is no longer calculated, which is called short-circuit and.
  3. The operators |and ||both represent a logical or (OR) operation. The result is true when either expression on either side of the operator evaluates to true; the result is false only if both expressions evaluate to false. Similar to the logical AND operation, ||the operator is also short-circuiting or. When ||the expression on the left is true, the expression on the right is no longer evaluated.
  4. operator ^represents a logical exclusive-or (XOR) operation. When the Boolean values ​​on both sides of the operator are the same (both true or both false), the result is false; when the Boolean values ​​of the expressions on both sides are different, the result is true. The XOR operation can be used to determine whether the Boolean values ​​of two expressions are different.

conditional operator (?:)

The conditional operator (?:) is an operator that chooses to perform different actions in an expression depending on whether the condition is true or false.

The syntax is as follows:

condition ? expression1 : expression2
  • condition Is a conditional expression, usually a logical expression, used to determine whether the condition is true or false.
  • The value that is computed and returned if  condition true  expression1 .
  • If  condition false,  expression2 the value that is computed and returned.

Using conditional operators can provide concise conditional judgment and assignment functions. Here is an example:

int num = 7;
int result = (num > 5) ? 10 : 20;

In the above example, we first defined a variable numand assigned the value 7. Then, use the conditional operator to determine numwhether is greater than 5. If yes, assign the result a value of 10; otherwise assign the result a value of 20. Since the value of is 7, which is greater than 5, the final value of will be 10. num result

It should be noted that the two expressions of the conditional operator expression1and expression2should have the same type or have an implicit conversion relationship to ensure type compatibility.

There are a few things to keep in mind when using the ternary operator:

  1. The conditional operators "?" and ":" are a pair of operators, they cannot be used alone, they must be part of the whole expression.
  2. Conditional operators have lower precedence than relational and arithmetic operators, but higher than assignment operators. Therefore, in an expression, if there are mixed use of these operators, care should be taken to use parentheses to clearly control the order of operations.
  3. The conditional operator supports nesting, and the conditional operator can be used again in expression 2. In multi-level nesting, parentheses can be used as needed to clearly specify the order of operations. For example, a > b ? a : c > d ? c : d should be understood as a > b ? a : (c > d ? c : d), that is, return a when the first condition is true, otherwise judge the second condition .

instanceof operator

The instanceof operator is used to determine whether an object is an instance of a particular class or its subclasses.

Its grammatical form is as follows:

object instanceof Class
  • object is the object to be judged, and Class is the class name to be checked.
  • When using the instanceof operator, it checks whether the object is an instance of the Class class or its subclasses. If yes, the operator returns true; if not, it returns false.

Here is an example:

class Animal {}

class Dog extends Animal {}

class Cat extends Animal {}

Animal animal = new Animal();
Dog dog = new Dog();
Cat cat = new Cat();

System.out.println(animal instanceof Animal);  // 输出 true
System.out.println(dog instanceof Animal);     // 输出 true
System.out.println(cat instanceof Animal);     // 输出 true
System.out.println(dog instanceof Dog);        // 输出 true
System.out.println(cat instanceof Dog);        // 输出 false

In the above example, a parent class Animal and two subclasses Dog and Cat are defined. By using the instanceof operator, we can determine whether an object is an instance of a class or its subclasses.

Java operator precedence

When performing operations on some complex expressions, it is necessary to clarify the order in which all operators in the expression participate in the operation. This order is usually called the priority of the operator.

The operator with the highest precedence in the following table is at the top of the table, and the operator with the lowest precedence is at the bottom of the table.

Guess you like

Origin blog.csdn.net/m0_74293254/article/details/132239800