• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

generics question

 
vijay umar
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
avi sinha
Ranch Hand
Posts: 453
Google Web Toolkit Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
vijay umar
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 453
Google Web Toolkit Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks
 
avi sinha
Ranch Hand
Posts: 453
Google Web Toolkit Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry i don't have it

avi
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic