## Learn java Lesson 9: operators (2)

zyqok 2020-11-13 10:16:56
learn java lesson operators

general programme ： Returns a list of

【1】 review

Last time we talked about logical operators , Arithmetic operator , Auto increment and auto decrement operator , Comparison operators these , Let's review it in table form ： Everyone has learned mathematical operations , So all of the above are relatively simple , I believe everyone should have mastered .

Let's go on to understand ： Logical operators 、 An operator 、 Ternary operator . To understand this, you have to go around a little bit , But it's not hard , Follow the pace , Study slowly , And I'll understand .

Create a class Test09 【2】 Logical operators

The result of all logical operations will also be a Boolean value , namely true（ really ） and false（ false ）.

（1） Logic and （ && ）

This understanding is in our daily life “ also ” It means .

For example ： Xiao Ming can not only swim , And I can skate .

Then it can be expressed as ： Xiao Ming can swim && Xiao Ming can skate

He said && The symbols are all right （true） When , The final result is true.

therefore （ Xiao Ming can swim && Xiao Ming can skate ） The result is true, Because both Xiao Ming and Xiao Ming can .

If Xiao Ming can't fight the landlord ,（ Xiao Ming can swim && Xiao Ming will fight against landlords  && Xiao Ming will fight against landlords ） The result is false, Because Xiaoming had better not set up one of them , only && One of the links does not hold up as false Words , The whole result , It doesn't work （false）.

We use code to operate ： Copy the following code to main In the method .

``````// 10 > 9, establish ,x = true
boolean x = (10 > 9);
// 10 > 11, establish ,y = true
boolean y = (10 < 11);
// 10 == 8, Don't set up ,y = false;
boolean z = (10 == 8);
// Logic and computation , seek a
boolean a = (x && y);
// Logic and computation , seek b
boolean b = (x && y && z);
// Output a result
System.out.println("a : " + a);
// Output b result
System.out.println("b : " + b);``````

Execute code , View results ： You can see , Last  a The result is ture,b The result is false.

Why is this result , We can analyze ：

It's not hard for us to see ,x = true, y = true, z = false;

that （x && y ） Equivalent to （ture && true）, It's on both sides of the symbol true, therefore a The end result is true.

Empathy （x && y && z） amount to （ture && true && false）, The last one in the symbolic connection is false, Then the whole operation result is false, namely b As the result of the false.

（2） Logic or （ || ）

Logic or two vertical lines , His meaning and && It's a little antagonistic ,&& It's all the conditions of the connection , Then the final result holds ;  and || In the connection condition of , As long as there is one established , So the end result is true .

Let's go back to the code above , hold && Change it to || Run it ： It can be seen that , Now not only a by true 了 , Even b Also for the true 了 . because b Of the three conditions that participate in the operation , although z Don't set up , but x and y It's all established , As long as there is one, it will eventually be true , therefore b The result is true , namely true.

Next we try to modify the code ：

modify x and y The comparison operator between two numbers , take x and y It doesn't work , We implement the results ： You can see , here a and b All for false 了 , because a and b in All the conditions of the connection are false, None of them true, So the end result can only be false.

（3） Logic is not （ ! ）

Logic is not , The abbreviation is not , It's the opposite result , He wrote in the result of the calculation , namely !true The result is false, !false The result is true.

We can look at the following code ：

because x by true, that x After taking the wrong, it is false, therefore y The result is false. 【3】 An operator

This is the most convoluted bit operator , And in the actual development process , Usually used less , These are usually used in algorithmic research operations , You may not know much about algorithms and computer composition , I won't go into details here , It's like higher mathematics in mathematics , Only when you go to college will you have a chance to use .

Let's talk about the commonly used , So the bit operator is omitted , Interested students can search for relevant materials to study .

Although not , But the definition and symbols of bit operators are listed below ： 【4】 Ternary operator

The ternary operator is not a symbol , The second is a fixed operation format .

`````` Format ： Conditional expression ？ value 1 : value 2
explain ： When the conditional expression holds , Value 1 For the end result , When the conditional expression doesn't hold , Value 2 The final result is .``````

for instance ： If it doesn't rain tomorrow , Xiao Ming will go to grandma's house , Or stay at home .

Then we can use the ternary operator to express it as （ It's not going to rain tomorrow ？ Xiao Ming goes to grandma's house  ： Xiao Ming stays at home ）

Next , Let's get to know ：

If the following code runs , You can do it first z What's the result of ？

``````int x = 10;
int y = 8;
int z = (x > y ? 1 : 0);
System.out.println("z = " + z);``````

Execute code , The results are shown below , Are you doing the right thing ！ analysis ：x > y namely 10 > 8, This is true , So take the first value 1, so z The value of is 1.