Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Common grammatical candy blocks in Java

2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Java in the common grammar candy, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

SyntacticSugar, also known as sugar-coated grammar, is a term coined by British computer scientist Peter.J.Landin to refer to a grammar added to a computer language, which has no effect on the function of the language, but is more convenient for programmers to use. In short, syntax candy makes the program more concise and more readable.

Interestingly, in the field of programming, in addition to grammatical sugar, there is also grammatical salt and grammatical saccharin, which is not expanded here because of limited space.

Many people say that Java is a "low-sugar language". In fact, all kinds of sugars have been added at the Java language level since Java7, mainly under the "ProjectCoin" project. Although some people in Java still think that the current Java is low in sugar, it will continue to develop towards "high sugar" in the future.

Interpretive sugar

As mentioned earlier, the existence of syntax sugar is mainly for developers' convenience. But in fact, the Java virtual machine does not support these syntax sugars. These grammatical sugars are reduced to a simple basic grammatical structure during the compilation phase, which is called paraphrase candy.

If you look at the source code of com.sun.tools.javac.main.JavaCompiler, you will find that one step in compile () is to call desugar (), which is responsible for the implementation of the paraphrase sugar.

Switch supports String and enumeration

As mentioned earlier, the syntax sugar in the Java language has gradually enriched since Java7, and one of the more important things is that switch in Java7 began to support String.

Under popular science before starting coding, swith in Java already supports basic types. Such as int, char and so on.

For the int type, make a numerical comparison directly. For the char type, compare its ascii code.

So, for compilers, only integers can be used in switch, and any type of comparison has to be converted to integers. Like byte. Short,char (the Aacki code is an integer) and int.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

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: 249

*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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report