In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "what are the functions of Java?". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
1. Switch must add break before it ends.
For multiple branch selection, a series of if-else-if statements will make the readability of the code worse. It is recommended to use the switch statement instead. However, the branch judgment in switch case must add a break statement to abort the execution of other case, such as:
Int count = 1; switch (count) {case 1: System.out.println ("one"); case 2: System.out.println ("two"); case 3: System.out.println ("three");}
The above code outputs:
One two three
However, this is not what we want, or goes against our common sense. If a certain condition is met, of course, you only need to execute the logic under this condition, and other case should ignore it and skip it directly. Like the above code, you only need to output one.
Of course, adding break at the end of each case can achieve the desired effect. Java 12 coquettish switch can still play like this. I recommend this article.
This function point is slightly "deceptive", and it is also a common mistake made by beginners, so it is also honored to be on the list, ranking 10th.
2. The "short circuit" phenomenon of logical operators.
When using logical operators, we will encounter the phenomenon of "short circuit": once we can determine the value of the entire expression, the rest will not be calculated, of course, this function point is actually very useful, but for beginners, may feel surprised, improper use will have "tricks" consequences. For example, the following code:
Int num = 1; System.out.println (false & & ((num++) = = 1)); System.out.println (num)
False and 1 will be output, because the first half of the logic and & & is false, regardless of whether the second half is true or false, the entire expression will return false, so the latter part will not be evaluated. If you change false to true, the second half will be executed and num will become 2.
It ranks ninth on the list of "tricks".
3. Array subscript starts from zero
Java programmers know that the subscript of an array starts from zero. For example, if we want to traverse an array, we can do the following:
Int [] arr = new int [] {1 for 3, 5, 7, 9}; for (int item0, witi)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.