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


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Queue" Watch "Queue" New topic
Author

Queue

Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
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..



The future belongs to those who believe in the beauty of their dreams.Dream BIG!
Klemens Katterbauer
Greenhorn

Joined: Jul 15, 2008
Posts: 8
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.


Klemens Katterbauer<br />Student of Mathematics,<br />University of Vienna
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
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 .
Raphael Rabadan
Ranch Hand

Joined: Jul 05, 2008
Posts: 141
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 ]

SCJP Java 6 (98%) - Story, SCJA (88%) - Story
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
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

Joined: Jul 05, 2008
Posts: 141
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

Joined: Nov 05, 2007
Posts: 661
What about ArrayList.
It uses order by index. So why was it throwing an exception.
Raphael Rabadan
Ranch Hand

Joined: Jul 05, 2008
Posts: 141
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

Joined: Nov 05, 2007
Posts: 661
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

Joined: Jul 05, 2008
Posts: 141
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Queue