In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article focuses on "what are the new features of JDK1.5", friends who are interested may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn what are the new features of JDK1.5.
1. Generics (Generic)
C++ can specify the element type of the collection through template technology, while Java has no corresponding function until 1.5. A collection can put objects of any type, and we have to cast them when we take objects from the collection accordingly. Tiger introduces generics, which allow you to specify the types of elements in the collection, so you can get the benefit of strong typing for type checking at compile time.
Collection c = new ArrayList ()
C.add (new Date ())
The compiler gives an error:
Add (java.lang.String) in java.util.Collection cannot be applied to (java.util.Date)
2.For-Each cycle
The For-Each loop has to be added to simplify the traversal of the collection. Suppose we want to traverse a collection to do some processing on the elements in it. The typical code is:
Void processAll (Collection c) {
For (Iterator i=c.iterator (); i.hasNext ();) {
MyClass myObject = (MyClass) i.next ()
MyObject.process ()
}
}
Using the For-Each loop, we can rewrite the code as follows:
Void processAll (Collection c) {
For (MyClass myObject: C)
MyObject.process ()
}
This code is much clearer than above and avoids forced type conversions.
3. Automatic packing / unpacking (Autoboxing/unboxing) automatic packing / unpacking greatly facilitates the use of basic type data and their packaging classes.
Automatic packing: the basic type is automatically converted to the packaging class. (int > > Integer)
Automatic unpacking: the packaging class is automatically converted to the basic type. (Integer > > int)
Before JDK1.5, we were always obsessed with the fact that collections could not hold primitive types, but now the automatic conversion mechanism solves our problem.
Int a = 3
Collection c = new ArrayList ()
C.add (a); / / automatically converted to Integer.
Integer b = new Integer (2)
C.add (b + 2)
Here Integer is automatically converted to int for addition, and then int is converted to Integer again.
4. Enumeration (Enums)
JDK1.5 adds a whole new type of "class"-enumerated types. For this reason, JDK1.5 introduces a new keyword enmu. We can define an enumeration type in this way.
Public enum Color
{
Red
White
Blue
}
You can then use Color myColor = Color.Red.
Enumerated types also provide two useful static methods values () and valueOf (). We can easily use them, such as
For (Color c: Color.values ())
System.out.println (c)
5. Variable parameter (Varargs)
Variable parameters enable programmers to declare a method that accepts a variable number of parameters. Note that the variable argument must be the last parameter in the function declaration. Suppose we want to write a simple method to print some objects.
Util.write (obj1)
Util.write (obj1,obj2)
Util.write (obj1,obj2,obj3)
...
Before JDK1.5, we could do it with overloading, but this requires writing a lot of overloaded functions, which is not very efficient. If we use variable arguments, we only need one function.
Public void write (Object... objs) {
For (Object obj: objs)
System.out.println (obj)
}
With the introduction of variable parameters, Java's reflection package is also more convenient to use. For c.getMethod ("test", new Object [0]) .invoke (c.newInstance (), new Object [0]), we can now write c.getMethod ("test") .invoke (c.newInstance ()), which is much clearer than before.
6. Static Import (Static Imports)
To use static members (methods and variables) we must give the class that provides this method. Using static imports, you can make all static variables and static methods of the imported class directly visible in the current class, and you no longer need to give their class names to use these static members.
Import static java.lang.Math.*
…… .
R = sin (PI * 2); / / No need to write r = Math.sin (Math.PI)
However, overuse of this feature can also reduce the readability of the code to some extent.
At this point, I believe you have a deeper understanding of "what are the new features of JDK1.5?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.