Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #37616 > unrolled thread
| Started by | Wayne <wayne@nospam.invalid> |
|---|---|
| First post | 2017-10-31 03:07 -0400 |
| Last post | 2017-10-31 13:06 -0400 |
| Articles | 3 — 2 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.
Re: Every expression has a type? Wayne <wayne@nospam.invalid> - 2017-10-31 03:07 -0400
Re: Every expression has a type? Eric Sosman <esosman@comcast-dot-net.invalid> - 2017-10-31 12:00 -0400
Re: Every expression has a type? Eric Sosman <esosman@comcast-dot-net.invalid> - 2017-10-31 13:06 -0400
| From | Wayne <wayne@nospam.invalid> |
|---|---|
| Date | 2017-10-31 03:07 -0400 |
| Subject | Re: Every expression has a type? |
| Message-ID | <ot97eq$47m$1@dont-email.me> |
On 10/30/2017 8:29 AM, Stefan Ram wrote:
> I was flabbergasted. I had made some handwritten notes
> during my course, and one read:
>
> _Jeder_ Ausdruck hat einen Typ.
>
> indicating that I wrote "Jeder Ausdruck hat einen Typ."
> ("Every expression has a type.") in my published HTML
> course notes, and that there was something wrong with
> "Jeder" ("Every").
>
> 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?
> So, eventually, I discarded my note.
>
> Still, I was wondering ...
>
> Then it struck my mind!
>
> The JLS says:
>
> THE Java programming language is a statically typed
> language, which means that every variable and every
> expression has a type that is known at compile time.
>
> But what's the type of a call of a method with the result
> (jls9:8.4.5) »void«?
>
> In Java, »void« is not a type!
>
> So, »java.lang.System.out.println()« is an expression,
> but does it have a type?
>
See 15.1, where all that is spelled out (Java 9 version):
<https://docs.oracle.com/javase/specs/jls/se9/html/jls-15.html#jls-15.1>
"An expression denotes nothing if and only if it is a method invocation (§15.12) that
invokes a method that does not return a value, that is, a method declared void
(§8.4). Such an expression can be used only as an expression statement (§14.8) or as
the single expression of a lambda body (§15.27.2), because every other context in
which an expression can appear requires the expression to denote something. ..."
So, an expression consisting of a void method invocation has no result and thus
no type.
--
Wayne
[toc] | [next] | [standalone]
| From | Eric Sosman <esosman@comcast-dot-net.invalid> |
|---|---|
| Date | 2017-10-31 12:00 -0400 |
| Message-ID | <ota6ne$6u0$1@dont-email.me> |
| In reply to | #37616 |
On 10/31/2017 11:03 AM, Stefan Ram wrote:
> Wayne <wayne@nospam.invalid> writes:
>> See 15.1, where all that is spelled out (Java 9 version):
> ...
>> (§8.4). Such an expression can be used only as an expression statement (§14.8) or as
>> the single expression of a lambda body (§15.27.2), because every other context in
>> which an expression can appear requires the expression to denote something. ..."
>
> My compiler is fine with this one:
>
> public final class Main
> {
>
> private static void nothing() {}
>
> public static void main( final java.lang.String[] args )
> { for( nothing(); true; nothing() )break; }}
JLS 14.14.1 tells us that the first and third pieces of
"BasicForStatemet" are "ForInit" and "ForUpdate", respectively.
Both of these are "StatementExpressionList"s, which are comma-
separated lists of "StatementExpression"s. A "StatementExpression"
is that part of an "ExpressionStatement" preceding the semicolon
(14.8), so all is well.
(By the way, I just found yet another infinity of angels holding
a cotillion on that zero-sized pinhead.)
--
esosman@comcast-dot-net.invalid
Eleven hundred seventy-seven days to go.
[toc] | [prev] | [next] | [standalone]
| From | Eric Sosman <esosman@comcast-dot-net.invalid> |
|---|---|
| Date | 2017-10-31 13:06 -0400 |
| Message-ID | <otaair$79q$1@dont-email.me> |
| In reply to | #37616 |
On 10/31/2017 11:13 AM, Stefan Ram wrote:
> [...]
> We can have types of expressions, which do not have any
> value at all. For example consider this program:
>
> public final class Main
> {
>
> public static double test( final int i ){ return i; }
>
> public static void main( final java.lang.String[] args ) {} }
>
> The compiler will check that the type »int« of the return
> expression »i« is a subtype of the type of the return type
> »double«. So, »i« has a type. But »i« does never have a value
> in this program.
The compiler doesn't require that the type of `i' be a subtype
of the return type. If it did, the example would not compile: `int'
is not a subtype of `double'. The compiler requires, rather, that
the expression in the `return' produce a value that is convertible
to the method's returned type, and that's a far weaker requirement.
> So, »i« is an expression, it has a type, but not a value.
I think all you're saying is that code not executed doesn't
execute. That is, what you say of `i' is also true of `args':
If the main() method is not called, `args' has no value.
> So, it actually would be perfectly fine for the call of
> a void-method to have a type but not a value, as is the
> case in many other languages like, for example, C, where
> »void« is a type.
`void' is a type in C, but not a type in Java. `if' can test an
`int' expression in C, but not in Java. `->' is an operator in C, but
not in Java. `goto' is a statement in C, but a reserved and meaningless
word in Java. So what? Different languages assign different meanings
to similar symbols. `Gift' in English is something that you'd like to
receive, but that in German you'd probably refuse.[*]
[*] https://translate.google.com/#de/en/Gift
--
esosman@comcast-dot-net.invalid
Eleven hundred seventy-seven days to go.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web