• 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

Generic class question

 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is from scjptest.com (buggy but recommended):



Can't seem to figure out why this doesn't work. Any ideas?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


X is a type of something that extends Object. Question: is type X guarranteed to have the doubleValue() method?


And BTW, I disagree with the answer. I don't think that this will compile.

Henry

 
Ken Truitt
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok I forget to mention I also ran it without the 'extends Object' and it still wouldn't compile:

 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ken Truitt wrote:Ok I forget to mention I also ran it without the 'extends Object' and it still wouldn't compile:



Same question... is type X guaranteed to have the doubleValue() method?

Henry
 
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here x is not guaranteed to have an Integer.
Why x is not guaranteed to be an integer if because you can instantiate another instance of the class with another type say Character which is not related to Integer. Well you have to return the instance variable as the generic type X and in the method where the type safe is applied you have convert in into an int.
 
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

Nitish Bangera wrote:i suppose if you change doubleValue to intValue it would probably work with an Integer but here x is not guaranteed to have an Integer.



The Number class (which is sub-class of numeric wrappers including Integer) declares the doubleValue so every numeric wrapper class has that method. This problem is only due to the fact that X is not guaranteed to have a doubleValue or intValue method. Try changing the class declaration to TrickyNum<X extends Number>
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry Ankit that was a mistake....Well i updated my post. Kind off stressed out preparing for the Cert and doing simple mistakes. Well with X extends Number the error still persists when the return type is String. If it is changed to Double then it runs as the return type and the returning variables are inconvertible.

well we can print x.doubleValue() in the method and then return x. with the return type as X and return x.toString() if the return type is String.
 
Ken Truitt
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok thanks for the helpful responses. The String return type is an editing error--it should be double, in both versions.

Now, just to sum up, a.x.doubleValue works because it's a runtime issue and not a compile-time issue when we're accessing
x via a reference to an instance of the generic class?
 
Ken Truitt
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The String return type is an editing error--it should be Double, in both versions.



That's double.
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:
This problem is only due to the fact that X is not guaranteed to have a doubleValue or intValue method.



Arguing in a similar manner, X in the following code is not guaranteed to be in the same inheritance tree as Double, so the cast (Double)x should not be allowed.
Question : Why is there no compiler error at "LINE 1"???



 
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
Harsh, in your code you are doing an explicit type cast. This is allowed as the actual object might be of type Double at runtime. But if the cast fails at compile time, then you'll get a ClassCastException. See the code below



Just as this code is allowed, your code is allowed as explicit downcast in the same hierarchy is always allowed at compile time...
 
Harsh Pensi
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ankit, I completely understand the point you are making. An object reference variable being downcasted is perfectly fine.
But, in the TrickyNum class code, X extends Object. So X can be any subclass of Object at runtime and it need not be in same hierarchy as Double. And as it turns out in our case it is Integer, which gives a ClassCastException at runtime at "LINE 1".
 
Harsh Pensi
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote: your code is allowed as explicit downcast in the same hierarchy is always allowed at compile time...


How can we be sure that X and Double are in same hierarchy?
 
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
There is no guarantee that X is of type Double. That's you have to use a cast. If you want to check on runtime whether X is of type Double, then use instanceof operator on x (which is a reference variable of X). Actually since X extends Object, thus X is equivalent to Object, that's why I gave that example...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic