Reputation: 171
public class Main {
public static void main(String[] args) {
int j = + -1234;
System.out.printf("%d", j);
System.out.println();
System.out.println(j);
}
}
The Result is -1234. Can any body explain me why the result is -1234 is coming?
Upvotes: 3
Views: 65
Reputation: 1239
The assigment int j = + -1234
; is equivalent to:
j = (1) * (-1) * 1234 (a)
now:
-1 = (1) * (-1) (b)
so substitute b into a and get:
j= -1 * 1234
so j = -1234
In the assignment equation the + and - are acting as unary oprators
Upvotes: 1
Reputation: 181
Actually the java compiler take +- as +- so it results to -1234. if you try -+-1234 then it will processed as -+*-1234 which is 1234.
public class Main {
public static void main(String[] args) {
int j = -+ -1234;
System.out.printf("%d", j);
System.out.println();
System.out.println(j);
} }
This will print 1234. you cannot use ++/-- since it is already predefined in java for increment and decrement operation
Upvotes: 0