• 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 Teaser

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
the author says that the code given below compiles and runs fine...just observe.


class TestBadLegacy{
public static void main(String [] args){
List<Integer> myList =new ArrayList<Integer>();
myList.add(4);
myList.add(6);
Inserter in=new Inserter();
in.insert(myList);
/*for(Integer s: myList)
System.out.println(s);*/--some thing
}
}
class Inserter{
void insert(List list){
list.add(new String("42"));
}
}
ok......when we don't include for loop it compiles and runs fine
without any exception......n the author didn't included this for loop when she states that this code compiles and runs fine...
but I wnated to test the code with printing the list elements so
now it gives an exception.........u might be knowing the exception.
So why its not working with print statement.........while it works without that.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello "Manjunath.N Hirennavar.Bangalore"-

Welcome to JavaRanch.

On your way in you may have missed that we have a JavaRanch Naming Policy for displayed (screen) names. Your displayed name must consist of a first name (or an initial), a space, and a family name (in that order) and not be obviously fictitious. Since yours "Manjunath.N Hirennavar.Bangalore", does not conform with it, please take a moment to change it, which you can do right here.

Posters with nonconforming displayed names will be locked out of JavaRanch after a few posts using those names.

Thanks
-Barry

(NR) - search tag
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, please play due respect to the author of the original example by giving a reference to where you found the code.
Thanks
 
Manjunath Hirennavar
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, Barry
I have updated my name, why i kept so because the site was not accepting.......

Ok now coming to the author, again sarry in hurry i forgot to mention the name of aoutor......its the Miss.Kathy Sierra..n i'm referring SCJP 5.0 written by her.
 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Manjunath,

class TestBadLegacy{
public static void main(String [] args){
List<Integer> myList =new ArrayList<Integer>();
myList.add(4);
myList.add(6);
Inserter in=new Inserter();
in.insert(myList);
/*for(Integer s: myList)
System.out.println(s);*/--some thing
}
}
class Inserter{
void insert(List list){
list.add(new String("42"));
}
}

The reason why you get a runtime exception(ClasscastException) when you run the code with the for loop uncommented is because JVM finds a string as the third element in the list and in the for loop you are trying to assign a String object to an Integer reference variable so ofcourse JVM will not allow this because they are not in same inheritance hierarcy therefore you get a Classcast exception. I hope this clears your doubt.
 
Manjunath Hirennavar
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok...........ya I got it Rancy.....................!
 
Manjunath Hirennavar
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Rancy.................!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic