• 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

doubt on wrappers

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Ranch Hand
Posts: 826
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Would you mind stating the doubt or the problem or the error, please...
 
saipavan vallabhaneni
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[LIST][*]why is the 1st method invokation getting comppiled even when the types are different
[*]why is showing error for 4th invokation to which the values passed are of same type and result is being casted to int ???
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

1.Object z = t.findLarger(123, "456"); //y is this getting compiled??? when the parameters being passed are different types
2.int m = t.findLarger(123, new Double(456)); compiler shows error
3.int y = t.findLarger(123, new Integer(456)); compiler shows error
4.int x = (int) t.findLarger(new Double(123), new Double(456)); what's wrong with this??? we are passing the same type of args to this ...y is it askin req int



1.Object z = t.findLarger(123, "456");
it will become t.findLarger(Object,Object); why?

and findLarger :
public Object findLarger(Object x, Object y){...}




As 123 is int, but "456" is String object, both differs so they will be upcasted to same parent Object the Object class.
123 will be boxed to Integer object than it will be upcasted to Object.

2.
int m = t.findLarger(123, new Double(456)); compiler shows error

it will be called as:
int m = t.findLarger(new Object(123),new Object(new Double(456)));

and againe findLarger:

public Object findLarger(Object x, Object y){...}



Here findLarger() returning Object instance that cannot be converted to int.
so compiler error.


3.int y = t.findLarger(123, new Integer(456)); compiler shows error
this will not show any error...
as it will become
int y = t.findLarger(new Integer(123), new Integer(456));

and findLarger:
public Integer findLarger(Integer x, Integer y){...}



Here findLarger() returns Integer that can be auto unboxed to int by compiler.


4.int x = (int) t.findLarger(new Double(123), new Double(456)); what's wrong with this??? we are passing the same type of args to this ...y is it askin req int.

findLarger:
public Double findLarger(Double x, Double y){...}


Here findLarger() returning Double object, and Double cannot be casted in int. so ...
 
saipavan vallabhaneni
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Punit,
thanks for the detailed explanation
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think that the first case
(#1) Object z = t.findLarger(123, "456");
will become t.findLarger(Object,Object).

First of all, Object does not implement Comparable so it would not work with findLarger's definition i.e.
public <T extends Comparable> T findLarger(T x, T y)

Secondly, if you add 2 getClass().getName() displays and run the code it shows that the two parms are coming in as Integer and String, i.e.



WrapperCheck class of x java.lang.Integer
WrapperCheck class of y java.lang.String

Note that you must comment out the compareTo logic as it fails at runtime.
[ December 12, 2008: Message edited by: patrick avery ]
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


  • Object z = t.findLarger(123, "456"); - It is not giving compile error as return type is Object class and ideally we can convert any object to Object class, but this statment will fail while running compateTo() method..
  • int m = t.findLarger(123, new Double(456)); - It will give compile time error as return type expected in int and we are passing one object of type Double() which cannot be type cast to int

  • int y = t.findLarger(123, new Integer(456)); -This should run perfectly
  • int x = (int) t.findLarger(new Double(123), new Double(456)); - It has same case as type casting...

  • if you remove all left side parameter i.e. Object z, int x etc. you will not get any complie time error.. so as per my understanding here return type compatiblity is checked at complie time
     
    Ranch Hand
    Posts: 77
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    well Mohit i am a bit confused regarding T

    Object z = t.findLarger(123, "456");

    if this is the call to the following function
    public <T extends Comparable> T findLarger(T x, T y)

    then it should be

    public <Object extends Comparable> Object findLarger(Object x,Object y)

    but why doesn't it give compiler error as Object doesn't implements Comparable.

    Please clarify it.
     
    Ranch Hand
    Posts: 67
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Reviving this old thread as I have the same query and it is unanswered here. Checked other threads related to this question.
    - Object does not implement Comparable then why it is accepted?
    - What exactly determines the type T in this method?
     
    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
    Well the wrappers in Object z = t.findLarger(123, "456"); are not boxed and widened to Object but instead they are simply boxed. As explained above, at runtime it will give a class cast exception as Integer and String cannot be compared.


    the Above code will give an error saying that the method cannot be applied for Object.


    The type is determined by <T extends Comparable> of public <T extends Comparable> T findLarger(T x, T y) means whatever T you pass must extend Comparable. Its a check that's all.
     
    Ranch Hand
    Posts: 37
    Hibernate Spring Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    2.
    int m = t.findLarger(123, new Double(456)); compiler shows error

    it will be called as:
    int m = t.findLarger(new Object(123),new Object(new Double(456)));

    and againe findLarger:

    public Object findLarger(Object x, Object y){...}



    well Punit.. as you said if the two arguments differ then they will be upcast to same object. So in the above case first arg will become Integer and second Double and then they will be upcast to their parent Object, i.e., Number class. So following will compile fine:


     
    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
    Sanjay please the date of the post for punit. He may not even see this reply of yours.
     
    If you live in a cold climate and on the grid, incandescent light can use less energy than LED. Tiny ad:
    Gift giving made easy with the permaculture playing cards
    https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
    reply
      Bookmark Topic Watch Topic
    • New Topic