In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "what is java reactor pollution". In daily operation, I believe many people have doubts about what java reactor pollution is. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "what is java reactor pollution?" Next, please follow the editor to study!
Brief introduction
What is heap pollution? Heap pollution occurs when an object referenced by a parameterized type variable is not an object of that parameterized type.
We know that in JDK5, the concept of generics has been introduced, and we can specify the object types that should be stored in the collection class when we create the collection class.
If different types are referenced in a collection of specified types, this is called heap pollution.
An example of heap pollution
Some students may ask, since generics are introduced in JDK5, why is there heap pollution?
This is a good question. Let's look at an example:
Public void heapPollution1 () {List normalList= Arrays.asList ("www.flydean.com"); List integerList= normalList;}
In the above example, we used Arrays.asList to create a normal List.
This List contains two types: int and String. When we assign List to List, the java compiler will not judge the type in the assigned List. IntegerList contains non-Integer elements, resulting in errors in use.
Assigning a value directly to List does not do type checking, so what if we are adding elements directly to List?
Let's take a look at the following example:
Private void addToList (List list, Object object) {list.add (object);} [@ Test] (https://my.oschina.net/azibug) public void heapPollution2 () {List integerList=new ArrayList (); addToList (integerList, "www.flydean.com");}
In the above example, we define an addToList method whose argument is a normal List, but we pass in a List.
As a result, we find that the list.add method does not perform parameter type checking.
How to modify the above example?
We need to add type checking to the List parameter of the addToList method:
Private void addToList (List list, Object object) {list.add (object);}
What if addToList is a very general method? It is realistic to add parameter types to the parameters of addToList.
At this point, we can consider using the Collections.checkedList method to convert the input List into a checkedList, thus receiving only certain types of elements.
Public void heapPollutionRight () {List integerList=new ArrayList (); List checkedIntegerList= Collections.checkedList (integerList, Integer.class); addToList (checkedIntegerList, "www.flydean.com");}
By running the above code, we will get the following exception:
Java.lang.ClassCastException: a more general example of Attempt to insert class java.lang.String element into collection with element type class java.lang.Integer
Above we define an addToList method, because there is no type judgment, so the problem of heap pollution may occur.
Is there any way to not only be universal, but also avoid pile pollution?
Of course, let's take a look at the following implementation:
Private void addToList2 (List list, T t) {list.add (t);} public void heapPollutionRight2 (T element) {List list = new ArrayList (); addToList2 (list,element);}
In the above example, we define a parameter type T in the addToList method, which ensures the consistency of element types in List.
Variable parameter
In fact, method parameters can be variable, so let's consider the following example:
Private void addToList3 (List... ListArray) {Object [] objectArray = listArray; objectArray [0] = Arrays.asList ("www.flydean.com"); for (List integerList: listArray) {for (Integer element: integerList) {System.out.println (element);}
In the above example, our parameter is an array of List, although the element type in List is fixed, but we can re-assign to the parameter array, thus actually changing the parameter type.
If the method parameters of the above addToList3 are changed to the following way, there will be no problem:
Private void addToList4 (List listArray) {
In this case, the type of List is fixed, and we cannot modify it by reassigning it.
At this point, the study on "what is java pile pollution" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.