Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #33336

Re: method parameter binding

From Eric Sosman <esosman@comcast-dot-net.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: method parameter binding
Date 2015-12-10 11:33 -0500
Organization A noiseless patient Spider
Message-ID <n4c9c1$luo$1@dont-email.me> (permalink)
References <6bfa4a78-934e-4f90-8e5f-a2f56e529842@googlegroups.com>

Show all headers | View raw


On 12/10/2015 10:44 AM, Philipp Kraus wrote:
> Hello,
>
> I have got a class with two methods like:
>
> Number get( Double x, Double y )
> {
>      return new Double(x.doubleValue() + y.doubleValue());
> }
>
> Number get( Long x, Long y )
> {
>      return new Long(x.longValue() + y.longValue());
> }
>
> Number get( Number x, Number y )
> {
>     throw new IllegalStateException("test");
> }
>
>
> If I run
>
> this.get( new Long/Double(3), new Long/Double(4) );
>
> it calls everytime the method with the exception definition. IMHO the compiler should bind the method to the exact parameter type and if the compiler cannot match the generic type bind should be used.
> In C++  I want expect this binding structure, so can I do an equal behaviour with Java?
>
> I have changed the Number to a generic type: <T extends Number> Number get( T x, T y )
> and get also the exception.
>
> Can you explain me, why the last/bottom method is used?

     No, I cannot.  Also, I cannot reproduce your problem.  With:

package esosman.misc;
public class Kraus {

    public static void main(String[] unused) {
       Kraus kraus = new Kraus();
       System.err.println("Doubles: " + kraus.get(new Double(3), new 
Double(4)));
       System.err.println("Longs: " + kraus.get(new Long(3), new Long(4)));
       System.err.println("Integers: " + kraus.get(new Integer(3), new 
Integer(4)));
    }

    Number get(Double x, Double y) {
       return new Double(x.doubleValue() + y.doubleValue());
    }

    Number get(Long x, Long y) {
       return new Long(x.longValue() + y.longValue());
    }

    Number get(Number x, Number y) {
       throw new IllegalStateException("test");
    }
}

... I get the output:

Doubles: 7.0
Longs: 7
Exception in thread "main" java.lang.IllegalStateException: test
	at esosman.misc.Kraus.get(Kraus.java:21)
	at esosman.misc.Kraus.main(Kraus.java:9)

     Are you doing something slightly different from what you've shown?

-- 
esosman@comcast-dot-net.invalid
"Don't be afraid of work. Make work afraid of you." -- TLM

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

method parameter binding Philipp Kraus <philipp.kraus.flashpixx@gmail.com> - 2015-12-10 07:44 -0800
  Re: method parameter binding Eric Douglas <e.d.programmer@gmail.com> - 2015-12-10 07:53 -0800
    Re: method parameter binding Jan Burse <janburse@fastmail.fm> - 2015-12-10 17:14 +0100
      Re: method parameter binding Eric Douglas <e.d.programmer@gmail.com> - 2015-12-10 08:20 -0800
        Re: method parameter binding Jan Burse <janburse@fastmail.fm> - 2015-12-10 17:23 +0100
          Re: method parameter binding Jan Burse <janburse@fastmail.fm> - 2015-12-10 17:23 +0100
      Re: method parameter binding Jan Burse <janburse@fastmail.fm> - 2015-12-10 17:21 +0100
        Re: method parameter binding Jan Burse <janburse@fastmail.fm> - 2015-12-10 17:27 +0100
          Re: method parameter binding Jan Burse <janburse@fastmail.fm> - 2015-12-10 17:28 +0100
            Re: method parameter binding Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-12-10 11:35 -0500
              Re: method parameter binding Martin Gregorie <martin@address-in-sig.invalid> - 2015-12-10 18:00 +0000
          Re: method parameter binding Eric Douglas <e.d.programmer@gmail.com> - 2015-12-10 08:41 -0800
            Re: method parameter binding j4n bur53 <janburse@fastmail.fm> - 2015-12-10 19:29 +0100
  Re: method parameter binding Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-12-10 11:33 -0500
  Re: method parameter binding Jan Burse <janburse@fastmail.fm> - 2015-12-10 22:34 +0100
    Re: method parameter binding Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-12-10 17:09 -0500
      Re: method parameter binding j4n bur53 <janburse@fastmail.fm> - 2015-12-11 09:27 +0100

csiph-web