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


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

Re: Every expression has a type?

From Robert Klemme <shortcutter@googlemail.com>
Newsgroups comp.lang.java.programmer
Subject Re: Every expression has a type?
Date 2017-10-31 14:40 +0100
Message-ID <f5rcqjFogckU1@mid.individual.net> (permalink)
References <Every-20171030130857@ram.dialup.fu-berlin.de> <m3po94xrte.fsf@sonera.fi> <null-20171030231708@ram.dialup.fu-berlin.de> <m3d1537i2r.fsf@sonera.fi>

Show all headers | View raw


On 31.10.2017 11:00, Jukka Lahtinen wrote:
> ram@zedat.fu-berlin.de (Stefan Ram) writes:
>> Jukka Lahtinen <jtfjdehf@hotmail.com.invalid> writes:
>>> How about null?
>>> Is it cosidered to have a type, since null can be any type at all?
> 
>>    I have written this exercise.
>>    Try to predict the output of the following program.
> 
>> public final class Main
>> {
>>    private static void f( final java.lang.Object o )
>>    { java.lang.System.out.println( "Bject" ); }
>>
>>    private static void f( final java.lang.String s )
>>    { java.lang.System.out.println( "Tring" ); }
>>
>>    public static void main( final java.lang.String[] args )
>>    { f( null ); }}
> 
> Actually I'm surprised of the output of that.
> Just the opposite of what I thought.

I'd guess the compiler picks the most specific type - and since Object 
is a superclass of String it will pick String. If you add a similar 
method with CharSequence argument the situation does not change.  But if 
you add a method with argument String[] or Integer the compiler complains

The method f(Object) is ambiguous for the type Main

Now all of a sudden there are two specific types between which the 
compiler cannot chose automatically.

Kind regards

	robert

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

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


Thread

Re: Every expression has a type? Jukka Lahtinen <jtfjdehf@hotmail.com.invalid> - 2017-10-30 23:13 +0200
  Re: Every expression has a type? Eric Sosman <esosman@comcast-dot-net.invalid> - 2017-10-30 18:07 -0400
  Re: Every expression has a type? Jukka Lahtinen <jtfjdehf@hotmail.com.invalid> - 2017-10-31 12:00 +0200
    Re: Every expression has a type? Robert Klemme <shortcutter@googlemail.com> - 2017-10-31 14:40 +0100

csiph-web