Reprinted from: http://www.cnblogs.com/lanxuezaipiao/p/3460373.html
Every language is powerful, whether you are a beginner like me or a great God with an N-year project experience, there's always something you don't know. In terms of the language itself, such as Java, perhaps you have developed Java for several years, it can be said to be well-known to the heart, but you can guarantee the use of Java all you know? It's all right today. What are the hidden features in Java? Know can raise your hand oh ~ ~ ~
One, double parenthesis initialization Syntax (doublebraceinitialization) (this refers to curly braces {})
Mainly refers to the collection class (List,map,set, etc.), we create a constant set or pass a constant set as a parameter, often do this (in set as an example):
set<string> validcodes = new hashset<string> (); Validcodes.add ("xz13s"); Validcodes.add ("ab21/x"); Validcodes.add ("Yylex"); Validcodes.add ("ar2d"); Removeproductswithcodein (Validcodes);
Or initialize a constant collection in the class:
private static final set<string> valid_codes = new hashset<string> (); static { Validcodes.add ("xz13s"); Validcodes.add ("ab21/x"); Validcodes.add ("Yylex"); Validcodes.add ("ar2d"); }
Do you think it is time-consuming and laborious, in fact, there is a better way, that is the double parenthesis syntax, like this:
private static final set<string> valid_codes = new hashset<string> () {{ Add ("xz13s"); Add ("ab21/x"); Add ("Yylex"); Add ("ar2d"); }};//Or:removeproductswithcodein (New hashset<string> () {{ Add ("xz13s"); Add ("ab21/x"); Add ("Yylex"); Add ("ar5e"); }});
Here we explain these two parentheses: the first parenthesis creates a new anonymous inner class, which we all know; the second parenthesis declares an instance initialization block that runs when an anonymous inner class is instantiated.
There are two points to note when using the double-brace syntax:
1. If you want to establish an anonymous subclass in an anonymous inner class, you can only use it for a non-final class, which is obvious and not limited to the collection class, and can be used to instantiate any object, such as for a GUI object, as follows:
Add (New JPanel () {{ setlayout (...); SetBorder (...); Add (New JLabel (...)); Add (New JSpinner (...)); }});
2. This syntax is incompatible with the commonly used equals (Object O) method. For example, the example class has this method:
public boolean equals (Final Object o) { if (o = = null) { return false; } else if (!getclass (). Equals (O.getclas S ())) { return false; } else { Example other = (Example) o; Compare this to other. } }
Then, any objects created using the double-brace initialization syntax will not be equal to objects created without the use of the double-brace syntax. Therefore, it is recommended that you do not use this syntax if you want the Equals (Object O) method in your class. However, the collection class does not have this problem, it should be due to the internal optimization of the collection.
So when do you suggest using the double parenthesis syntax?
If you're just creating and initializing an instance instead of creating a new class, or creating any anonymous class that doesn't add field properties or overloaded methods, it's nice to use the double parenthesis syntax.
3. If you are using a collection class and the class has a constructor parameter that accepts another collection to generate an instance of the collection, then there is a better, more idiomatic alternative, as everyone knows the list initialization can be arrays.aslist (), as follows:
list<string> myList = new Arraylist<string> (Arrays.aslist ("One", "one", "one", "three");
Note, however, that Aslist returns a list of immutable lengths. How long the array is, and how long the list is converted to, we cannot add or remove to increase or decrease its length.
Ii. type parameter and Operation & (Typeparameterjointunion)
Is that parameters are bound to multiple types, such as:
public class Classname<t extends Class & Interface1 & Interface2 & ...> {}
Note: There is only the first class for extends, and all of the following & are interface interface, and the declaration or definition of class classes must precede interface.
For example: If you want a parameter that is both the comparable class and the collection class, the function is: whether the two given collection is equal or whether any of the two sets contain the specified element, you can do so with the following function:
public static <a, B extends Collection<a> & comparable<b>> boolean foo (b b1, b B2, a a) { return (B1.compareto (b2) = = 0) | | B1.contains (a) | | B2.contains (a);}
Here B1 and B2 can have both type collection and comparable types, so collection methods for contains classes can be used as well as comparable methods of CompareTo classes.
Third, VisualVM monitoring tools (Java VisualVM)
This is the JDK6.0 Update 7 comes with the monitoring tool (Java boot without the need for specific parameters, the tool in Bin/jvisualvm.exe), be able to monitor the thread, memory situation, view the method of CPU time and in-memory objects, the object has been GC, reverse view the allocated stack ( For example, 100 string objects are assigned by each of the objects).
Double-click to open, from the UI, this software is based on NetBeans development.
From the interface is still relatively concise, the left is a tree structure, automatically display the current native running Java program, you can also add a remote Java VM, where the PID in parentheses refers to the process ID. The Overview interface displays the VM startup parameters and some of the properties for that VM. The monitor interface monitors the Java heap size, PermGen size, classes, and number of threads. The profiler interface is interesting and seems to be able to dynamically tune a Java program. However, I did not try this function, feel to tune or in NetBeans is more natural, at least there is code, no code tuning is not very useful. Iv. Classpath supports wildcard characters (Setting the class path) This is a feature that is supported by Java 6, such as a configuration that is often used in engineering:
Java-classpath./lib/log4j.jar:./lib/commons-codec.jar:./lib/commons-httpclient.jar:./lib/ Commons-collections.jar:./lib/myapp.jar so. Main
More complex, but also error-prone, in fact, you can use the wildcard character more concise and convenient:
Java-classpath./lib/* so. Main
V. Covariant return type (covariant return types)
This is the functionality added by Java 5, and we cannot change the return type of the overridden method when we overwrite the method of the base class in the subclass before Java5, that is, the methods of the base class and the parent class must be exactly the same, and want to change can only be cast when the object is created. After Java 5, we can change, but it's important to note that the changed type must be a subtype of the original type. Let me give you an example.
public class Covariantreturntypestest {public static void Main (string[] args) { //TODO auto-generated method Stu b Mill m = new Mill (); Grain g = m.process (); System.out.println (g); Output:grain m = new Wheatmill (); g = m.process (); System.out.println (g); Output:wheat }}class Grain {public String toString () { return ' Grain '; }} Class Wheat extends Grain {public String toString () { return ' Wheat '; }} Class Mill { Grain process () { return new Grain ();} } Class Wheatmill extends Mill { //Here return type changed to Grain subtype Wheat Wheat process () { return new Wheat ()} }
Reference from: Hidden Features of Java
The things you don't know about Java-java hidden features