Win a copy of Microservices in Action this week in the Web Services forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Question in Generics-1  RSS feed

 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Source : http://nikojava.wordpress.com/2008/10/09/scjp-mock-exam-for-generics/
I was trying to solve the below question
Q)Will this code complies successfully or not ?


Choices
a) Yes
b) No

To my knowledge the answer is 'Yes' but when I executed it using eclipse editor it gives compile time exception.
Why 'Yes' according to me is as follows:
?--> denotes any type
extends -->denotes either implements or extends the type on the right hand side which means that a sub class that extends/implements Number is allowed.
super --> denotes that the type on the right hand side of super and its parent classes are allowed.

since Integer is a subclass of Number , this code should work , but it throws compile time exception
Can any one let me know where am I going wrong here?

thanks,
Jyothsna
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jyothsna Panchagnula wrote:Will this code complies successfully or not ?

To my knowledge the answer is 'Yes' but when I executed it using eclipse editor it gives compile time exception.
Why 'Yes' according to me is as follows:
?--> denotes any type
extends -->denotes either implements or extends the type on the right hand side which means that a sub class that extends/implements Number is allowed.
super --> denotes that the type on the right hand side of super and its parent classes are allowed.
since Integer is a subclass of Number , this code should work , but it throws compile time exception
Can any one let me know where am I going wrong here?



Short explanation: super of Integer is in "worst case" Object, which is does not extends Number
 
author
Posts: 23810
140
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

To my knowledge the answer is 'Yes' but when I executed it using eclipse editor it gives compile time exception.
Why 'Yes' according to me is as follows:
?--> denotes any type



Actually, a better interpretation is ... It is any type, but the compiler doesn't know what type it is. It doesn't mean that the queue can hold any type, it just means that you can't determine the actual type from the reference.

extends -->denotes either implements or extends the type on the right hand side which means that a sub class that extends/implements Number is allowed.
super --> denotes that the type on the right hand side of super and its parent classes are allowed.



Correct. It is not completely unknown. There are some boundaries of what this unknown type could be.

since Integer is a subclass of Number , this code should work , but it throws compile time exception
Can any one let me know where am I going wrong here?



Well, how do you know that it is an Integer type? All you know is that it is bounded by Integer. The element type for q2 can be Integer, Number, or Object. The compiler actually doesn't know what it is. Now, I agree. If the type was Integer or Number, then the assign should be safe. But what if it was a Queue of Object? How does the compiler check that it isn't?

Henry
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I\m pretty confused with this.

From the above example, I should NOT be able to compile this:


But this compiles. For all we know, q1 can be Number, Integer or Byte and q2 can be Integer or a class thereof.
Therefore, how can the compiler allow me to do this but not this:



This fails.
 
Jyothsna Panchagnula
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Maduranga,

According to K&B SCJP 1.5 ..
"The type of the variable declaration must match the type that you pass to the actual type"
which means say for eg:-

the above code doesn't compile . due to the reason mentioned above , how ever , the above code compiles if changed to


Hope this helps
 
Henry Wong
author
Posts: 23810
140
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Maduranga Liyanage wrote:I\m pretty confused with this.

From the above example, I should NOT be able to compile this:



Well, your example is very different from the above example, but...

But this compiles. For all we know, q1 can be Number, Integer or Byte and q2 can be Integer or a class thereof.
Therefore, how can the compiler allow me to do this but not this:




You have to remember what is being assigned to what. In this case, you are assigning an object (referenced by the q2 variable) to the q1 reference variable. The object is of a list whose elements are of some type that can be Integer or extends it. The q1 reference can hold a list whose elements are of some type of Number or extends it. So this should be fine.

Now, if you have tried to assign the q1 to q2, you would have had a compile error.

Henry
 
Maduranga Liyanage
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you.

But doesnt q1 (<? extends Number>) has the possibility of being of type Byte or any of the other wrapper classes?
And q2 can be Integer or thereof. So assigning q1=q2 is OK?

I'm sorry if I mistaking something gravely here.
 
Henry Wong
author
Posts: 23810
140
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Maduranga Liyanage wrote:Thank you.

But doesnt q1 (<? extends Number>) has the possibility of being of type Byte or any of the other wrapper classes?
And q2 can be Integer or thereof. So assigning q1=q2 is OK?

I'm sorry if I mistaking something gravely here.



Remember that q1 is a reference -- it is *not* an object. In the assignment, it is being assigned to the object that is referenced by q2.

Sure, q1 could possibly be referencing a type Byte prior to the assignment. But so what? The old object will not be affected here. It just won't be referenced anymore.

Henry
 
Maduranga Liyanage
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Henry.

I think I got it now. and sorry for hijacking this thread.

Cheers.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!