aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes generics question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "generics question" Watch "generics question" New topic
Author

generics question

vijay umar
Ranch Hand

Joined: Mar 24, 2009
Posts: 100
i have a doubt in this program please somebody clear it please!

public static void main(String[] args)
{
Queue<String> q = new LinkedList<String>();
q.add("Veronica");
q.add("Wallace");
q.add("Duncan");
showAll(q);
}

public static void showAll(Queue q)
{
q.add(new Integer(42));
while (!q.isEmpty())
System.out.print(q.remove() + " ");
}

the output i got is-Veronica Wallace Duncan 42


my doubt-- how can we add a Ineger type of object to a s=String type of Object doesnt it contradicts what we have studied in the book. i am not clear that how that Integer is added into the String arrayList
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Vijay,

You should get a warning when you are compiling this. The reason why it works is because you are using a raw type reference (Queue) to add the Integer. When you use raw types you lose generics' type safety. This is dealt with in the book when they talk about mixing generics with legacy code.


All code in my posts, unless a source is explicitly mentioned, is my own.
avi sinha
Ranch Hand

Joined: Mar 15, 2009
Posts: 453

vijay umar wrote:
my doubt-- how can we add a Ineger type of object to a s=String type of Object doesnt it contradicts what we have studied in the book. i am not clear that how that Integer is added into the String arrayList


you are not adding the Integer object to Generic typed queue.you are adding it to a simple queue which can hold anything but primitive.


ofcourse compiler will issue a warning here

avi


SCJP 5.0 SCWCD 5.0
vijay umar
Ranch Hand

Joined: Mar 24, 2009
Posts: 100
yes you are right!! thank you very much

I am preparing for scjp exam! but i am not able have all the knowledge to attend the questins! what should i do ? i am studying all the topics of kathy serra book. but some questions i fell very hard to even understand! Is there any options or is there any methodology to follow to prepare for this particular exam? can you guide me?
avi sinha
Ranch Hand

Joined: Mar 15, 2009
Posts: 453

vijay umar wrote:
Is there any options or is there any methodology to follow to prepare for this particular exam? can you guide me?


i think k & b book is the best way to learn for the exam. try to do lots of small codes.that will help you understanding the concepts.

avi...
vijay umar
Ranch Hand

Joined: Mar 24, 2009
Posts: 100
thanks
avi sinha
Ranch Hand

Joined: Mar 15, 2009
Posts: 453

sorry i don't have it

avi
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
vijay umar wrote:yes you are right!! thank you very much

I am preparing for scjp exam! but i am not able have all the knowledge to attend the questins! what should i do ? i am studying all the topics of kathy serra book. but some questions i fell very hard to even understand! Is there any options or is there any methodology to follow to prepare for this particular exam? can you guide me?

Practice a lot, and write sample code. I think that's the best way to learn. Good luck with it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: generics question