In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how to use the goto sentence in Java". In the daily operation, I believe that many people have doubts about how to use the goto sentence in Java. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use the goto sentence in Java". Next, please follow the editor to study!
The wonderful use of Java goto sentence
Today, when I was chatting with a friend, I inadvertently talked about the goto sentence, but in Java, goto reserved the keyword, but my friend said that there were sentences in Java with similar effect to goto, but I had no idea! All of a sudden, I feel that Java has learned for nothing! My heart is surging!?
Immediately come back to check the information + write demo, and this kind of operation?! I cried.
Java label
As we all know, in Java, break, continue, and return can be used in the termination statements of for loops. But if it is more than two layers of for loops, want to jump from the inner layer to the outermost layer, that is, jump out of all for loops, the above three are not easy to implement. At this point, we can do this with the custom name +':'(colon). Look directly at the code:
Public class JavaLabel {public static void main (String [] args) {/ / you can customize the name outer: for (int I = 0; I) here
< 10; i++) { for (int j = 0; j < 10; j++) { if (j == 5) { break outer; } System.out.println(j); } } }} label 关键字可以配合 break 和 continue 使用,以上代码,只要进入 for 循环,就会直接终止整个两层 for 循环,实现了我们想要的结果。 反编译 我们再用 javap 工具反编译一下:You can see that the Java bytecode also uses goto at the bottom, so, after all, it still uses goto, falling leaves return to the roots!
Also calculate to increase the knowledge, thanks to my friend, hereby record.
"goto"-introduction to label (basic usage + example) 1.GoTo
In programming languages, goto exists from the beginning. When learning assembly language, there are program control statements for instruction jumps. in fact, program jumps are common in programming phonetics, such as if,for in languages such as cMagnec, languages, and so on. If you look at the assembly code generated by the final compilation, you will find that there is nothing special about it.
two。 So why does goto have such a bad reputation?
The bad reputation of goto statements is still due to the fact that goto statements jump at the source code level. Destroys the structured design style; often brings errors or hidden dangers, which may skip the construction of certain objects, initialization of variables, important calculations, and so on. So, if you use it carefully.
Goto-- tags in 3.java
Although there are some drawbacks to the goto statement, in fact goto works well at some point. Java takes a middle-of-the-road approach to this problem. Java does not have a goto, but adopts a label with the same mechanism.
Label:
While (or other loop statements)
Note: don't have any code between tags and iterations!
Java introduced tags to solve the problem of interrupting nested loops, because if we use nested loops, break and continue can only break the current loop, that is, only the inner loop, while the outer loop is still there, so let's test it.
Public class label {public static void main (String [] args) {int iTun9; while (I > 3) {while (I > 3) {if (I)
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.