File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generics Teaser

 
Manjunath Hirennavar
Greenhorn
Posts: 24
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Rancy Chadha
Ranch Hand
Posts: 135
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok...........ya I got it Rancy.....................!
 
Manjunath Hirennavar
Greenhorn
Posts: 24
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Rancy.................!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic