Java Miscellaneous 2 - Operators and Expressions

operator

foreword

For java, there are many types of data, and there are many kinds of operations. This article will explain common operators.

There are:

  • Math operations: addition, subtraction, multiplication and division
  • Comparison operation: size comparison
  • Logical Operations: Boolean Operations

operator

assignment operator

In the previous article, we said that declaring a variable is equivalent to getting a piece of land in memory, so what is stored in this piece of land requires us to assign data to it. Assignment is to set the content of this location to a certain value of .

What we need to use to assign a variable is the assignment operator. The common assignment operator is =. Note: = is used for assignment in java.

math operator

Mathematical operators include addition, subtraction, multiplication and division, and the symbols are +-*/ respectively. There are also the modulo operator %, as well as the increment (++) and decrement (–) operators. The modulo operation applies to integer and character types, and other mathematical operations apply to all numeric and character types. Most of the cases of addition, subtraction, multiplication and division are intuitive and easy to understand. It should be noted that ++ – and += -= after combining with the assignment operator.

Note the range of types

When using integers for mathematical operations, we need to pay attention to the range of the type. Once the calculation result exceeds the representation range and is not received with the appropriate type, there will be problems. What do you mean? see the example below

~java
int a = 2147483647*2; //2147483647 is the maximum value that int can represent
~

The result of printing a at this time is -2. The solution is to use a long to receive the result.

Why is there such a situation?

We need to start with binary, there is a little more content, so we will not talk about the details. Simply put, it is binary addition and subtraction. When the calculation result exceeds the representation range, the highest bit is often 1, which will be regarded as a negative number.

automatic type promotion

When performing mathematical operations, we need to pay attention to the problem of automatic type promotion. We mentioned automatic type promotion when we explained data types before. This article will not explain too much.

  • If the value involved in the operation is double, the result is automatically promoted to double
  • If the value involved in the operation is float, the result is automatically promoted to float
  • If the value involved in the operation is long, the result is automatically promoted to long
  • In other cases (byte, short, char), the result is promoted to int by default
+= -=

When + and assignment = are combined, it is equivalent to assignment after operation. For example, a+=2 is equivalent to a=a+2. There is no difference in the results between these two methods, and they will not be able to be distinguished in the process of use. But there are still differences between them.

As we mentioned above, when the operation is performed, automatic type promotion will occur. Adding 10 to a of byte type will become int, and automatic type promotion will not occur by writing +=. Similarly, -= *= /= neither Automatic type promotion occurs.

Increment (++)/Decrement (–)

The JDK provides us with the auto-increment operator ++, the auto-decrement operator –, and the first thing to note is ++ – and automatic type promotion does not occur.

For example: a++ is equivalent to a+=1 is equivalent to a=a+1.

a++ is an expression , then a++there will be a calculation result of the expression, which is the old value of a (the value before adding 1). Conversely, ++athe value after adding 1 to the evaluation result of the expression. So their essential difference is: the value of the expression (the result of the operation) is the value of the variable before adding 1 or the value of the variable after adding 1 (the same is true for decrement). In other words: the value of a is indeed increased by 1 after the a++ calculation, but the overall value of the expression a++ is the value before the addition of 1.

a++ is not as fast as ++a ++a saves a register

In addition, self-increment and self-decrease are not thread-safe. The self-increment and self-decrement operator contains two operations: an operation of adding 1 (subtracting 1) and an operation of assignment, which is not an atomic operation. Therefore, under multi-threading, if you want to To implement self-increment and self-decrement operations on shared variables, it is necessary to add locks, or use the atomic self-increment and self-decrease methods provided by the atomic operation classes provided by JDK (such as AtomincInteger, etc.).AtomicLong

comparison operator

Also known as relational operators, they form Boolean expressions. Mainly used to calculate the relationship between two values, the result is a boolean value. Applies to all numeric and character types.

Comparison operators are: greater than (>), greater than or equal to (>=), less than (<), less than or equal to (<=), equal to (==), not equal to (!=).

The thing to note here is equals. Equal uses two equal signs == instead of one equal sign (=). An equals sign (=) represents an assignment operation.

Logical Operators

Logical operations generate a Boolean value true or false according to the logical relationship of the data. Logical operations can only be applied to data of type boolean, but the result of the comparison operation is a boolean value, so the comparison results of other types of data can be used for logical operations.

The logical operators are as follows:

  • And (&): Both are true to be true, as long as one of them is false, it is false
  • Or (|): as long as one of them is true, it is true, and both are false.
  • Not (!): For a variable, true will become false, false will become true
  • XOR (^): Two are the same as false, two are not the same as true
  • Short-circuit and (&&)
  • short circuit or (||)

Most of the logical operations are relatively intuitive, and the difference between & and &&, and | and || should be noted.

References

http://www.cnblogs.com/jinggod/p/8424808.html


I can't guarantee that every place is right, but I can guarantee that every sentence, every line of code has been scrutinized and considered. I hope that behind every article is my attitude of pursuing a purely technical life.

Always believe that good things are about to happen.

Guess you like

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