| Author |
generics doubt
|
sharan vasandani
Ranch Hand
Joined: Feb 22, 2007
Posts: 100
|
|
hi, List l=new List(); l.add("hi"); l.add("hello"); String s=l.get(0);////here but what is the return type here by default?i know i will need a cast to String for making the code correct.
|
 |
Chandra Bhatt
Ranch Hand
Joined: Feb 28, 2007
Posts: 1707
|
|
Originally posted by Saran: List l=new List(); l.add("hi"); l.add("hello"); String s=l.get(0);////here but what is the return type here by default?i know i will need a cast to String for making the code correct.
List l = new ArrayList(); //List l is not type safe so by default it is List<?> l; But don't treat it like List<Object> l; The difference between List<?> and List<Object>: List<?> l = new ArrayList<Animal>(); //OK l = new ArrayList<String>(); //OK l = new ArrayList<Integer>(); //OK But List<Object> l; l = new ArrayList<Object>(); //OK l = new ArrayList<String>(); //ERROR l = new ArrayList<Integer>(); //ERROR l = new ArrayList(); //OK with warning, unsafe operation You got the difference, List<Object> l can only hold reference of a List(Object that IS-A List of course) and parameterized with Object; In your example what is returned is Object, so you have to cast from Object to String because you know it is String ultimately. Regards, cmbhatt [ April 16, 2007: Message edited by: Chandra Bhatt ]
|
cmbhatt
|
 |
Nitesh Kant
Bartender
Joined: Feb 25, 2007
Posts: 1638
|
|
Originally posted by sharan vasandani: hi, List l=new List(); l.add("hi"); l.add("hello"); String s=l.get(0);////here but what is the return type here by default?i know i will need a cast to String for making the code correct.
If you do not specify the type of the object you will be storing in the List ,by writing List<String> , the return type will be Object. [ April 16, 2007: Message edited by: Nitesh Kant ]
|
apigee, a better way to API!
|
 |
Chandra Bhatt
Ranch Hand
Joined: Feb 28, 2007
Posts: 1707
|
|
Nitesh Kant says: If you do not specify the type of the object you will be storing in the List ,by writing List<String> , the return type will be Object.
By writing List<String> why will the return type be Object? cmbhatt
|
 |
Tommaso Nuccio
Ranch Hand
Joined: Dec 11, 2006
Posts: 66
|
|
Originally posted by Chandra Bhatt: By writing List<String> why will the return type be Object? cmbhatt
It says "If you do NOT..."
|
Ciao,<br /> Tommaso<br /> <br />~*~*~*~<br />There are 10 types of people, those who understand binary and those who don't.
|
 |
Nitesh Kant
Bartender
Joined: Feb 25, 2007
Posts: 1638
|
|
Originally posted by Chandra Bhatt: By writing List<String> why will the return type be Object? cmbhatt
I wrote: If you do not specify the type of the object you will be storing in the List ,by writing List<String> , the return type will be Object. Probably, this is the reason why, most question papers put "not" as bold. I should have done the same.
|
 |
Chandra Bhatt
Ranch Hand
Joined: Feb 28, 2007
Posts: 1707
|
|
Tommaso and Nitesh,
Nitesh says: Probably, this is the reason why, most question papers put "not" as bold. I should have done the same.
I agree, and not only in bold, the font size of that should be 72 By the way thanks for clarifying!!! Regards, cmbhatt
|
 |
Omer Haderi
Ranch Hand
Joined: Sep 27, 2006
Posts: 42
|
|
hi, List l=new List(); l.add("hi"); l.add("hello"); String s=l.get(0);////here but what is the return type here by default?i know i will need a cast to String for making the code correct.
Hello there, By the way, you can not say List l = new List() List is an interface and can not be instantiated. Cheers.
|
 |
Nitesh Kant
Bartender
Joined: Feb 25, 2007
Posts: 1638
|
|
Originally posted by Omer Haderi: Hello there, By the way, you can not say List l = new List() List is an interface and can not be instantiated. Cheers.
Dude thats a good catch No one really noticed that !!!
|
 |
Chandra Bhatt
Ranch Hand
Joined: Feb 28, 2007
Posts: 1707
|
|
Wow Omer, Fantastic catch in the current reference (when talk had lmost ended)! List is an interface and can't be instantiated!!! Regards, cmbhatt
|
 |
sharan vasandani
Ranch Hand
Joined: Feb 22, 2007
Posts: 100
|
|
|
sorry,my mistake
|
 |
 |
|
|
subject: generics doubt
|
|
|