Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #33327 > unrolled thread
| Started by | Philipp Kraus <philipp.kraus.flashpixx@gmail.com> |
|---|---|
| First post | 2015-12-10 07:44 -0800 |
| Last post | 2015-12-11 09:27 +0100 |
| Articles | 17 — 6 participants |
Back to article view | Back to comp.lang.java.programmer
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
| From | Philipp Kraus <philipp.kraus.flashpixx@gmail.com> |
|---|---|
| Date | 2015-12-10 07:44 -0800 |
| Subject | method parameter binding |
| Message-ID | <6bfa4a78-934e-4f90-8e5f-a2f56e529842@googlegroups.com> |
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?
Thanks
Phil
[toc] | [next] | [standalone]
| From | Eric Douglas <e.d.programmer@gmail.com> |
|---|---|
| Date | 2015-12-10 07:53 -0800 |
| Message-ID | <b3b24809-56ed-4aa5-ac5f-f5893571d451@googlegroups.com> |
| In reply to | #33327 |
On Thursday, December 10, 2015 at 10:44:55 AM UTC-5, 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?
>
> Thanks
>
> Phil
I pasted your method overloads exactly in Eclipse, then added static in front so I could call them from main(), then put this line in my main method:
System.out.println(get(new Double(5), new Double(4)));
My output was:
9.0
[toc] | [prev] | [next] | [standalone]
| From | Jan Burse <janburse@fastmail.fm> |
|---|---|
| Date | 2015-12-10 17:14 +0100 |
| Message-ID | <n4c8dt$kki$1@news.albasani.net> |
| In reply to | #33328 |
Hi, Eric Douglas schrieb: > I pasted your method overloads exactly in Eclipse, then added static in front so I could call them from main(), then put this line in my main method: > System.out.println(get(new Double(5), new Double(4))); > My output was: > 9.0 Static method resolution is slightly different than dynamic method resolution. In dynamic method resolution a sub class could still have overridden the method and thrown the exception. Need more code... Bye
[toc] | [prev] | [next] | [standalone]
| From | Eric Douglas <e.d.programmer@gmail.com> |
|---|---|
| Date | 2015-12-10 08:20 -0800 |
| Message-ID | <aa4751c4-f52e-4585-8dab-c99eb09bb69f@googlegroups.com> |
| In reply to | #33329 |
On Thursday, December 10, 2015 at 11:15:10 AM UTC-5, Jan Burse wrote:
> Static method resolution is slightly different than
> dynamic method resolution.
>
> In dynamic method resolution a sub class could still
> have overridden the method and thrown the exception.
>
> Need more code...
>
> Bye
I'm not sure I get what you're saying. Certainly if it's an override it should choose that.
class One {
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());
}
}
Class Two extends One {
Number get( Number x, Number y )
{
throw new IllegalStateException("test");
}
}
[toc] | [prev] | [next] | [standalone]
| From | Jan Burse <janburse@fastmail.fm> |
|---|---|
| Date | 2015-12-10 17:23 +0100 |
| Message-ID | <n4c8tt$lev$2@news.albasani.net> |
| In reply to | #33330 |
Eric Douglas schrieb:
> Class Two extends One {
> Number get( Number x, Number y )
> {
> throw new IllegalStateException("test");
> }
> }
If you override get( Double x, Double y )
or get( Long x, Long y ) you can also throw
an exception there.
For static methods, you can override like
that, I mean you would write Two.get and
not One.get, but this is not overriding,
for static methods if you write One.get
you get One.get.
For dynamic methods if you write x.get and
x is of type One, you might also get
Two.get invoked.
Bye
[toc] | [prev] | [next] | [standalone]
| From | Jan Burse <janburse@fastmail.fm> |
|---|---|
| Date | 2015-12-10 17:23 +0100 |
| Message-ID | <n4c8ue$lev$3@news.albasani.net> |
| In reply to | #33332 |
Jan Burse schrieb: > For static methods, you can override like can --> cannot
[toc] | [prev] | [next] | [standalone]
| From | Jan Burse <janburse@fastmail.fm> |
|---|---|
| Date | 2015-12-10 17:21 +0100 |
| Message-ID | <n4c8q6$lev$1@news.albasani.net> |
| In reply to | #33329 |
Jan Burse schrieb:
> Need more code...
Works fine, as expected:
Main Method:
public static void main(String[] args) {
Test test = new Test();
System.err.println("test.get(1L, 2L)="+test.get(1L, 2L));
System.err.println("test.get(1.0, 2.0)="+test.get(1.0, 2.0));
System.err.println("test.get(1, 2)="+test.get(1, 2));
}
Results:
test.get(1L, 2L)=3
test.get(1.0, 2.0)=3.0
Exception in thread "main" java.lang.IllegalStateException: test
at protect.Test.get(Test.java:20)
at protect.Test.main(Test.java:27)
In the last case the signature is Integer,Integer and
it only finds the throwing get method, since Number is
super class of Integer.
Bye
[toc] | [prev] | [next] | [standalone]
| From | Jan Burse <janburse@fastmail.fm> |
|---|---|
| Date | 2015-12-10 17:27 +0100 |
| Message-ID | <n4c957$m15$1@news.albasani.net> |
| In reply to | #33331 |
Jan Burse schrieb:
> System.err.println("test.get(1L, 2L)="+test.get(1L, 2L));
> System.err.println("test.get(1.0, 2.0)="+test.get(1.0, 2.0));
> System.err.println("test.get(1, 2)="+test.get(1, 2));
I put on purpose System.err.println and not System.out.println,
since my IDE, IntelliJ, since some recent release scrambles
err and out, so that it doesn't show in the order the
println were emitted.
Very annoying, sometimes I don't find which exceptions
matches to which step in the program.
Bye
[toc] | [prev] | [next] | [standalone]
| From | Jan Burse <janburse@fastmail.fm> |
|---|---|
| Date | 2015-12-10 17:28 +0100 |
| Message-ID | <n4c96k$m15$2@news.albasani.net> |
| In reply to | #33334 |
Jan Burse schrieb: > Very annoying, sometimes I don't find which exceptions > matches to which step in the program. Maybe some f*** up Logger that sits somewhere inbetween.
[toc] | [prev] | [next] | [standalone]
| From | Eric Sosman <esosman@comcast-dot-net.invalid> |
|---|---|
| Date | 2015-12-10 11:35 -0500 |
| Message-ID | <n4c9gj$luo$2@dont-email.me> |
| In reply to | #33335 |
On 12/10/2015 11:28 AM, Jan Burse wrote:
> Jan Burse schrieb:
>> Very annoying, sometimes I don't find which exceptions
>> matches to which step in the program.
>
> Maybe some f*** up Logger that sits somewhere inbetween.
(Topic drift ...)
Could it be that System.out and System.err are two different
and independent PrintStreams, each with its own buffering and
making its own decisions about when to flush buffers?
Naaah, too simple ...
--
esosman@comcast-dot-net.invalid
"Don't be afraid of work. Make work afraid of you." -- TLM
[toc] | [prev] | [next] | [standalone]
| From | Martin Gregorie <martin@address-in-sig.invalid> |
|---|---|
| Date | 2015-12-10 18:00 +0000 |
| Message-ID | <n4cejp$qdp$1@dont-email.me> |
| In reply to | #33337 |
On Thu, 10 Dec 2015 11:35:48 -0500, Eric Sosman wrote:
> Could it be that System.out and System.err are two different
> and independent PrintStreams, each with its own buffering and making its
> own decisions about when to flush buffers?
>
> Naaah, too simple ...
>
That's certainly the case in the UNIX/Linux world, where stdout, which
System.out maps to, is quite extensively buffered while stderr, which
System.err uses, is unbuffered.
Normally it all works as expected if you only use stdout or stderr, but
use both, e.g. for debugging, and the actual output from mixed stdout and
stderr print statements does not necessarily appear in the same order as
the statements were executed. As Jan says, this can be confusing,
especially if you're capturing the output in a single stream using
something like:
myprog 2>&1 |less
because you know the output is much bigger than one screen's worth and
you want to search it or page through it using the 'less' utility.
Similarly, in C you can output a prompt with fprintf(stderr, "....") and
follow it immediately with a read to get user input, but if you used print
("....") or fprintf(stdout, "....") you need to call fflush(stdout) to
make sure the prompt has been displayed before attempting to read the
user input.
I don't recall whether Windows does the same.
--
martin@ | Martin Gregorie
gregorie. | Essex, UK
org |
[toc] | [prev] | [next] | [standalone]
| From | Eric Douglas <e.d.programmer@gmail.com> |
|---|---|
| Date | 2015-12-10 08:41 -0800 |
| Message-ID | <ff81c1b9-24c9-48e1-babb-2b633e520bb2@googlegroups.com> |
| In reply to | #33334 |
On Thursday, December 10, 2015 at 11:27:35 AM UTC-5, Jan Burse wrote:
> Jan Burse schrieb:
> > System.err.println("test.get(1L, 2L)="+test.get(1L, 2L));
> > System.err.println("test.get(1.0, 2.0)="+test.get(1.0, 2.0));
> > System.err.println("test.get(1, 2)="+test.get(1, 2));
>
> I put on purpose System.err.println and not System.out.println,
> since my IDE, IntelliJ, since some recent release scrambles
> err and out, so that it doesn't show in the order the
> println were emitted.
>
> Very annoying, sometimes I don't find which exceptions
> matches to which step in the program.
>
> Bye
I've only ever used Eclipse for Java code and never had such problem. Do you use IntelliJ for any particular reason other than personal preference?
Everywhere in my custom API that I need to print a standard out message for logging I just call my own custom log class, so I can just edit that class if I want to hide or redirect all messages, and I can add common text to every message. From there I call Thread.currentThread().getStackTrace() so I can show which class, which method, which line number the message came from.
[toc] | [prev] | [next] | [standalone]
| From | j4n bur53 <janburse@fastmail.fm> |
|---|---|
| Date | 2015-12-10 19:29 +0100 |
| Message-ID | <n4cg9m$50s$1@news.albasani.net> |
| In reply to | #33338 |
Eric Douglas schrieb: > I've only ever used Eclipse for Java code and never > had such problem. Do you use IntelliJ for any particular > reason other than personal preference? I saw the problem recently on IntelliJ 14.1.3, if you execute a main() inside the IDE. But it could also persist to other IDEs, Consoles, etc.. It seems there are or have been even more problems than only interleaving, when using err and out at the same time: Printing to stderr and stdout sometimes gets lost in IDEA 12.1.4 https://youtrack.jetbrains.com/issue/IDEA-110325 Bye
[toc] | [prev] | [next] | [standalone]
| From | Eric Sosman <esosman@comcast-dot-net.invalid> |
|---|---|
| Date | 2015-12-10 11:33 -0500 |
| Message-ID | <n4c9c1$luo$1@dont-email.me> |
| In reply to | #33327 |
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
[toc] | [prev] | [next] | [standalone]
| From | Jan Burse <janburse@fastmail.fm> |
|---|---|
| Date | 2015-12-10 22:34 +0100 |
| Message-ID | <n4cr4k$pqk$1@news.albasani.net> |
| In reply to | #33327 |
Hi,
BTW: Situation changes totally when the argument types
are primitive types, since then the specificity rules are
different and widening applies, I guess.
See here:
Take this example program:
public class Test {
double get( double x, double y ) {
return x+y;
}
long get( long x, long y ) {
return x+y;
}
Number get( Number x, Number y ) {
throw new IllegalStateException("test");
}
public static void main(String[] args) {
Test test = new Test();
System.err.println("test.get(1L, 2L)="+test.get(1L, 2L));
System.err.println("test.get(1.0, 2.0)="+test.get(1.0, 2.0));
System.err.println("test.get(1, 2)="+test.get(1, 2));
}
}
We get the following results:
test.get(1L, 2L)=3
test.get(1.0, 2.0)=3.0
test.get(1, 2)=3
So the two integers 1 and 2 are widened to long and
the long, long method is invoked.
Bye
Philipp Kraus schrieb:
> 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?
>
> Thanks
>
> Phil
>
[toc] | [prev] | [next] | [standalone]
| From | Eric Sosman <esosman@comcast-dot-net.invalid> |
|---|---|
| Date | 2015-12-10 17:09 -0500 |
| Message-ID | <n4ct2s$blh$1@dont-email.me> |
| In reply to | #33344 |
On 12/10/2015 4:34 PM, Jan Burse wrote:
> Hi,
>
> BTW: Situation changes totally when the argument types
> are primitive types, since then the specificity rules are
> different and widening applies, I guess.
> [...]
Unless I've missed something, nobody has yet been able to reproduce
the problem you originally reported:
- Eric Douglas tried it got the unsurprising result, but his use
of `static' may have confused things (I think it's a red herring,
but even so ...),
- Jan Burse tried it and got the unsurprising result,
- I tried it and got the unsurprising result.
Only your reported result was surprising -- and it would be well
to understand why that is so before you go looking for dark corners in
the specificity rules.
--
esosman@comcast-dot-net.invalid
"Don't be afraid of work. Make work afraid of you." -- TLM
[toc] | [prev] | [next] | [standalone]
| From | j4n bur53 <janburse@fastmail.fm> |
|---|---|
| Date | 2015-12-11 09:27 +0100 |
| Message-ID | <n4e1dh$1jo$1@news.albasani.net> |
| In reply to | #33345 |
Eric Sosman schrieb: > > Only your reported result was surprising -- and it would be well > to understand why that is so before you go looking for dark corners in > the specificity rules. Yeah, the dark force of specificity. Your friend and enemy.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web