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 mainly explains "Java type wildcard application example analysis", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor to take you to learn the "Java type wildcard application example analysis"!
A little bit of eye contact
List objects cannot be used as List objects, that is, the List class is not a subclass of the List class.
Arrays are different from generics: assuming that Foo is a subtype of Bar (subclass or subinterface), Foo [] is still a subtype of Bar [], but G is not a subtype of G.
To represent the parents of various generic List, we need to use a type wildcard, which is a question mark (?), passing a question mark as a type argument to the List collection, written: List (meaning List of an unknown type element). This question mark is called a wildcard, and its element type can match any type.
Excellent design of bigenerics
1 code
Import java.util.*;public class ArrayErr {public static void main (String [] args) {/ / define an Integer array Integer [] ia = new Integer [5]; / / you can assign an Integer [] array to the Number [] variable Number [] na = ia; / / A where the following code compiles normally, but throws an ArrayStoreException exception / / because 0.5 is not Integer na [0] = 0.5 / / List iList = new ArrayList () at / B; / / the following code causes a compilation error / / List nList = iList; / / C}}
2 run
Exception in thread "main" java.lang.ArrayStoreException: java.lang.Double at ArrayErr.main (ArrayErr.java:23)
3 description
The code at A does not cause a compilation error, but it raises a run error at B.
The code from B directly causes compilation errors.
This is the difference between arrays and generics, which are designed to detect errors early rather than at run time.
Three practical battles-using type wildcards
1 code
Import java.util.*;public class ArrayErr {public static void main (String [] args) {List c = new ArrayList (); c.add ("first"); c.add ("second"); test (c); / / the following usage is wrong, this type of wildcard cannot add the element List wrongList = new ArrayList (); / / wrongList.add (new Object ()) } / / this question mark type wildcard represents the parent class of various generics, and its element type can match any type, and can be used in traversal for public static void test (List c) {for (int iPre0uti).
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.