In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of "java generic case code analysis". The editor shows you the operation process through the actual case. The operation method is simple, fast and practical. I hope this "java generic case code analysis" article can help you solve the problem.
Let's start with a simple example:
Public void testGenerics () {
Collection numbers = new ArrayList ()
Numbers.add (1); / / ok
Collection tmp = numbers
/ / don't work, you don't know what type 'tmp' obviously contains
/ / tmp.add (1)
Collection objects1 = integers; / / ok
}
The range represented by Collection is larger than that of Collection
The correct way to represent any type of collection is Collection
Collection cannot represent any type of collection.
Why doesn't Collection represent any type? in fact, the compiler thinks there is a risk of type conversion errors:
Public void testGenerics4 () {
List integers = new ArrayList ()
List objects = new ArrayList ()
/ / this will be ok if List equals List
/ / objects = strings
/ / objects.add ("1")
/ / Integer I = (Integer) objects.get (0); / / and crashes
List objects1 = new ArrayList (); / / ok
/ / objects1.add ("1"); / / compiler will make it illegal
}
List can collect add data, because Object is the parent class of all objects, is a known type, and can be judged by obj instanceof Object.
The List compiler is not allowed to throw data into it, because you don't know what kind of data type List is, and you can't use obj instanceof UnknownType to judge it.
? Is the unknown type, and Object is the known type
If List means any type, according to Murphy's law (what may happen is bound to happen), then the crash in the above example is bound to happen. (another online glitch)
This is the end of the introduction to "java generic example Code Analysis". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
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.