• 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

Queue

 
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a code from K&B .
I thought this prgram would result in an Exception after giving warnings during the compiile time.This is because we are try adding an Integer to a queue typed <String>

However, it gives warnings and runs successfully with out any error or exceptions.

Can some one help me with that..

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mohammad,

Just have a look at the method that's defined.



As you see the method doesn't use generics in its argument list, so the compiler doesn't know what will taken as an argument and therefore lets you add everything you want. It will print nevertheless a warning that you might do an unsafe operation but as I said the compiler won't enforce it.

As generics provide only compile-time protection, the JVM will not issue an error or an exception since all operations are correct.
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Klemens..

Thanks for the response.

Check this other code which is also from K&B.
Here it throws and exception and this is what I was expecting.

Here the list is of type Integer and I try to insert a String which results in Exception.
Isnt that a similiar case to the previous code.
I added an Integer to a Queue which was of String type.
How come that got executed and this didn't .
 
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Nabila Mohammad:
Hi Klemens..

Thanks for the response.

Check this other code which is also from K&B.
Here it throws and exception and this is what I was expecting.

Here the list is of type Integer and I try to insert a String which results in Exception.
Isnt that a similiar case to the previous code.
I added an Integer to a Queue which was of String type.
How come that got executed and this didn't .



Run this code and try to understand why this one is throwing an exception.



Hint: PriorityQueue uses natural order
[ August 06, 2008: Message edited by: Raphael Rabadan ]
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Raphael Rabadan:


Run this code and try to understand why this one is throwing an exception.



Hint: PriorityQueue uses natural order

[ August 06, 2008: Message edited by: Raphael Rabadan ]



This is code throwing an Exception becuase you are using PriorityQueue which sorts the queue in Alphabetical order or Priority .

so in this case it can't sort out if there is a String and Integer...
however if it's a Linked list which simply adds to the list..it gets casted as a String?! Right?

What about ArrayList - Does it require Casting? and is that why it throws exception?

So what has this got to with the
 
Raphael Rabadan
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Nabila Mohammad:


This is code throwing an Exception becuase you are using PriorityQueue which sorts the queue in Alphabetical order or Priority .

so in this case it can't sort out if there is a String and Integer...
however if it's a Linked list which simply adds to the list..it gets casted as a String?! Right?

What about ArrayList - Does it require Casting? and is that why it throws exception?

So what has this got to with the




PriorityQueue sorts the Queue using natural order, it means, using Comparable, or you can sort in other way using a Comparator(if you pass one to the constructor). The only diference of the Queue interface are the methods to get the next in the queue, like .poll(), .peek(), .remove().

LinkedList is ordered by insertion, so, the, when you add something to a list like LinkedList, nothing will be casted, because its not sorted (wont use a comparator or comparable), so wont have any cast, why it would?
When you iterate a Queue without generics its returning Objects, so, no problem too.

That's it.

I hope i could clarify your doubt.


Kind Regards,
Raphael Rabadan.
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What about ArrayList.
It uses order by index. So why was it throwing an exception.
 
Raphael Rabadan
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Nabila Mohammad:
What about ArrayList.
It uses order by index. So why was it throwing an exception.



Since when ArrayList implements Queue? Can you show the example?
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Nabila Mohammad:
I did not mean ArrayList implementing Queue
Was asking in General...

In this code it throws an exception because we are adding an Integer to a list of type String.

It does not sort the List like a PriorityQueue
It is inserting the element like a linked list except that it is ordered by index and not by insertion.

In that case, why is it throwing an exception?
Simply because it cannot Integer cannot be casted ot String?

I basically cannot understand the link between the different Collections when adding a different type element to it, when it's type is already predefined.

Priorty Queue -> throws exception -> it sorts it in natural order
LinkedList -> doesnot throws exceeption ->it simple inserts the element even if it is not of the type mentioned?
What about ArrayList?




[/QB]

 
Raphael Rabadan
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This case is completly diferent, you are using a explicit cast.
Remember, even if the List accept Objects, the objets inside of it has your own types.
In this example you showed, it has Integers and Strings, and you are trying to use a explicit cast to a Integer over all the List, when it get on the String object it will throw the ClassCastException, like it:

[ August 07, 2008: Message edited by: Raphael Rabadan ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic