• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

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
 
Beauty is in the eye of the tiny ad.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic