3
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?

3
  • Hm, everything correct. What are you expected? Commented Sep 22, 2017 at 5:48
  • 1
    this is the same as 0 + (-1234) Commented Sep 22, 2017 at 6:03
  • Bcz the value of - is higher than + so it assign - sign and thats why your answer is -1234 Commented Sep 22, 2017 at 6:24

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.