Java syntax based operator learning notes sharing

1、 Operator

Operators include the following:

Arithmetic operator assignment operator comparison operator logical operator bit operator ternary operator

Bit operators are the least commonly used, but they are also the closest to the bottom of the computer.

1. Arithmetic operator

(1) Several uses of +: addition, positive number, string connector

(2) When dividing, you should pay attention to one problem: if you divide integers, you can only get integers. To get decimals, you can * 1.0 the data itself, that is, convert the data itself to floating-point type first.

2. Assignment operator

Symbol = + = - = * = / =%=

Note: = is the basic assignment operator, and others are extended assignment operators

Interview questions:

(1)short s=1,s = s+1;

(2) short s=1,s+=1;

Are there any problems with the above two codes? If so, where are the problems?

Answer: the code (1) is wrong and will lose precision. When defining byte and short, they actually receive a value of type int. this is a data detection done by themselves. If it is no longer within their range, an error will be reported. The effect is as follows:

Code (2) has no error because the extended assignment operator actually implies a cast.

That is, I + = 1; Not equivalent to I = I + 1; It is equivalent to I = (data type of I) (s + 1);

3. Relational operator

Note 1: the results of comparison operators are Boolean, that is, they are either true or false.

Note 2: the comparison operator "= =" cannot be written as "=". give an example:

4. Logical operator

(1) Logical operators are used to connect Boolean expressions. They cannot be written as 3 < x < 6 in Java, but should be written as x > 3 & x < 6.

(2) The difference between "&" and "& &"? Similarly, the difference between "|" and "|"?

A: The end result is the same. For example, a and B must be true at the same time, and the results of a & B and a & & B are true. B: & & has short circuit effect, false on the left and not executed on the right|| It has a short circuit effect. It is true on the left and not executed on the right.

Note: common logical operators in development: & &, |, |,!

(3) The difference between XOR (^) and or (|) is that when both left and right are true, the result of XOR is false.

5. Bitwise operators:

Although it is not used much in development, it will be seen in many source codes, because the computing at the bottom of the computer is bit computing.

Interview question 1: realize the exchange of two integer variables

The code is as follows:

Interview question 2: please write the result of multiplying 2 by 8 in the most efficient way.

Answer: 2 * 8 is equivalent to 2 < < 3

Knowledge review:

< <: move the left highest bit to discard, and fill 0 on the right > >: move the right highest bit to 0, and fill 0 on the left; The highest is 1, and the left is filled with 1 > > >: move the unsigned right, and fill 0 on the left no matter whether the highest bit is 0 or 1

6. Ternary operator:

Format: (relational expression)? Expression 1: expression 2;

If the condition is true, the result of the operation is expression 1;

If the condition is false, the result of the operation is expression 2;

Example:

Get the largest of two numbers:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>