Refactoring the Java Foundation (2) | Talking about Java Operators

1. Operators in java

Operators in Java are divided into: arithmetic operators, shift operators, comparison operators, bit operators, logical operators, conditional operators, assignment operators. Next, we will explain the above operators one by one.

1.1 Arithmetic operators

	+ - * / %   %:任何整数模2不是0就是1,所以只要改变被模数就可以实现开关运算。
	+:连接符。
	++,--

1.1.1 Addition and subtraction operators

Perform addition and subtraction operations on data of basic numeric types, and the data type of the result depends on the data types of the two operands.
When the two operand types are char, byte, short and int, the data type of the operation result must be int.
"+" is also used to connect two objects, and will be used to connect two strings.

int a=a+1;//把(a+1)的值赋值给a
String str=”abc”+”bcd”;//连接两个字符串

1.1.2 Multiplication Operator

Operators are used to multiply data of two basic numeric types. The data type of the operation depends on the data types of the two operands.Note: When the two operand types are char, byte, short, or int, the data type of the operation result must be int.

int a=b*c; // b=10 c=10   结果 :a=100

1.1.3 Division operator

The division operation is used to divide the type data of two basic values. When the two operands of the division are integers, if the operation result is an integer, the operation result is returned as the return value; if the operation result is not an integer, the integer part of the operation result is returned as the return value. When only one of the two operands of the division is a floating-point number, the result of the operation must be a floating-point number. When the value of the right operand in the division operation is 0, if the operands are all integers, an ArithmeticException will be thrown; if at least one of the operands is a floating-point number, the result is an Infinity value.

int a=b/c; // b=10,c=10  结果:a=1

1.1.4 Modulo operator

The modulus operation% is used to obtain the remainder of the division of two numbers from the division. The operand of the modulus operation can be either an integer or a floating-point number. The modulo process is actually to subtract the right operand from the left operand continuously until the result of the subtraction is less than the right operand, the result at this time is remainder. This process is suitable for floating-point numbers and negative numbers in the operand. When the right operand takes the value 0 in the modulus operation, if the operands are all integers, an ArithmeticException will be thrown, because the modulus operation is ultimately an arithmetic division operation; if the operand has at least one floating-point number, the result is It is a NAN (Not a Number) value. The sign of the result of the modulo operation completely depends on the left operand, that is, it is consistent with the sign of the left operand.

int a=b%c; // b=10,c=10 结果:a=0

1.2 Shift operator

Java defines several bit operators, namely the signed left shift operator <<, the signed right shift operator >> and the unsigned right shift operator >>>, which can be applied to integer types, long Type, integer, short integer, character and byte. Bit operators act on bits and perform bit-by-bit operations

1.2.1 Left shift operator

The left shift operator is represented by "<<", which moves the object on the left side of the operator to the left by the specified number of digits on the right side of the operator, and fills in the low bits with zeros. In fact, shifting to the left by n bits is equivalent to multiplying by 2 to the nth power

public class data{
    
    
public static void main(String[] args){
    
    
int a=2;
int b=2;
system.out.println("a 移位的结果是:"+(a<<b));}
}
// a 移位的结果是:8

1.2.2 Right shift operator

The right shift operator is represented by the symbol ">>>", which moves the object on the left of the operator to the right by the number of digits specified on the right of the operator, and adds 0 to the high digit. In fact, shifting to the right by n bits is equivalent to dividing by 2. n power.

public class data{
    
    
public static void main(String[] args){
    
    
       int a=16;
       int b=2;
       System.out.println("a 移位的结果是:"+(a>>>b));
}}
// a 移位的结果是:4

1.2.3 Signed right shift operator

The signed right shift operator is represented by the symbol ">>", which moves the operand on the left of the operator to the right by the specified number of bits on the right of the operator. If it is a positive number, add zero in the high position, if it is a negative number, add one in the high position

public class data17{
    
    
public class data19{
    
    
public static void main(String[] args){
    
    
       int a=16;
       int c=-16;
       int b=2;
       int d=2;
       System.out.println("a 的移位结果:"+(a>>b));
       System.out.println("c 的移位结果:"+(c>>d));
  }
}


//结果
a 的移位结果:4
c 的移位结果:-4


1.3 Comparison operators

The Java language provides a total of 6 comparison operators, which are less than, less than or equal to, greater than, greater than or equal to, equal to, and not equal to. These 6 comparison operators are suitable for comparisons between all basic types of values, but Boolean values ​​can only be compared with Boolean values. The result of the comparison operation is a Boolean value, and the entire comparison expression can be used as the operand of the logical operator In addition to the comparison between basic types of values, it can also be used for comparison between object variables. Character values ​​can be used as comparison operands to compare with other basic types of values ​​(except Boolean values), because each character has a corresponding standard character code. When the character value is compared, the actual comparison is an integer character code corresponding to the character.

特点:The characteristic of this operator is: the result of the operation is either true or false.

  1. == and != operators
    ==and != operators: the relational operators equal to (==) and not equal to (!=) can be used not only for comparisons between basic types, but also between reference types , But the comparison between the reference type is different from the comparison between the basic type values, it is the reference value of the object, that is, the memory address. The equality comparison between two reference variables is to determine whether the two variables refer to the same object. If you need to determine whether the contents of two objects are equal, you should use the equals() method. The equals() method defined in java.lang.Object is used to compare whether the contents of two objects are equal.
int a=1,b=1;
a==b的结果是true
String s1="123",s2="123";
s1==s2的结果是true
String s1="123";String s2=new String("123");
s1==s2的结果是false;
  1. > 或 >=Operator Operator
    > 或 >=: corresponding to greater than and greater than or equal, and the return value is true or false according to the result of the comparison
Boolean a=c<b; //  c=20,b=10 结果:a=false 

3. < 或 <=Operator Operator
< 或 <=: corresponding to less than and less than or equal to, and the return value is true or false according to the result of the comparison.

Boolean a=c>b; //c=20,b=10  结果:a=true

1.4 Bitwise operators

Java language provides 4 kinds of bitwise operators, namely bitwise AND (&), bitwise OR (|), bitwise exclusive OR (^) and bitwise NOT ( ), these four operators are suitable for all integers Type data. When performing bitwise operations, always convert character, byte, and short integer values ​​to integers before performing bitwise operations. For Boolean operations, only bitwise negation ( ) cannot be used for Boolean values. The other three types of bit operations can treat Boolean values ​​as a one-bit value for bitwise operations. Boolean value true corresponds to 1, and false corresponds to 0. Although bit operations can be applied to Boolean values, you cannot mix Boolean values ​​with other integer values. If they are mixed, a type conversion error will occur, because the Boolean type Value and other basic types cannot be converted to each other.

1.4.1 AND operation

& Operator: The & (and) operator performs operations on the corresponding bits in the two integer operands. See the table below for details:

X bit Y bit result
1 0 0
0 1 0
0 0 0
1 1 1

1.4.2 OR operation

| Operator: The | (or) operator performs operations on the corresponding bits in the two integer operands. See the table below for details:

X bit Y bit result
1 0 1
0 1 1
0 0 0
1 1 1

1.4.3 XOR operation

^ Operator: The ^ (exclusive OR) operator performs operations on the corresponding bits in the two integer operands. See the table below for details:

X bit Y bit result
1 0 1
0 1 1
0 0 0
1 1 0

1.5 Logical operators

There are three types of logical operators:! (Logical negation), && (logical AND), || (logical OR). Their operand type must be a Boolean value or Boolean expression.

1.5.1! Operator

public class data{
    
    
public static void main(String[] args)
  int a=2;
  System.out.println("a 非的结果是:"+(~a));
  }
}

1.5.2 && Operator

&& Operator: A formula similar to bitwise AND. The left and right operands must be a Boolean value or a Boolean expression. This operator has a logical short circuit phenomenon: A&&B&&C. If A is false, then the following expression will not be calculated;

public class data{
    
    
public static void main(String[] args){
    
    
 int a=129;
 int b=128;
 System.out.println("a 和b 与的结果是:"+(a&b));
}}

// 运行结果
a 和b 与的结果是:128

1.5.3 || Operator

|| Operator: a formula similar to bitwise AND. The left and right operands must be a Boolean value or a Boolean expression. This operator has a logical short-circuit phenomenon: A||B||C. If A is true, then the following expression will not be evaluated.

public class data{
    
    
public static void main(String[] args)
 int a=129;
 int b=128;
 System.out.println("a 和b 或的结果是:"+(a|b));
  }
}

// 运行结果
a 和b 或的结果是:129

1.6 Conditional Operator

The conditional operator is the only ternary operator (three operands) in the Java language, which is equivalent to if...else. Its format:

条件值?表达式1: 表达式2

The type of the value of the entire expression, the value of expression 1, and the value of expression 2 must be consistent. When the condition value is true, then the final result of the entire expression is the result of expression 1; if it is false, the final result is the result of expression 2. The final result type of the conditional operator is determined by two sub-expressions, and type conversion is performed between the two sub-expressions. The conversion rule satisfies the conversion to a wide range of values.
Examples:

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

1.7 Assignment operator

Assignment operators are used to assign specific values ​​to variables.
Assignment operators are: =, +=, -=, *=, /=, %=, >>=, <<=, &=, ^=, |=
Format:

类型  变量名=

1.8 Compound operators

The compound operator can be combined with the assignment operator in different ways. The combination details are as follows:

Compound operator meaning Compound operator meaning
+= a+=b is equal to a=a+b <<= a<<=b等于a=a<<b
-= a-=b is equal to a=ab >>>= a>>>=b等于a=a>>>b
*= a*=b is equal to a=a*b &= a&=b等于a=a&b
/= a/=b is equal to a=a/b |= a |= b is equal to a = a |b
%= a%=b is equal to a=a%b ^= a^=b等于a=a^b
>>= a>>=b等于a=a>>b ~= a~=b等于a=a~b

2. Operator precedence

Operators in the Java language have different levels. Operators with higher precedence are run first. The levels of specific operators are arranged as follows:

Single-operand operator> arithmetic operator> shift operator> comparison operator symbol> bit operator> logical operator> conditional operator> assignment operator

3. Summary

  1. Arithmetic operators:
    +-* /% %: Any integer modulo 2 is either 0 or 1, so just change the modulus to achieve switching operations.
    +: connector.
    ++,–
  2. Assignment operator:
    = += -= *= /= %=
  3. Comparison operator:
    Features: The feature of this operator is: the result of the operation is either true or false.
  4. Logical operators:
    & | ^! && ||
  • Except for !, logical operators are used to connect two boolean expressions.
  • &: Only both sides are true and the result is true. Otherwise it is false.
  • |: As long as both sides are false, the result is false, otherwise it is true
  • ^: XOR: and or is a bit different.
  • If the results on both sides are the same, it is false.
  • If the results on both sides are different, it is true.
  1. The difference between & and &&:
    &: No matter what the result on the left is, the right is involved in the operation.
    &&: Short-circuit AND, if the left side is false, then the right side does not have an AND operation.

  2. | And || Difference: |: Operate on both sides.
    ||: Short-circuit OR, if the left side is true, then the right side does not participate in the operation.

  3. Bit operators: operators used to manipulate binary bits.
    & | ^
    << >> >>> (unsigned right shift)

The first article: Refactoring Java basics (1) | Understanding constants and variables in java & java

Guess you like

Origin blog.csdn.net/weixin_43853746/article/details/107726119