Path: csiph.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Robert Klemme Newsgroups: comp.lang.java.programmer Subject: Re: Every expression has a type? Date: Tue, 31 Oct 2017 14:40:33 +0100 Lines: 40 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net nNvFU5TTi9zz0ObRujcx/AVS/4Y4s6vNXcXQ3t3b/LImbT1LQ= Cancel-Lock: sha1:0b0HxixgAPx3kfC19w0DfZXUKu4= User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.4.0 In-Reply-To: Content-Language: en-US Xref: csiph.com comp.lang.java.programmer:37625 On 31.10.2017 11:00, Jukka Lahtinen wrote: > ram@zedat.fu-berlin.de (Stefan Ram) writes: >> Jukka Lahtinen 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/