0

public class Main {

public static void main(String[] args) {
   byte x = 126;
    short y = 32734;
    int z = 32789;
    long a = 50000L + 10L * (x + y + z);
    System.out.println(a);

    short b = (short) (1000 + 10 * (x + y + z));
    System.out.println(b);
}

}

Output : 706490 2130 Why I'm getting b = 2130?

  • What number do you expect? And what do you know about `short`? – tgdavies Sep 11 '21 at 05:40
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Sep 17 '21 at 09:22

2 Answers2

0

Here are some facts from which you can assemble the answer:

  • short is a 16 bit signed integer type, with max value 32767
  • integer truncation occurs if overflowing with narrowing cast
  • 1000 + 10 * (126 + 32734 + 32789) is 657490
  • 657490 % 32768 is 2130
Bohemian
  • 412,405
  • 93
  • 575
  • 722
-1

Reason is java Narrowing or Explicit Conversion. Refer this *