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


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

Re: Multichar tokens

Started byNigel Wade <nmw@local.domain>
First post2016-08-15 10:17 +0100
Last post2016-08-15 10:17 +0100
Articles 1 — 1 participant

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: Multichar tokens Nigel Wade <nmw@local.domain> - 2016-08-15 10:17 +0100

#35348 — Re: Multichar tokens

FromNigel Wade <nmw@local.domain>
Date2016-08-15 10:17 +0100
SubjectRe: Multichar tokens
Message-ID<e1dflaFsqfU1@mid.individual.net>
On 13/08/16 23:05, Stefan Ram wrote:
> In Java, »- -2« must be written as »- -2« and not as »--2«,
>    because there also is a special »--« operator. S

Surely there is no ambiguity, --2 would be an illegal operation since you cannot decrement a constant.

The Java lang. specification says:
"The result of the unary [++/--]expression must be a variable of a type that is convertible (§5.1.8) to a numeric type, or a compile-time error occurs."

Since 2 is not a variable it's a compile-time error:

Test.java:6: error: ';' expected
           i = i --2;
                   ^
Test.java:7: error: ';' expected
           i--2;
              ^
2 errors

Neither format of --2 is allowed. Are there any others?

If you are worried about any potential ambiguity, remove it with parentheses.
-(-2) has no ambiguity, and no reason to be misinterpreted.

class Test {
   public static void main(String[] args) {
     int i = 3;
     i = i -(-2);
     System.out.println(i);
   }

$ javac Test.java
$ java Test
5

[toc] | [standalone]


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


csiph-web