Learn java Lesson 9: operators (2)

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


【 Related links 】

Last one : Xiaobaixue Java The first 8 course : Operator ( One )

Next : Xiaobaixue Java The first 10 course :if Conditional statements

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.

 

【 Related links 】

Last one : Xiaobaixue Java The first 8 course : Operator ( One )

Next : Xiaobaixue Java The first 10 course :if Conditional statements

general programme : Returns a list of

 

版权声明
本文为[zyqok]所创,转载请带上原文链接,感谢

  1. [front end -- JavaScript] knowledge point (IV) -- memory leakage in the project (I)
  2. This mechanism in JS
  3. Vue 3.0 source code learning 1 --- rendering process of components
  4. Learning the realization of canvas and simple drawing
  5. gin里获取http请求过来的参数
  6. vue3的新特性
  7. Get the parameters from HTTP request in gin
  8. New features of vue3
  9. vue-cli 引入腾讯地图(最新 api,rocketmq原理面试
  10. Vue 学习笔记(3,免费Java高级工程师学习资源
  11. Vue 学习笔记(2,Java编程视频教程
  12. Vue cli introduces Tencent maps (the latest API, rocketmq)
  13. Vue learning notes (3, free Java senior engineer learning resources)
  14. Vue learning notes (2, Java programming video tutorial)
  15. 【Vue】—props属性
  16. 【Vue】—创建组件
  17. [Vue] - props attribute
  18. [Vue] - create component
  19. 浅谈vue响应式原理及发布订阅模式和观察者模式
  20. On Vue responsive principle, publish subscribe mode and observer mode
  21. 浅谈vue响应式原理及发布订阅模式和观察者模式
  22. On Vue responsive principle, publish subscribe mode and observer mode
  23. Xiaobai can understand it. It only takes 4 steps to solve the problem of Vue keep alive cache component
  24. Publish, subscribe and observer of design patterns
  25. Summary of common content added in ES6 + (II)
  26. No.8 Vue element admin learning (III) vuex learning and login method analysis
  27. Write a mini webpack project construction tool
  28. Shopping cart (front-end static page preparation)
  29. Introduction to the fluent platform
  30. Webpack5 cache
  31. The difference between drop-down box select option and datalist
  32. CSS review (III)
  33. Node.js学习笔记【七】
  34. Node.js learning notes [VII]
  35. Vue Router根据后台数据加载不同的组件(思考-&gt;实现-&gt;不止于实现)
  36. Vue router loads different components according to background data (thinking - & gt; Implementation - & gt; (more than implementation)
  37. 【JQuery框架,Java编程教程视频下载
  38. [jQuery framework, Java programming tutorial video download
  39. Vue Router根据后台数据加载不同的组件(思考-&gt;实现-&gt;不止于实现)
  40. Vue router loads different components according to background data (thinking - & gt; Implementation - & gt; (more than implementation)
  41. 【Vue,阿里P8大佬亲自教你
  42. 【Vue基础知识总结 5,字节跳动算法工程师面试经验
  43. [Vue, Ali P8 teaches you personally
  44. [Vue basic knowledge summary 5. Interview experience of byte beating Algorithm Engineer
  45. 【问题记录】- 谷歌浏览器 Html生成PDF
  46. [problem record] - PDF generated by Google browser HTML
  47. 【问题记录】- 谷歌浏览器 Html生成PDF
  48. [problem record] - PDF generated by Google browser HTML
  49. 【JavaScript】查漏补缺 —数组中reduce()方法
  50. [JavaScript] leak checking and defect filling - reduce() method in array
  51. 【重识 HTML (3),350道Java面试真题分享
  52. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  53. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  54. [re recognize HTML (3) and share 350 real Java interview questions
  55. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  56. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  57. 【重识 HTML ,nginx面试题阿里
  58. 【重识 HTML (4),ELK原来这么简单
  59. [re recognize HTML, nginx interview questions]
  60. [re recognize HTML (4). Elk is so simple