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 introduces the relevant knowledge of "Java erasure and conversion example analysis". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
A little bit of eye contact
In strict generic code, classes with generic declarations should always take type parameters. However, in order to be consistent with old Java code, it is also allowed to use classes with generic declarations without specifying type parameters. If no type parameter is specified for this generic class, the type parameter is called a raw type (primitive type), and the default is the first upper bound type specified when the parameter is declared.
When an object with generic information is assigned to another variable that does not have generic information, all type information between angle brackets is thrown away. For example, if a List type is converted to List, the List's type checking on the collection element becomes the upper limit of the typed variable (that is, Object), which is called erasure.
II actual combat-erasure
Class Apple {T size; public Apple () {} public Apple (T size) {this.size = size;} public void setSize (T size) {this.size = size;} public T getSize () {return this.size;} public class ErasureTest {public static void main (String [] args) {Apple a = new Apple (6); the getSize method of / / ① / / a returns Integer object Integer as = a.getSize () / / assign an object to the Apple variable and lose the type information in angle brackets Apple b = a; / / ② / / b only knows that the type of size is Number Number size1 = b.getSize (); / / the following code causes a compilation error / / Integer size2 = b.getSize (); / / ③}}
Three actual battles-- erasure and conversion
Import java.util.*;public class ErasureTest2 {public static void main (String [] args) {List li = new ArrayList (); li.add (6); li.add (9); List list = li; / / the following code causes an "unchecked conversion" warning that compiles and runs normally List ls = list; / / ① / / but as long as you access elements in ls, such as the following code will cause a run-time exception. / / System.out.println (ls.get (0));}}
This is the end of the content of "example Analysis of Java erasure and conversion". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.