-2

Why do I get an error in this program?

If I change the middle operator to minus like this (a++-++b), it is executing without any error:

class Demo
{  
    public static void main(String []args){

        int a=10;
        int b=20;

        System.out.println (a+++++b);

     }
}
user unknown
  • 35,537
  • 11
  • 75
  • 121
javaMahesh
  • 21
  • 1
  • 1

6 Answers6

1

The parser is unable to see what you want it to see. You want it to see

(a+++++b)

as

(a++ + ++b)

but, the parser is seeing it as

(a++++ +b)

and therefore, it is throwing a compilation error as ++ operator is expecting a variable but it is getting a++, which is a value.

ncoder
  • 151
  • 1
  • 8
0

The subtract sign works because it is identified as a separate operator from the series of additions. The additions however appear to become one string of operators. if you just add a space between the addition signs it should work fine as it will identify the addition operator separately.. eg System.out.println(a++ + ++b);

Marko
  • 1
0
public class Demo {
    public static void main(String[] args) {
        int a=10;
        int b=20;
        System.out.println(a++ + ++b);
    }
}

// output
31

I think you might want this, don't use +++ it's not valid operator in java, BTW, you should post better question next time.

yuzhen
  • 127
  • 1
  • 9
0

For

(a++-++b);

The parser knows, that there is no such thing as an +- or -+ operator on int. So it can split the expression in (a++ - ++b);

For

(a+++++b);

This can be (a+ ++... or (a++ +... but let's first have a look at a simpler expression:

int a = 100;
int b = 0;
-> int c=a+++b;
|  Added variable c of type int with initial value 100

What is the desired result? a++ +b or a+ ++b? Well, it's interpreted as c= (a++)+b; but it's bad style, since you have to have the details of operator precedence in head.

Is ++++b allowed, or a++++?

-> ++++b;
|  Error:
|  unexpected type
|    required: variable
|    found:    value
|  ++++b;
|    ^-^

-> a++++;
|  Error:
|  unexpected type
|    required: variable
|    found:    value
|  a++++;
|  ^-^

No. Why not? You could argue, that it is (a++)++; or ++(++b); But both include two steps, increment by one and returning a value, so for

a=100
b=0
c=a++;
c=++b;

we know, that c will become 100 or 1, in one case, the return is performed before incrementing (c=a++), in the other case after (++b);

Now what should happen with ++++b; or ++(++b); What get's returned?

Considering that, the only valid interpretation for a+++++b is a++ + ++b; There is no a+ ++ ++b, nor a+ +++ +b.

And a++ + ++b; works:

-> c=a++ + ++b;
|  Variable c has been assigned the value 101

But since it is very rarely used, and not a big deal to insert two spaces, and more reader friendly, anyhow, we should live with the inconsistency, of a+++++b being rejected, while a++-++b is allowed, which doesn't make the latter a nice to read expression. Horizontal spaces make code better parsable for humans too, and that should be your main concern.

If you like to drive someone crazy, use

c=a--- - --b;

since there is this unary -, which is handy for producing negative values like -7.

user unknown
  • 35,537
  • 11
  • 75
  • 121
0

Here, as + operator and ++ operator have equal precedence (similarly, - and -- have equal precedence.) So, +++ and --- gives an error as it will be broken into '+' and '++' ('-' and '--').

To solve this error, we can use +(++).

Hope that helps.

Laveena
  • 97
  • 1
  • 10
0

a+++++b ,a++---b,a------b and a--+++b will throw an error because + and ++ have same precedence.So Java will throw invalid argument for +++b,---b,---b and +++b respectively.

bhargav
  • 1
  • 3