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

What are the skills of using double parentheses in Java

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article will explain in detail what the skills of using Java double parentheses are. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Review of Java double parenthesis skills

At first glance, the initialization of double parentheses in cgaolei's translation of Java skills is easy to see. I only know how to use it and do not take a closer look at the following explanation. Stunning, I thought to myself that Java had such a magical grammar and never knew about it. Because it is really convenient to initialize the collection. You used to use the commons-lang package and JDK's Arrays utility class to initialize the collection for some tests, but now you know you can do this:

Map map = new HashMap () {{put ("Name", "Unmi"); put ("QQ", "1125535");}}; List stooges = new ArrayList () {{add ("Larry"); add ("Moe"); add ("Curly");}}

It seems to be done in one sentence, rather than step-by-step:

Map map = new HashMap (); map.put ("Name", "Unmi"); map.put ("QQ", "1125535")

People who are not careful to understand it may think that it is some special grammar, the key is that the curly braces are linked together, and the original author is also posturing, calling it double parenthesis grammar (double-brace syntax). It is really indiscriminate spending to attract people's eyes, in fact, it is anonymous class plus initial block. The article explains: * * layer brackets actually define an internal anonymous class (Anonymous Inner Class), and the second layer parenthesis is actually an instance initialization block (instance initializer block), which is executed when the internal anonymous class is constructed.

So how to better understand it? It should be easier to understand if we write it in the following way. As a tip, press Ctrl + Shift + F on the * code in Eclipse as follows:

Map map = new HashMap () {{put ("Name", "Unmi"); put ("QQ", "1125535");}}

In fact, an anonymous class will create a subclass of HashMap. In the anonymous class, an initialization block enclosed by {} can naturally be placed in the initialization code. The code in the {} block is compiled into (), the constructor, so it can be used to initialize the instance. If you write it in the TestDoubleBrace class, you will see that the TestDoubleBrace$1.class file will be generated after compilation, and the decompiled file will contain:

Final class com.unmi.TestDoubleBrace$1 extends java.util.HashMap {/ / created a subclass of HashMap TestDoubleBracke$1 com.unmi.TestDoubleBrace$1 (); Code: 0: aload_0 1: invokespecial # 8; / / Method java/util/HashMap. "": () V / / {} put the code in the constructor 4: aload_0 5: ldc # 10; / / String Name 7: ldc # 12 / / String Unmi 9: invokevirtual # 14; / / Method put: (Ljava/lang/Object;Ljava/lang/Object;) Ljava/lang/Object; 12: pop 13: aload_0 14: ldc # 18; / / String QQ 16: ldc # 20; / / String 1125535 18: invokevirtual # 14; / / Method put: (Ljava/lang/Object;Ljava/lang/Object;) Ljava/lang/Object 21: pop 22: return}

So to put it bluntly, what double-bracketed syntax, ah, is that the code is not standardized, which makes it so puzzling. If you don't understand, list two more idiomatic codes:

JFrame frame = new JFrame (); frame.addMouseListener (new MouseAdapter () {public void mouseClicked (MouseEvent e) {/ / do womething here. }}); Thread thread = new Thread () {{/ / also learn to write this.setName ("job processing thread") with curly braces;} / / if you don't redefine the run () method, then the following curly braces can also join this with this public void run () {/ / do something here. }}; thread.start ()

There should be no problem, the above is the common writing of event listeners and multithreading, if he does not connect the curly braces together, but the standard code, I believe you will not have too much confusion about the so-called Double Brace Syntax at first. It is convenient to use this initialization method in the collection, but there are more anonymous classes for no reason.

At the beginning, I saw this kind of Java double bracket writing method is also regarded as the Bible, with only a little knowledge of it. Yesterday, when I used XStream to generate an object into a XML file, there was a List attribute. I borrowed this double parenthesis method to initialize the element. As a result, the XML file generated is out of shape, because XStream's Converter can handle ArrayList, but can not handle the anonymous subclass of the generated ArrayList very well. Therefore, I went back to seriously re-examine this so-called double-bracketed initialization syntax.

This is the end of this article on "what are the skills for using Java double parentheses". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

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.

Share To

Development

Wechat

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

12
Report