In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "what are the differences between placeholders T and? in java generics". The explanation in this article is simple and clear and easy to learn and understand. please follow the editor's train of thought to study and learn "what are the differences between placeholders T and? in java generics"!
Let's start with two pieces of code:
Public static void show1 (List list) {for (Object object: list) {System.out.println (object.toString ());}} public static void show2 (List list) {for (Object object: list) {System.out.println (object);}}
You can see that we use T in the show1 method, and we all know that this is a common way to write generics, so T here refers to a certain class of specific objects. Only the same type of data can be stored in the list collection, and an error will be reported if you insert different types of data.
So what do we use in the show2 method? You can see that there is no such thing in front of void. It can be expressed as a placeholder, and it doesn't know how many types of data will be stored in the list collection, so this shows that it is possible to store N data types in our list.
Let's intuitively feel the difference between the two through a piece of test code:
Public static void test () {List list1 = new ArrayList (); list1.add (new Student ("zhangsan", 18Power0)); list1.add (new Student ("lisi", 28Power0)); list1.add (new Student ("wangwu", 24Power1)); / / here if add (new Teacher (...)); will report an error because we have assigned List the data type Student show1 (list1). System.out.println ("* split line *"); / / We do not specify a specific data type for List here, and we can store multiple types of data List list2 = new ArrayList (); list2.add (new Student ("zhaoliu", 22); list2.add (new Teacher ("sunba", 30 list2)); show2 (list2);}
Let's take a look at the running results:
Student {name='zhangsan', age=18, sex=0}
Student {name='lisi', age=28, sex=0}
Student {name='wangwu', age=24, sex=1}
* Segmentation line *
Student {name='zhaoliu', age=22, sex=1}
Teacher {name='sunba', age=30, sex=0}
From the show2 method, we can see the difference between list2 and show1. List2 stores two types of Student and Teacher, and can also output data, so this is T and? The difference ~ do you guys understand?
Why don't you take a look next? The extended writing method of
List
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.