• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

generics doubt

 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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 ]
 
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Chandra Bhatt:


By writing List<String> why will the return type be Object?



cmbhatt



It says "If you do NOT..."
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry,my mistake
 
reply
    Bookmark Topic Watch Topic
  • New Topic