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 > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the knowledge of "which is faster, for (;) or while (true) in 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!
Do a cursory search under JDK8u's jdk project:
Mymbp:/Users/me/workspace/jdk8u/jdk/src$ egrep-nr "for\\ (\\ sletters;\\ slots;". | wc-l 369mymbp nr while while\\ (true). | | wc-l 323 |
It's not that different.
Secondly, the source of for (;;) in Java. My personal view is that people who like to use this way of writing are influenced by the way they are written in C language. These people are not necessarily used to writing C before, but may be indirectly influenced by the teachers and seniors who used to write C.
In C, there are no built-in Bool / bool types or literal values of Bool / bool types such as TRUE / FALSE / true / false if you don't include some header files or declare them yourself. So, assuming that there are no include header files or define the above literals, one does not write the loop condition in while (…) The most common while statement in parentheses is as follows:
While (1) {/ *... * /}
But not everyone likes to see the magic number "1". Using for (;;) to express the condition of not writing a loop (that is, an infinite loop without break or goto in the loop body) is very intuitive, which is the function of the for statement itself, and there is no need to write any magic numbers.
Therefore, this method of writing has been handed down.
By the way, I tend to write while (true) in Java, but I don't mind others writing for (;;) in their own projects.
As for for (;;) in Java or while (true), which is faster?
The answer to all the questions that are not specified in this specification is "depends on implementation". After all, implementation only needs to ensure that the semantics conform to the specification, and efficiency is not within the scope of the specification. In terms of the implementation of Oracle/Sun JDK8u / OpenJDK8u, let's first look at the result of javac's compilation of the following two statements:
Public void foo () {int I = 0; while (true) {ionization;}} / * public void foo (); Code: stack=1, locals=2, args_size=1 0: iconst_0 1: istore_1 2: iinc 1, 1 5: goto 2
Vs.
Public void bar () {int I = 0; for (;;) {ionization;}} / * public void bar (); Code: stack=1, locals=2, args_size=1 0: iconst_0 1: istore_1 2: iinc 1, 1 5: goto 2
Even compilers like javac, which does almost nothing but constant folding as required by the Java specification, and a very small number of other optimizations, generate the same bytecode for both versions of the code.
This is the end of the content of "which is faster, for (;) or while (true) in Java". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.