Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Generics SCJP 1.5

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi folks,
Following is the code snippet I got from whizlabs mock test, which I am unable to understand :

Given:
1. public class Test {
2. public <T extends Comparable> T findLarger(T x, T y) {
3. if(x.compareTo(y) > 0) {
4. return x;
5. } else {
6. return y;
7. }
8. }
9. }
and:
22. Test t = new Test();
23. // insert code here
Which two will compile without errors when inserted at line 23?
(Choose two.)
A. Object x = t.findLarger(123, �456�);
B. int x = t.findLarger(123, new Double(456));
C. int x = t.findLarger(123, new Integer(456));
D. int x = (int) t.findLarger(new Double(123), new Double(456));

Answer : AC

Can anybody explain me ?

Regrads,
Sagar
 
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its more about Autoboxing than Generics.

Just a hint:
1)a primitive integer can be wrapped in Integer object
2)a String can be represented as integer and wrapped in Integer object as the overloaded constructor accepts String objects too

A integer can't be wrapped into Double object as the method says (T x, T y)

and D option is wrong because of typecasting of Double to int (Wider to narrower)
 
Sagar Jani
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Sandeep. But when I try these options all the options are compiled without errors. Because T can be any object.

So why only AC ?
 
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sagar,

But when I try these options all the options are compiled without errors.



Really? But it did give me errors, when I tried...


So why only AC ?


Considering option B)


I think, this method invocation's signature would be like findLarger(Integer, Double) and even if it did compile, would cause a ClassCastException when it tries to compare an Integer with a Double...

But the compiler even before that guesses that there is a possibility that
a Double can be returned by the method(in the else part) and that it cannot be assigned to an int without an explicit cast... and hence the compiler error...


Considering Option D)

int x = (int) t.findLarger(new Double(123), new Double(456));



Now here, if at all it executes, there wont be any problems with the comparison of a Double with a Double and there wont be any ClassCastException... But then the compiler sees that you are trying to cast a Double to an int which is not possible... (Double to double is ok, double to int is ok, but Double to int is not...) and hence the error.

Options A and C are fine, but still option A gives a ClassCastException on running since a String cannot be compared with an Integer... anws thats not a compiler error, so option A holds..


Regards,
Vishwa
 
crispy bacon. crispy tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic