Latest topics
The most tagged keywords
Meaning Of <?> in java
9jabaze - Entertainment | Webmaster | Browsing Tricks | Games/Apps Hub :: Technology Forums :: Blogging
Page 1 of 1
Meaning Of <?> in java
It's a wildcard. <?> is the same as <? extends Object>
- any type.
The way wildcards are usually used is <? extends Foo> (any class that extends Foo) or <? super Foo>
(any class that is a supertype of Foo)
Here is an example of usage of <?>:
Consider the following method, printList:
public static void printList(List<Object> list) {
for (Object elem : list)
System.out.println(elem + " ");
System.out.println();
}
The goal of printList is to print a list of any type, but it
fails to achieve that goal — it prints only a list of
Object instances; it cannot print List<Integer>,
List<String>, List<Double>, and so on, because they
are not subtypes of List<Object>. To write a generic
printList method, use List<?>:
public static void printList(List<?> list) {
for (Object elem: list)
System.out.print(elem + " ");
System.out.println();
}
Because for any concrete type A, List<A> is a subtype
of List<?>, you can use printList to print a list of any
type:
List<Integer> li = Arrays.asList(1, 2, 3);
List<String> ls = Arrays.asList("one", "two",
"three");
printList(li);
printList(ls);
SOURCE
- any type.
The way wildcards are usually used is <? extends Foo> (any class that extends Foo) or <? super Foo>
(any class that is a supertype of Foo)
Here is an example of usage of <?>:
Consider the following method, printList:
public static void printList(List<Object> list) {
for (Object elem : list)
System.out.println(elem + " ");
System.out.println();
}
The goal of printList is to print a list of any type, but it
fails to achieve that goal — it prints only a list of
Object instances; it cannot print List<Integer>,
List<String>, List<Double>, and so on, because they
are not subtypes of List<Object>. To write a generic
printList method, use List<?>:
public static void printList(List<?> list) {
for (Object elem: list)
System.out.print(elem + " ");
System.out.println();
}
Because for any concrete type A, List<A> is a subtype
of List<?>, you can use printList to print a list of any
type:
List<Integer> li = Arrays.asList(1, 2, 3);
List<String> ls = Arrays.asList("one", "two",
"three");
printList(li);
printList(ls);
SOURCE
![-](https://2img.net/i/fa/modernbb/tabs_less.gif)
» Meaning Of this() in Java
» Meaning Of %= in Java
» Meaning of bounce rate in Google Analytics
» Pes 2020 Java Game Download For Nokia And Java Phone (All Sizes)
» How to start Java game development? I know the basics of Java.
» Meaning Of %= in Java
» Meaning of bounce rate in Google Analytics
» Pes 2020 Java Game Download For Nokia And Java Phone (All Sizes)
» How to start Java game development? I know the basics of Java.
9jabaze - Entertainment | Webmaster | Browsing Tricks | Games/Apps Hub :: Technology Forums :: Blogging
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|
» Optimal Сasual Dating - Authentic Damsels
» Superlative Сasual Dating - Verified Ladies
» Wapkiz 2020 Code: wapkiz register page responsive design Code
» 2020Bbnaija : Why I Can’t Be In A Relationship With Ozo – Nengi
» Can You Have S*x On Your First Date? Check Out What Actress, Debbie Shokoya Has To Say About This
» Nigerian Singer, Seunfunmi Stephen, Recounts How Her Husband Was Killed By Stray Bullet
» Olakunle Churchill Shares Photo Of Himself And Actress Rosy Meurer As He Writes On The Importance Of Loyalty
» Ubi Franklin Reveals What Happened To Him When He Visited A Hospital Amid COVID-19 Pandemic
» Vardy Wins Premier League Golden Boot, Beats Ings, Aubameyang & Sterling
» The Right Way To Preach About Hell - Milton Goh
» “Surgery For What?” – Regina Daniels Shows Off Hot Body After Childbirth (Video)
» I Got Pregnant As A Virgin At 23 – Wathoni
» #Bbnaija2020: Wathoni Kisses Kiddwaya During BBNaija Truth Or Dare Game
» Bbnaija: Kiddywaya Gropes Erica’s Breast While Dancing And She Reacts
» “I Hope To Collaborate With Rihanna & Drake Soon” – Rema
» “Girls That Give Threesome With Their Friend Have A Special Place In Heaven” – Naira Marley
» “To Be A Boss, You Must Be A Servant” – Frodd Recounts On His Humble Beginnings
» “Stop Pressuring Us With Your Beauty”, Actress Queen Nwokoye Begs Ini Edo
» You Look Like A Weed Smoker – Lady Fires Adekunle Gold Over New Look