Home  >  Article  >  Java  >  Object, T (generics), and their differences in Java

Object, T (generics), and their differences in Java

怪我咯
怪我咯Original
2017-06-30 10:40:411355browse

The following editor will bring you an article that discusses the differences between Object, T (generics), and ? in Java. The editor thinks it is quite good, so I will share it with you now and give it as a reference for everyone. Let’s follow the editor to take a look.

Because I recently re-read generics and read some reflections, I have doubts about Object, T (hereinafter referred to as generics), and ?.

Let’s try to understand the Object class. Anyone who studies Java should know that Object is the parent class of all classes. Note: Then this means it The range is very wide! First of all, remember this, if your parameter type is Object, then the parameter type will be very wide!

"Thinking in Java" said that many reasons contributed to the emergence of generics, and one of the most eye-catching reasons is to create container classes. How to understand this? My understanding is that you can put aside this in order to create container classes, and return to the purpose of generics to limit a certain type.

So we can now summarize the two important differences between Object and T:

Object has a very wide range, while T starts from one This type will be qualified from the beginning (including that it can be limited to type Object).

Because Object is the parent class of all classes, it will force type conversion, and T has limited some specificity from the beginning when coding (note that when writing code) type, so it doesn't require type casting. (The reason why we should emphasize when writing code is because generics will be erased by the JVM in the virtual machine. Its specific type information can be referred to generics, which will not be extended here).

For example, the List class in jdk is a generic class. Now we specify that its type is Object.

List<Object> list = new ArrayList<Object>();
int i = 0;
String s = "s";
list.add(i);
list.add(s);

List itself is a generic class. Now we specify that it receives parameters of type Object. At this time, you can put any type of parameters into it, and when you take it out, you must perform forced type conversion into a specific one. type.

Now if we specify the List to receive String type parameters, then this List can only be placed in String type, and there is no need to perform forced type conversion when taking it out.

The inspiration this brings to us is that when writing a class similar to List, we must pay attention to whether to use generics. Be sure to write more generic classes and discuss more and understand more, otherwise they may still get mixed up.

Followed by ?, this may be used when reflection needs to be used to obtain the Class type. Its explanation is: receiving an uncertain type, which is a bit the same as Object. My understanding of it is that if you only use "?", then it is the same as Object, but "?" has a slightly more "advanced" usage than Object, that is, it can narrow an uncertain range and use something like "?" extends Test", this means that only inherited classes that receive the Test class are received. Is it smaller than the scope of Object?

The above is the difference between Object, T, and ?. Write a few more lines of code, do more experiments, and discuss more, and you will know when and where to use it appropriately.

The above is the detailed content of Object, T (generics), and their differences in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn