• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

warning?

 
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why does this give no warning

PriorityQueue queue1 = null;
PriorityQueue<Integer> queue2 = null;
queue1 = queue2;

whereas this does?

PriorityQueue queue1 = null;
PriorityQueue<Integer> queue2 = null;
queue2 = queue1;

doesnt K&b say that generic on both the sides should be same...
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ankur please Use A Meaningful Subject Line when you start a topic.

The second assignment gives a compilation warning because an untyped collection is being assigned to a typed list. So the compiler won't be able to ensure type safety if the untyped list contains elements of type other than Integer. Suppose this is the code

In the first case, the compiler doesn't give a warning because you are assigning from a typed queue to an untyped queue. So the compiler is not responsible for the untyped queue. But if you try to add elements to the untyped list, then the compiler knows that this might break type safety somewhere else, so it generates a warning


HTH
 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey there are no compile errors in any one of them,warning in the second but none in the first....and what about that K&B says that generics on both sides should be same....
 
Ankit Garg
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankur kothari wrote:hey there are no compile errors in any one of them,warning in the second but none in the first....


Who said there are compilation errors, there will be a runtime exception in both of them. I'm getting a warning in the first program

Are you compiling the program from command line??

and what about that K&B says that generics on both sides should be same


Where exactly is this stated in the book?? Can you quote the exact statement...
 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
page 608........."Just keep the generic type of the reference and the generic type of the object to which it refers identical"

"the type of the variable declaration must match the type you pass to the actual object type"

and i was talking about the warnings in the examples in my first post....no warnings in the first and warning in the second
 
Ankit Garg
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankur kothari wrote:and i was talking about the warnings in the examples in my first post....no warnings in the first and warning in the second


Yes sorry, I saw the opposite codes, I've corrected my post.

page 608........."Just keep the generic type of the reference and the generic type of the object to which it refers identical"
"the type of the variable declaration must match the type you pass to the actual object type"


I can't understand how is this relevant to what you are saying. Is this one statement or two separate statements??
 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
separate statements......i only meant by the first one


page 608........."Just keep the generic type of the reference and the generic type of the object to which it refers identical"

actually i thought the 2nd sentence meant the same as the first
 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey ankit leave it....thanks for trying to make me understand....i am very new to generics and collections so am getting too confused..
 
Ankit Garg
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By that statement (without knowing in which context the statement is said), I can say that the book is trying to tell you this


[Edit]

hey ankit leave it


Too late for that
 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ArrayList<Number> nlist1 = new ArrayList();

is this legal? let me try it out...i guess this is what my first post meant by.
 
Ankit Garg
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java generics is designed to support legacy code, that's why that assignment is allowed. It will just generate a warning that's all...
 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok now i get what you are trying to explain....i got confused with

ArrayList nlist1 = new ArrayList<Number>(); and

ArrayList nlist1<Integer> = new ArrayList<Number>();

the first one is completely different...
 
Yes, my master! Here is the tiny ad you asked for:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic