Java expression compilation error [duplicate]
Asked Answered
B

4

9

I'm wondering why this code doesn't compile:

 int x=-3;
    System.out.println(x-----x);

Whereas this code does:

 int x=-3;
    System.out.println(x--- --x);

I think the priority is for pre and then post decrements then the subtraction should be applied.

Brezin answered 10/11, 2015 at 8:27 Comment(1)
There is a difference between x---x and x-- - x. How is the compiler supposed to know whether you mean x - --x or x-- - x ?Extricate
C
12

x-----x is evaluated from left to right :

((x--)--)-x

The first x-- returns -3, and you can't apply the -- operator on a value (-3), only on a variable (such as x).

That's why you get Invalid argument to operation ++/-- error.

When you add a space - x--- --x

It's evaluated as (x--)- (--x)

                -3 -   -5    = 2
Crossbred answered 10/11, 2015 at 8:31 Comment(4)
Is there a reason why it chose to evaluate x----- as (x--)--)- and not (x--)-)--?Salinas
Can we add that to the answer? :3Salinas
@Keale. No, because it has nothing to do with operator precedence. Operator precedence only kicks in when we already know which operators occur in which position in the input, and here that is ambiguous.Readership
@Eran, please add your comment to the answer above, it's big deal and deserves to add!Brezin
R
4

x-----x is parsed as (x--) -- -x, and here -- is applied to an expression which is not a variable. That is not allowed.

The reason for this is the following. The first stage in parsing is tokenizing the input stream: the input stream, which consists of characters, is grouped in chunks which are called tokens. Tokens are strings which are meaningful to Java, e.g. keywords, operators, or identifiers.

Tokenizing is greedy: as long as another character can be added to the token such that it is still a valid token, the character is added. Thus, for example forLoop is considered as a single identifier, and not as the keyword for followed by the identifier Loop.

The strings - and -- are both valid tokens in Java. So when the tokenizer encounters ---, it reads the first character. Although it knows that - is a valid token, it first looks at the next character, and decides that -- is a valid token also, so the first token returned will be --, not -.

Readership answered 10/11, 2015 at 8:53 Comment(0)
O
4

This is directly covered by the Java Language Specification, §3.2. Lexical Translations

The longest possible translation is used at each step, even if the result does not ultimately make a correct program while another lexical translation would. There is one exception: if lexical translation occurs in a type context (§4.11) and the input stream has two or more consecutive > characters that are followed by a non-> character, then each > character must be translated to the token for the numerical comparison operator >.

The input characters a--b are tokenized (§3.5) as a, --, b, which is not part of any grammatically correct program, even though the tokenization a, -, -, b could be part of a grammatically correct program.

So since this is not a type context and not about > characters either, the rule of the longest token applies. So x-----x is tokenized as x, --, --, -, x much like the cited example.

Orientalize answered 10/11, 2015 at 10:11 Comment(0)
A
1

The first example is like a subtraction

int x=-3;
System.out.println(x-----x);

The second one is like minimize x -> its the same like

x++ => x=x+1

What you did is there something like

x-- => x=x-1

and the second part:

--x 

does first subtract -1 from the variable

Antagonize answered 10/11, 2015 at 8:31 Comment(2)
This doesn't answer the question.Vogul
@UmaKanth you've right, but i thought its clear after this explanationAntagonize

© 2022 - 2024 — McMap. All rights reserved.