Java basic syntax: operators

Time:2021-7-20

  • Java operators are classified according to their functions

    • Arithmetic operator+,-,*,/,%,++,–
    • Assignment Operators =
    • Relational operators>,=,<=,==,!=,instanceof
    • Logical operators&&,||,!
    • Bitwise Operators :&,|,^,~,>>,<>>
    • Conditional operator? :
    • Extended assignment operator:+=,-=,*=,/=,%=,<<=,>>=,&=,^=,|=
  • Operators are classified according to the number of operation data

    • Unary operator:++,–,!,~
    • Binary operator:+,-,*,/,%,=,>,=,<=,==,!=,instanceof,&&,||,&,|,,>>,<>>,+=,-=,*=,/=,%=,<<=,>>=,&=,=,|=
    • Ternary operator :? :
  • JavaMath classProvides a lot of scientific and engineering calculation methods and constants, special operations need to be applied to the method!


1、 Arithmetic operator

I. four operations

  • The four operators are binary operators with the following syntax:
    • Addition: VAR + var
    • Subtraction: var – var
    • Multiplication: VAR * var
    • Division: var / var

be careful:

  • During the operation, the data type with the highest level of type conversion is the data type of the operation result.
  • In integer operation, if the two data involved in the operation are byte or short type, the result of the operation is int type.
  • String connector: ‘+’ operatorAs long as one of the operands is of string type, the system will automatically convert the other to a string before connecting.

Example:

System.out.println(2 + 1);//3
System.out.println(2 - 1);//1
System.out.println(2 * 1);//2
System.out.println(2 / 1);//2
System.out.println(2 + (double)1);//3.0


II. Modular operation

  • Modular operation is a binary operator, syntax: VAR% var

  • Its operands can be floating-point numbers, generally using integers.

  • The modular operation is similar to the remainder operation, and the result of the operation isThe redundant value after the previous value is divided by the next value。 The difference lies in the operation of division of negative integers

    • Negative% negative = negative
    • Negative% positive = negative
    • Positive% negative = positive
    • Positive% positive = positive

Example:

System.out.println(-3 % -2);//-1
System.out.println(-3 % 2);//-1
System.out.println(3 % -2);//1
System.out.println(3 % 2);//1


III. self increase and self decrease

  • The autoincrement (+ +) autodecrement (- -) operator is a unary operator.
  • Divided intoPrefix add subtractandSuffix add subtractThere are two kinds of prefixes: the prefixes are added or subtracted first, and then the expressions are calculated; Suffixes are first operated by expressions, and then by self increment or self decrement.
  • Its operands can be floating-point numbers, generally using integers.

Grammar:

  1. Prefix auto increment: + + var
  2. Prefix subtraction: – – var
  3. Suffix auto increment: VAR++
  4. Suffix minus: VAR–

Example:

int num = 1;
System.out.println(num++);//1
System.out.println(num);//2
System.out.println(num--);//2
System.out.println(num);//1
System.out.println(++num);//2
System.out.println(num);//2
System.out.println(--num);//1
System.out.println(num);//1


2、 Assignment operator

  • ‘=’, a simple assignment operator that assigns the value of the right operand to the left.

Grammar:var = value


3、 Relational operators

  • The relational operator is used to compare two operands, and the return result bits are Boolean values.
  • This operator is used to manipulate an object instance to check whether the object is of a specific type (class type or interface type).

Grammar:

  1. Is it equal:var == var
  2. Is it unequal:var != var
  3. Is it greater than:var > var
  4. Is it less than:var < var
  5. Is it greater than or equal to:var >= var
  6. Is it less than or equal to:var <= var
  7. Instanceof operator:object instanceof type

Example:

System.out.println(1 == 2);//false
System.out.println(1 != 2);//true
System.out.println(1 > 2);//false
System.out.println(1 < 2);//true
System.out.println(1 >= 2);//false
System.out.println(1 <= 2);//true
System.out.println("" instanceof String);//true


4、 Logical operators

  • Logical operators are used to operate on Boolean data, and the operation result is also Boolean.
  • Logical and operator (&) and logical or operator (|) are binary operators, while logical non operator (!) Is a unary operator.
  • Logical and operator, if and only if both operands are true
  • Logic or operator. If either of the two operands is true, the result is true
  • Logical non operator, used to reverse the logical state of the operandsIf the operands are true, the logical non operator gets false.

Grammar:

  1. logical and operator :booleanVar && booleanVar
  2. logical or operator :booleanVar || booleanVar
  3. Logical non operator:!booleanVar

be careful:

  • Logic and operation and logic or operation are adoptedshort circuitHow to do this:
    1. In logical and (&) operation, if the first operand is false, the Boolean value judgment of the second operand will be skipped and the result will be false directly.
    2. In logical or (|) operation, if the first operand is true, the Boolean value judgment of the second operand will be skipped and the result will be true directly.

Example:

System.out.println(true && true);//true
System.out.println(true && false);//false
System.out.println(false && false);//false
System.out.println(true || true);//true
System.out.println(true || false);//true
System.out.println(false || false);//false
int num = 1;
System.out.println(false && (num++ > 0));//false
System.out.println(num);//1
System.out.println(true || (num++ > 0));//true
System.out.println(num);//1


5、 Bitwise operator

  • Bit operators are used for int, long, short, char, and byte types.
  • Bit operators act on all bits and operate bit by bit.
  • ‘~’ is a unary operator, and the rest are binary operators.
  • Bit operation is one and two binary operation of bit mode according to bit or binary number in program design. In many old microprocessors, bit operation is slightly faster than addition and subtraction operation, and generally bit operation is much faster than multiplication and division operation. In modern architectures, this is not the case: bit operations are usually as fast as addition operations (still faster than multiplication operations).

effect:

  1. And (&): if the corresponding bits are all 1, the result is 1, otherwise it is 0.
  2. Or (|): if the corresponding bits are all 0, the result is 0, otherwise it is 1.
  3. XOR (^): if the corresponding bit values are the same, the result is 0, otherwise it is 1.
  4. Non (~): the bitwise negation operator flips each bit of the operand, that is, 0 becomes 1, and 1 becomes 0.
  5. Shift left (<: bit by bit shift left operator, left operand, bit by bit shift left, the number of bits specified by the right operand.
  6. Move right (>): bitwise shift right operator, the left operands shift the bits specified by the right operands bitwise.
  7. Shift right without sign (>): shift the value of the left operands to the right according to the number of bits specified by the right operands, and the empty bits are filled with zeros.

Grammar:

  1. And (&):var & var
  2. Or (|):var | var
  3. XOR (^):var ^ var
  4. Non (~):~var
  5. Shift left (< var < integer
  6. Move right (>):var >> integer
  7. Shift right without sign (>):var >>> integer

Example:

int num60 = 0B0011_1100;
int num13 = 0B0000_1101;
System.out.println(num60 & num13);//12:0000 1100
System.out.println(num60 | num13);//61:0011 1101
System.out.println(num60 ^ num13);//49:0011 0001
System.out.println(~num60);//-61:1100 0011
System.out.println(num60 << 2);//240:1111 0000
System.out.println(num60 >> 2);//15:1111
System.out.println(num60 >>> 2);//15:0000 1111


6、 Conditional operator

  • Ternary conditional operator, there are three operands, and need to determine the value of Boolean expression.
  • The main purpose of this operator is to determine which value should be assigned to the variable as the result.

Grammar:type var = expression ? valueIfTrue : valueIfFalse

Example:

int number = 1;
int numResult = number > 0 ? 10 : 20;
System.out.println(numResult);//10
numResult = number < 0 ? 10 : 20;
System.out.println(numResult);//20


7、 Extended assignment operator

  • Assignment operators for simplifying code.
  • When the operation result of ordinary binary operator will be assigned to left operands, it can be simplified by using extended assignment operator.

effect:

  1. Addition and assignment operator (+ =): adds the left and right operands to the left. Equivalent to: varleft = varleft + varright
  2. Subtraction and assignment operator (- =): assigns a value to the left operand by subtracting the left and right operands. Equivalent to: varleft = varleft – varright
  3. Multiplication and assignment operator (* =): multiplies the left and right operands to assign a value to the left. Equivalent to: varleft = varleft * varright
  4. Division and assignment operators (/ =)Divide the left and right operands and assign a value to the left. Equivalent to: varleft = varleft / varright
  5. Modulus and assignment operators (% =): the left and right operands are modulated and assigned to the left operands. Equivalent to: varleft = varleft% varright
  6. Left shift assignment operator (< =): to shift the value of the left operand to the left and then assign it to the left. Equivalent to: varleft = varleft < varright
  7. Right shift assignment operator (> =): to shift the value of the left operand to the right and assign it to the left. Equivalent to: varleft = varleft > > varright
  8. Bitwise and assignment operators (& =): assign the left and right operands to the left operands bit by bit. Equivalent to: varleft = varleft & varright
  9. Bitwise XOR assignment operator (^ =): assign the left and right operands to the left operands after bitwise XOR. Equivalent to: varleft = varleft ^ varright
  10. Bitwise or assignment operator (| =): assign the left and right operands to the left operands bit by bit or after. Equivalent to: varleft = varleft | varright

Grammar:

  1. Addition and assignment operator (+ =):varLeft += varRight
  2. Subtraction and assignment operator (- =):varLeft -= varRight
  3. Multiplication and assignment operator (* =):varLeft *= varRight
  4. Division and assignment operators (/ =):varLeft /= varRight
  5. Modulus and assignment operators (% =):varLeft %= varRight
  6. Left shift assignment operator (< =):varLeft <<= varRight
  7. Right shift assignment operator (> =):varLeft >>= varRight
  8. Bitwise and assignment operators (& =):varLeft &= varRight
  9. Bitwise XOR assignment operator (^ =):varLeft ^= varRight
  10. Bitwise or assignment operator (| =):varLeft |= varRight

Example:

int numberLeft = 1;
int numberRight = 2;
System.out.println(numberLeft += numberRight);//3
System.out.println(numberLeft -= numberRight);//1
System.out.println(numberLeft *= numberRight);//2
System.out.println(numberLeft /= numberRight);//1
System.out.println(numberLeft %= numberRight);//1
System.out.println(numberLeft <<= numberRight);//4
System.out.println(numberLeft >>= numberRight);//1
System.out.println(numberLeft &= numberRight);//0
System.out.println(numberLeft ^= numberRight);//2
System.out.println(numberLeft |= numberRight);//2


8、 Operator priority

  • When multiple operators appear in an expression, who comes first and who comes second is related to the priority of operators.
  • In actual use, it can be used moreparenthesesDistinguish the priority of operators in a long string of expressions to improve the readability of the code.

List (lower priority below)

category Operator Relevance
suffix () []. (dot operator) From left to right
one yuan expr++ expr– From left to right
one yuan ++expr –expr + – ~ ! From right to left
Multiplicity * / % From left to right
Additive + – From left to right
displacement >> >>> << From left to right
relationship > >= < <= From left to right
equal == != From left to right
Bitwise AND & From left to right
Bitwise XOR ^ From left to right
Bitwise OR | From left to right
Logic and && From left to right
Logical or || From left to right
condition ?: From right to left
assignment = += -= *= /= %= >>= <<= &= ^= |= From right to left
comma , From left to right

Recommended Today

Front end 123: how browser caching works

Workflow for browser caching Obtaining content through the network is slow and expensive. Large responses require multiple round-trip communications between the client and the server, which will delay the time for the browser to obtain and process content, and increase the traffic cost of visitors. Therefore, the ability to cache and reuse previously acquired resources […]