Java Data Types Issues

public class Main {

  public static void main(String[] args) {
    int j = + -1234;
    System.out.printf("%d", j);
    System.out.println();
    System.out.println(j);
  }
}

Result: -1234. Can someone explain to me why the -1234 result is suitable?

+4
source share
3 answers

Assignment int j = + -1234; is equivalent to:

j = (1) * (-1) * 1234 (a)

Now:

-1 = (1) * (-1) (b)

then replace b with a and get:

j = -1 * 1234

therefore j = -1234

In the assignment equation, the + and - act as unary opponents

+1
source

Actually, the java compiler accepts + - as + - therefore it leads to -1234. if you try - + - 1234, then it will be 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 the increase and decrease operation

0

You set j to + -1234 This actually adds 0 to the value of -1234, which is the same as subtracting 1234 from 0. Therefore, it makes sense that when printing the value it will be -1234. You can check it out at Wolframalpha: http://www.wolframalpha.com/input/?i=%2B+-1234

0
source

Source: https://habr.com/ru/post/1686177/


All Articles