10

I would like to know the reason why byte and short values are promoted to int whenever an expression is evaluated or a bit-wise operation is processed?

Shabbir Dhangot
  • 8,954
  • 10
  • 58
  • 80
Frederic Nault
  • 986
  • 9
  • 14
  • 1
    Both if them are not my question – Frederic Nault Dec 20 '14 at 16:52
  • 3
    You're right - they're not. But the accepted answer to the second one contains a really good discussion on your question. – Paul Boddington Dec 20 '14 at 16:58
  • For me its not the answer I want at all. I want compiler reason behind auto promoting type during evaluate predicate and or during bitwise logic, if possible comparing what other languaage doest, . I kind mad about poeple here being more grind to downvote than to help. HAving a -2 for a good answer... – Frederic Nault Dec 20 '14 at 17:02
  • 2
    You can't always get what you want. But if you try sometime you find you get what you need. – Gilbert Le Blanc Dec 20 '14 at 17:20
  • http://stackoverflow.com/questions/5662685/point-of-byte-and-short-in-java-ive-read-the-other-questions/ – assylias Dec 20 '14 at 18:31
  • Because there has got to be some sort of rule, and that's a simple, easy to remember, easy to implement rule. In particular, the JVM only does integer arithmetic in 32 and 64-bit units. – Hot Licks Dec 20 '14 at 19:12

1 Answers1

13

Because the Java Language Specification says so. Section 5.6.1 defines unary numeric promotion for evaulation of certain operators, and it says:

  • If the operand is of compile-time type byte, short, or char, it is promoted to a value of type int by a widening primitive conversion (§5.1.2).

Section 5.6.2 on evaluation of binary numeric operators ('binary' meaning operators that have two operands, like '+') says something similar:

  • If either operand is of type double, the other is converted to double.
  • Otherwise, if either operand is of type float, the other is converted to float.
  • Otherwise, if either operand is of type long, the other is converted to long.
  • Otherwise, both operands are converted to type int.

Why was it defined this way? A major reason is that at the time of design of the Java language and Java virtual machine, 32-bit was the standard word size of computers, where there is no performance advantage to doing basic arithmetic with smaller types. The Java virtual machine was designed to take advantage of this, by using 32-bit as the int size, and then providing dedicated instructions in the Java bytecode for arithmetic with ints, longs, floats, and doubles, but not with any of the smaller numeric types (byte, short, and char). Eliminating the smaller types makes the bytecode simpler, and lets the complete instruction set, with room for future expansion, still fit the opcode in a single byte. Similarly, the JVM was designed with a bias towards easy implementation on 32-bit systems, in the layout of data in classes and in the stack, where 64-bit types (doubles and longs) take two slots and all other types (32-bit or smaller) take one slot.

So, the smaller types were generally treated as second-class citizens in the design of Java, converted to ints at various steps, because that simplified some things. The smaller types are still important because they take less memory when packed together (e.g., in arrays), but they do not help when evaluating expressions.

Boann
  • 48,794
  • 16
  • 117
  • 146