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


Groups > comp.lang.java.programmer > #37611 > unrolled thread

Re: Every expression has a type?

Started byJukka Lahtinen <jtfjdehf@hotmail.com.invalid>
First post2017-10-30 23:13 +0200
Last post2017-10-31 14:40 +0100
Articles 4 — 3 participants

Back to article view | Back to comp.lang.java.programmer

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  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

#37611 — Re: Every expression has a type?

FromJukka Lahtinen <jtfjdehf@hotmail.com.invalid>
Date2017-10-30 23:13 +0200
SubjectRe: Every expression has a type?
Message-ID<m3po94xrte.fsf@sonera.fi>
ram@zedat.fu-berlin.de (Stefan Ram) writes:

>   But I can't remember what I was thinking of when I wrote
>   this. How can there be an expression in Java without a type?

How about null?
Is it cosidered to have a type, since null can be any type at all?

-- 
Jukka Lahtinen

[toc] | [next] | [standalone]


#37613

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2017-10-30 18:07 -0400
Message-ID<ot87rs$mv9$1@dont-email.me>
In reply to#37611
On 10/30/2017 5:13 PM, Jukka Lahtinen wrote:
> ram@zedat.fu-berlin.de (Stefan Ram) writes:
> 
>>    But I can't remember what I was thinking of when I wrote
>>    this. How can there be an expression in Java without a type?
> 
> How about null?
> Is it cosidered to have a type, since null can be any type at all?

     `null' is a literal whose value is the null reference (JLS 3.10.7).
The type of the null reference is the null type (3.10.7, 4.1).  Unlike
other types, the null type has no name, hence one can neither cast a
value to the null type nor declare a variable of that type (4.1).

     When the null value is assigned to a variable (or provided as a
method's return value), the value is converted from the null type
to a null value of the appropriate reference type (5.2).  It seems
to me that a consequence is that one can never "get one's hands on"
the null type nor its lone value `null', because in any context
where the value can appear it is converted to some other reference
type "instantaneously."

     In other news, an infinite number of angels can dance on a
pinhead of zero size.

-- 
esosman@comcast-dot-net.invalid
Eleven hundred seventy-eight days to go.

[toc] | [prev] | [next] | [standalone]


#37617

FromJukka Lahtinen <jtfjdehf@hotmail.com.invalid>
Date2017-10-31 12:00 +0200
Message-ID<m3d1537i2r.fsf@sonera.fi>
In reply to#37611
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.

-- 
Jukka Lahtinen

[toc] | [prev] | [next] | [standalone]


#37625

FromRobert Klemme <shortcutter@googlemail.com>
Date2017-10-31 14:40 +0100
Message-ID<f5rcqjFogckU1@mid.individual.net>
In reply to#37617
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/

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web