Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Weird Java Syntax

Tags:

java

syntax

I was doing a practice Computer Science UIL test form when I came across this problem:

What is output by the following?

int a = 5;
int b = 7;
int c = 10;
c = b+++-c--+--a;
System.out.println(a + " " + b + " " + c);

I put down the answer "No output due to syntax error" but I got it wrong. The real answer was 4 8 1! (I tested it out myself)

Can someone please explain to me how line 4 works?
Thanks

like image 959
Will Sherwood Avatar asked Aug 04 '13 05:08

Will Sherwood


1 Answers

I added some parentheses:

int a = 5;
int b = 7;
int c = 10;
c = (b++) + (-(c--)) + (--a);
System.out.println(a + " " + b + " " + c);

b++ : b = b + 1 after b is used

c-- : c = c - 1 after c is used

--a : a = a - 1 before a is used

like image 113
Akinakes Avatar answered Sep 21 '22 11:09

Akinakes