Is addition of byte converts to int because of java language rules or because of jvm?
if java supports byte datatype then why operation on byte results int
Because that's how the Java Virtual Machine is designed. There is no instruction set to perform operation on a byte type. Rather the instruction set for int
type is used for the operation on boolean
, byte
, char
, and short
types.
From JVM Spec - Section 2.11.1:
A compiler encodes loads of literal values of types
byte
andshort
using Java Virtual Machine instructions that sign-extend those values to values of typeint
at compile-time or run-time. Loads of literal values of typesboolean
andchar
are encoded using instructions that zero-extend the literal to a value of typeint
at compile-time or run-time. [..]. Thus, most operations on values of actual typesboolean
,byte
,char
, andshort
are correctly performed by instructions operating on values of computational typeint
.
The reason behind this is also specified in that section:
Given the Java Virtual Machine's one-byte opcode size, encoding types into opcodes places pressure on the design of its instruction set. If each typed instruction supported all of the Java Virtual Machine's run-time data types, there would be more instructions than could be represented in a
byte
. [...] Separate instructions can be used to convert between unsupported and supported data types as necessary.
For the details on what all instruction sets are available for various types, you can go through the table in that section.
There is also a table specifying the mapping of actual type to the JVM computational type:
Why is the sum of bytes integer?
Because the Java Language Specification says so
Binary numeric promotion is performed on the operands (§5.6.2).
Note that binary numeric promotion performs value set conversion
(§5.1.13) and may perform unboxing conversion (§5.1.8).The type of an additive expression on numeric operands is the promoted
type of its operands.
and, regarding numeric promotion,
Widening primitive conversion (§5.1.2) is applied to convert either or
both operands as specified by the following rules:
- [...]
- Otherwise, both operands are converted to type
int
.
So the byte
values are promoted to int
values and added up. The result of the expression is the promoted type, therefore an int
.
You can simply cast the result
byte z = (byte) (b + a);
but careful with overflow/underflow.
Java byte to int Coercion
In the JVM, every stack element has a size of 32 bits. The actual addition works like this:
- The two bytes are pushed on the stack as 32bit values (therefore they are
int
) - The
iadd
instruction is called, which pops two values from the stack and adds them - The resulting integer is pushed on the stack again
This is why you have to cast the resulting value (of type int
) to a byte
again.
Why adding two numbers from data type less than int is converted to int?
Tl;dr:
That's just how Java was designed.
Long version:
JVM bytecode is designed to have opcodes of only one byte. This (in theory) keeps bytecode relatively memory efficient. As a result there are only 256 possible opcodes, which significantly limits the number of supported operations. This limitation means tat Java must choose only the most common operations when allocating opcodes.
Additionaly Java bytecode was designed to be compiled to machine code, and on many machines there is no byte addition instruction, and instead all operation have to be done on 32+ bit integers, so it makes little sense to have a bytecode instruction which would have to be compiled to a sequence of byte to integer conversion, followed by addition, followed by integer to byte conversion.
This limitation of the JVM appears to have trickled down into the Java language design resulting in byte addition actually being integer addition, since that is what is supported in JVMs.
Add bytes with type casting, Java
close, but a little off.
ars[0] = (byte)(ars[0] + ars[4]);
keep in mind ars[0]
and ars[4]
are already bytes, so there is no need to cast them to bytes.
Instead, cast the result of the summation to a byte.
Add bytes with type casting, Java
close, but a little off.
ars[0] = (byte)(ars[0] + ars[4]);
keep in mind ars[0]
and ars[4]
are already bytes, so there is no need to cast them to bytes.
Instead, cast the result of the summation to a byte.
Why byte and short division results in int in Java?
The main reason is that machines usually have only add instructions for their native integer type (and floats). This is why for many languages the least used type in an arithmetic expression is int
(usually the type that correspond in some way to the basic machine integer type).
For example, i386 spec says:
ADD performs an integer addition of the two operands (DEST and SRC).
The result of the addition is assigned to the first operand (DEST),
and the flags are set accordingly. When an immediate byte is added to
a word or doubleword operand, the immediate value is sign-extended to
the size of the word or doubleword operand.
This means that internally any byte value is extended to an integer (or similar). After all this is reasonable as the processor is 32/64 bits and then perform any arithmetic in these sizes. If it could be possible to make arithmetic in bytes this is generally not considered as useful.
The JVM specs says that (for addition) you have : iadd
, ladd
, fadd
, dadd
. This just reflect the fact that underlying machines usually behave such. Any other choice could have been possible, probably at the price of performance degradation.
Related Topics
Can Not Find the Tag Library Descriptor for "Http://Java.Sun.Com/Jsp/Jstl/Core"
How Returns Xxxsize from Jcomponent(S) Added to the Jlabel
Why Do I Need Transaction in Hibernate for Read-Only Operations
Exposed Beyond App Through Clipdata.Item.Geturi
Intellij Idea with Junit 4.7 "!!! Junit Version 3.8 or Later Expected:"
Dynamic Listview in Android App
Android Layout with Listview and Buttons
How to Serialize Object to JSON
Deserializing Generic Types with Gson
How to Parse This JSON in Android
Java Output Formatting for Strings
Jersey Rest Web Service with Activemq Middleware Integration
How to Change Webservice Url Endpoint
Getextractedtext on Inactive Inputconnection Warning on Android
How to Select and Crop an Image in Android
Which Android Data Storage Technique to Use
How to Avoid Garbage Collection Delays in Java Games? (Best Practices)