GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generics Teaser Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generics Teaser" Watch "Generics Teaser" New topic
Author

Generics Teaser

Manjunath Hirennavar
Greenhorn

Joined: Nov 15, 2006
Posts: 24
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

Joined: Aug 03, 2002
Posts: 7729
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


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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

Joined: Nov 15, 2006
Posts: 24
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

Joined: Jul 12, 2006
Posts: 135
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.


Thanks,<br />-Rancy
Manjunath Hirennavar
Greenhorn

Joined: Nov 15, 2006
Posts: 24
Ok...........ya I got it Rancy.....................!
Manjunath Hirennavar
Greenhorn

Joined: Nov 15, 2006
Posts: 24
Thank you Rancy.................!
 
GeeCON Prague 2014
 
subject: Generics Teaser