In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
In this article, the editor introduces in detail the "introduction to the use of Java generics example analysis", the content is detailed, the steps are clear, and the details are handled properly. I hope that this "introduction to the use of Java generics example analysis" article can help you solve your doubts.
1. What is generics?
Generics actually pass types as parameters. Generics allow programmers to use types that are specified later when writing code, and pass the desired types as parameters when instantiating the class to indicate these types.
Why introduce generics?
For example, implement a sequence table by yourself
Public class MyArrayList {public int [] elem; public int usedSize; public MyArrayList () {this.elem = new int [10];} / add method public void add (int val) {/ / does not consider the expansion problem for the time being. This is just to talk about the generic elem [usedSize++] = val;} / / get method public int get (int pos) {return elem [pos] }}
Here we can see that when using the custom sequence table above, we can only add elements of type int. We know that the sequence table in the java collection can add any type of data. How do we achieve this? Here we first try to change the int type to the Object type, so that any type can be passed in.
Public class MyArrayList {public Object [] elem; public int usedSize; public MyArrayList () {this.elem = new Object [10];} / add method public void add (Object val) {/ / does not consider the expansion problem for the time being. This is just to talk about the generic elem [usedSize++] = val;} / / get method public Object get (int pos) {return elem [pos] }}
When you add data to an object in the main method, you can add any type of data. However, when the data needs to be fetched, because the Object type is returned, it is very troublesome to make a strong conversion to receive it with the corresponding type.
Public static void main (String [] args) {MyArrayList myArrayList = new MyArrayList (); myArrayList.add (1); myArrayList.add ("hello"); int array1 = (int) myArrayList.get (0); String array2 = (String) myArrayList.get (1);}
So the question is, does it have to be forced every time before it can be accepted, and can it not be forced? At this time, we think that when creating an instance object, we can pass the desired type as a parameter, so that all the data of the incoming type is stored in the object, so when we take it out, we can make it clear that all the data in the object is of that type, and there is no need to force it. This introduces generics.
Public class MyArrayList {/ / when writing a program, you don't specify a specific type, but use the E here to temporarily replace / / the specific type and pass in public E [] elem; public int usedSize when instantiating the object. Public MyArrayList () {/ / the writing here is not very accurate, so we should use the reflection mechanism. Here we first write this.elem = (E []) new Object [10];} / / the add method public void add (Eval) {/ / does not consider the expansion problem for the time being. This is just to talk about the generic elem [usedSize++] = val. } / / get method public E get (int pos) {return elem [pos];}} public static void main (String [] args) {MyArrayList myArrayList1 = new MyArrayList (); myArrayList1.add (1); myArrayList1.add (3); / / myArrayList1.add ("world") / / an error will be reported here, because the data passed in is not of the specified type, so generics also have the function of automatically checking types int I1 = myArrayList1.get (0); MyArrayList myArrayList2 = new MyArrayList (); myArrayList2.add ("hello"); myArrayList2.add ("world"); String S1 = myArrayList2.get (0);}
This ensures that any type of data can be passed in, and there is no need to force it out! The meaning of generics:
1. Check the type automatically
2. Cast the type automatically
So what is the type of object corresponding to MyArrayList here? It's MyArrayList.
< Integer >Or something?
As you can see here, the object created by the instance is of type MyArrayList, and the content in does not participate in the composition of generic types, so where are the types in it? It's time to understand how generics work.
2. How generics are compiled
An important difference between arrays and generics is how they enforce type checking. Specifically, the array stores and checks type information at run time. However, generics check for type errors at compile time and have no type information at run time.
Compilation mechanism of generics: erasure mechanism
At compile time, the E in MyArrayList is rubbed into the Object type.
The main method is all scrubbed to the MyArrayList type.
After reading this, the article "case Analysis of the introduction to the use of Java generics" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, welcome to follow the industry information channel.
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.