• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Mock Exam doubt Generics

 
Ranch Hand
Posts: 71
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
Following is the code

Following are the code fragments which will compile without errors when inserted at //insert code here

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));

I tested this code and observed that A and C are compiling successfully.

Can anyone please explain this strange behaviour of code.

Thanks in advance.
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

C is correct, i think is rational. moreover, B and D is incorrect is also understandable.

But A is correct a bit confuse me. Could you try to change the string "456" to "abc"?

I guess, in Integer class, it accepts string argument which is like this:



so maybe it automatically turn into Integer??

Need explanation from pro....
 
Ranch Hand
Posts: 462
Scala jQuery Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A) works because the return type is either going to be an Integer or a String which are both subtypes of Object
B) fails because you can't cast a Double to an int
C) works because you can cast an Integer to an int
D) fails for the same reason as B

This is not a Generics question but a question of casting...sneaky eh?
 
Ranch Hand
Posts: 50
Android Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, it compiles, but does A run without Exceptions? (Like when we put in a TreeSet objects of different types)

EDIT:
Just tested, no it doesn't:
 
Will Myers
Ranch Hand
Posts: 462
Scala jQuery Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The programmer exam is quite specific between compiler and runtime errors - as we all know just because something compiles doesn't mean it is right or will run
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not quite clear guys
what class is 'T' determined as in the call of option A. ?
 
Will Myers
Ranch Hand
Posts: 462
Scala jQuery Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


is passing an int (123) and a String ("456") to the findLarger method, this is converted to an Integer and a String automatically by the compiler (AutoBoxing), as both Integer and String implement the Comparable interface this is fine and the method returns whichever of the 2 is appropriate depending on the implementation of Comparable (the compareTo part is where it will fail with the ClassCastException when it is run but the compiler doesn't know this)

if you run this code you get the following:



and this is because the compareTo implementation of Integer is:



and clearly you can't cast a String to an Integer
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, looking at it, in case A, T has got to be supertype of Integer and String, subject to the restriction that T is a subtype of Comparable.

I think there's only one possibility here: T is Comparable.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By the way, I think the way to turn your runtime exception into a compile-time error is to declare it like this:
Because Integer implements Comparable<Integer>, and String implements Comparable<String>, option A will no longer compile because they can't be compared with each other. The problem with the example given is that's it's partly generic, but it hasn't gone all the way.
 
Jagdev Singh
Ranch Hand
Posts: 71
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi,
Thanks all for responces , to understand the option D for casting use the code given below.



It gives following error.

This is an tricky casting question.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote:By the way, I think the way to turn your runtime exception into a compile-time error is to declare it like this:
Because Integer implements Comparable<Integer>, and String implements Comparable<String>, option A will no longer compile because they can't be compared with each other. The problem with the example given is that's it's partly generic, but it hasn't gone all the way.



Hi,

Could you please explain this in a little more detail how this declaration would help the compiler to catch the Integer-String comparison, preferable plugging in Integer/String into the declaration.

TIA.
 
Nothing? Or something? Like this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic