In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is to share with you how to understand the generics of the new features of JDK5.0. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article.
Use generics without parameters
Now that the collection types have been generalized in J2SE 5.0, what about the original code that uses these types? Fortunately, they will continue to work in JAVA 5 because you can use generics with no parameters. For example, you can continue to use the List interface as before, as in the following example.
List stringList1 = new ArrayList (); stringList1.add ("Java 1.0-5.0"); stringList1.add ("without generics"); String S1 = (String) stringList1.get (0)
A generic type without any parameters is called a raw type. It means that the code written for JDK1.4 or earlier will continue to work in java 5.
One thing to note, though, is that the JDK5 compiler wants you to use generics with parameters. Otherwise, the compiler will prompt a warning because he thinks you may have forgotten to define the type variable s. For example, you will see the following warnings when compiling the above code, because the first List is considered a prototype.
Note: com/brainysoftware/jdk5/app16/GenericListTest.java
Uses unchecked or unsafe operations.
Note: Recompile with-Xlint:unchecked for details.
If you don't want to see these warnings when you use a prototype, you have several options to achieve your goal:
1. Compile with the parameter-source 1.4
two。 Use @ SupressWarnings ("unchecked") annotations
3. Update your code and use List. An instance of List can accept any type of object, just like a prototype List. However, the compiler does not report an error.
Use? Wildcard character
As mentioned earlier, if you declare a List, then the List works on the aType, so you can store the following types of objects:
1. An example of aType
two。 An instance of its subclass (if aType is a class)
3. Class instance that implements the aType interface (if aType is an interface)
Note, however, that a generic type itself is a JAVA type, just like java.lang.String or java.io.File. Passing different type variables to generics can create different JAVA types. For example, list1 and list2 refer to different types of objects in the following example.
List
List1 = new ArrayList ()
List
List2 = new ArrayList ()
List1 points to a List where the type variable s is java.lang.Objects and list2 points to a List whose type variable s is String. So passing a List to a function with an argument of List will result in a compile time error. The following list can illustrate:
Public class AllowedTypeTest {
Public static void doIt (Listl) {
}
Public static void main (String [] args) {
ListmyList = new ArrayList ()
/ / an error will be generated here
DoIt (myList)
}
}
The above code cannot be compiled because you are trying to pass an incorrect type to the function doIt. The parameter of doIt is List2. The parameter you pass is List.
Can you use it? Wildcards solve this problem. List means a List that works on any object. So, doIt can be changed to:
Public static void doIt (List l) {}
Would you consider using it in some cases? Wildcards. For example, you have a printList function that prints all the members of a List that you want to work on any type of List. Otherwise, you will have to work hard to write a lot of printList overloaded functions. The following list refers to the use of? The printList function of the wildcard.
Public class WildCardTest {
Public static void printList (List list) {
For (Object element: list) {
System.out.println (element)
}
}
Public static void main (String [] args) {
Listlist1 = new ArrayList ()
List1.add ("Hello")
List1.add ("World")
PrintList (list1)
Listlist2 = new ArrayList ()
List2.add (100)
List2.add (200)
PrintList (list2)
}
}
This code shows that in the printList function, List represents various types of List objects. However, please note that it is used in the declaration? Wildcards are illegal, like this:
List myList = new ArrayList (); / / illegal
If you want to create a List that accepts any type of object, you can use Object as the type variable, like this:
ListmyList = new ArrayList ()
Use bounded wildcards in functions
In the previous section, you learned to create generics of different JAVA types by passing different type variables s, without considering the inheritance relationship between type variables s. In many cases, you want a function to have different List arguments. For example, you have a function getAverage that returns the average of the members of a List. However, if you take List as an argument to getAverage, you cannot pass List or List parameters, because List and List and List are not of the same type.
You can use prototypes or wildcards, but you can't do safe type checking at compile time, because you can pass any type of List, such as an instance of List. You can use List as an argument, but you can only pass List to the function. But this reduces the functionality of your functions, because you may want to operate List or List more often than List.
J2SE5.0 adds a rule to solve this constraint, which allows you to define an upper bound type variable. In this way, you can pass a type or its subclass. In the example of the getAverage function above, you can pass an instance of List or its subclass, such as Listor List.
The syntax using the upper bound rule is defined as this: GenericType
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.