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 question

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Friends,

I have doubt in the following code:



I am not able to understand:
1) why A option gets compiled, how the findLarger(T x, T y) accept 123 and "456" even though both are different, one is Integer and other is String.According to me both the arguments should be same.

2) Why option B is giving compiler error as: "Type mismatch:cannot convert from Number&Comparable<?> to int".

I think I am not able to get the correct concept of this statement "public <T extends Comparable> T findLarger(T x, T y)" . Can anyone clarify my doubt?

Regards,
Lata
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that the parameterised declaration T extends Comparable is letting you to declare a class T which inherits the properties of Comparable class.

The input arguements that you pass to the function, the return type is also the same.

Thats why the b option is getting screwed as the return type changes to either Double or Comparable. 'int' doesn't qualify for both these options.
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you also need to look at the return type as it and the arguments are both of type T.

In the case of A I think all arguments are considered to be compatible with Object. In the case of the 123 it is wrapped to an Integer which of course extends Object.

In the case of B the arguments extend Number but the return type is int and the method could return anything that extends Number.
 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think option a and c is correct.

B is incorrect because you cannot pass two different type of parameter for T and D is incorrect you cannot cast Double wrapper to int.
 
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Well this is really a very good question..a proper explanation to this will really clear your concept regarding wild card and type parameter in Generics:

let me try to explain how compiler use your code here..!!

This line has a quite complex meaning for compiler:

public <T extends Comparable> T findLarger(T x, T y) {

lets break it in different part:

< T extends Comparable >

As per i think, Compiler use this to accept only those parameter which implements Comparable.
for ex: if you try to call this method by passing:
Object and Object --> compiler will not accept, because Object doesn't implements Comparable,but if you pass:
Integer and Integer --> Compiler will accept.
Integer and String ---> Compiler will accept as both implements Comparable
Compiler treats both arguments differently..!!

This part has only this role.

return type T --> It can return only type T.

now lets consider your Problem option one by one:

Option 1)
A. Object x = t.findLarger(123, �456�);

now see
123 --> Integer --> implements Comparable
456 --> String ---> implements Comparable

Now how compiler can accept String and Integer ?

here compile do a conversion...it sees is there is something specific to both..
for eg:
Integer extends Object
String extends Object

So compiler see both as Object and accept them..so from compiler point of view:
T x ---> Object x --> Object x = new Integer(123)
T y ---> Object y --> Object y = new String(456);

and we can call Object.compareTo(Object)

again return type is int --> Integer --> extends Object
so this works fine.!!

/**********************************************/
Option 2)
B. int x = t.findLarger(123, new Double(456));

Here
123 ---> Integer ---> Number
456 ---> Double ----> Number

But problem is that we can not call : Number.compareTo(Number)
type mismatch between Number and comparable<?> --> Comparable< ? extends Object>

/*******************************************************/
Option 3)
C. int x = t.findLarger(123, new Integer(456));

since both are Integer here..so T refers to Integer..so rest is Obvious..

/****************************************************/

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

T refers to Double..
so return type is also Double..!!
and we can not convert double to int..inconvertible type..!!!
/*************************************************/

this all explanation was as per my understanding on the issue..!!!
 
Lata Bagga
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to all now I got the concept. Sunny you cleared all my doubts regarding this question. Thank you so much!!
 
There are 10 kinds of people in this world. Those that understand binary get this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic