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


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

Re: Multichar tokens

From Nigel Wade <nmw@local.domain>
Newsgroups comp.lang.java.programmer
Subject Re: Multichar tokens
Date 2016-08-15 10:17 +0100
Message-ID <e1dflaFsqfU1@mid.individual.net> (permalink)
References <tokens-20160813225403@ram.dialup.fu-berlin.de>

Show all headers | View raw


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

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


Thread

Re: Multichar tokens Nigel Wade <nmw@local.domain> - 2016-08-15 10:17 +0100

csiph-web