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

How to read this Generics method

 
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why line #1 is allowed? but not line #2?
thanks

 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Object x1 = TestType.findLarger(123, "456"); // #1
Line #1 will compile but it throws classcast exception becuase integer can not be compared with string

//int x2 = TestType.findLarger(123, new Double(456)); // #2
Line #2 does not compile because Dobule can't fint into int


int x3 = TestType.findLarger(123, new Integer(456)); // #3
Line #3 will compile and run with output 456 if you print it as s.o.p(x3)
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Manju, Could you please explain more about Line#2
I changed the type of x2 to double. Even then it is showing error.
 
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Priya,
To understand this probelm lets start with

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


Read the above line as a method which can take two argument x and y or Type T and returns T where T is anything which extends Comparable.
Passing any primitive type will be boxed to their respestive wrapper class.Passing double or say Double will return double/Double(autoboxing/autounboxing) which can be assigned to any thing which is bigger than double,so double.Assingin the method return to int will require a exlicit cast.
 
Priya Viswam
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sanjeev Kumar Singh:
Passing double or say Double will return double/Double(autoboxing/autounboxing) which can be assigned to any thing which is bigger than double,so double.Assingin the method return to int will require a exlicit cast.



Then why it is showing error, even after changing the int variable x2 to double.

double x2 = TestType.findLarger(123, new Double(456)); // #2
Showing the error - incompatible types.
 
Manju Devarla
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, it gives incompatible types ..we cant compare int and double

so if you chnage the code to

double x1 = (double) t.findLarger(123.0, new Double(456));

or

double x1 = (double) t.findLarger(new Double(123), new Double(456));

we need to cast it to double,it complies and gives 456.0
 
Priya Viswam
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Manju for the explanation. But i have one more doubt

As before i am passing an integer and a double
Object x5 = TestType.findLarger(123, new Double(456)); // #2

Here the value returned is stored in an Object reference. In this case it is not generating any error.

passing integer and double and storing the result in an int is creating problem.
passing integer and double and storing the result in an object ref is not a problem.

Could you please explain about these behaviour also?
 
Sanjeev Singh
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Priya,
Thanks for making the note of it.The type of return type can not be determined unless casted.I was under the wrong implession that the bigger argument in the method will be the return type of the method which is not true.So it is neccessay to cast the method either with double or Double.

passing integer and double and storing the result in an object ref is not a problem.


This will be always OK as Object class is the ultimate BOSS of all the objects.
[ January 18, 2007: Message edited by: Sanjeev Kumar Singh ]
 
New rule: no elephants at the chess tournament. Tiny ads are still okay.
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic