if byte + byte = int why int + int is not short/long? in C#
Just because it "can be justified" that way, doesn't mean that that is the reason.
Eric Lippert, who would know, was clear about this in a comment on the question you linked:
The various musings below are a reasonable approximation of the design considerations. More generally: I don't think of bytes as "numbers"; I think of them as patterns of bits that could be interpreted as numbers, or characters, or colors or whatever. If you're going to be doing math on them and treating them as numbers, then it makes sense to move the result into a data type that is more commonly interpreted as a number.
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:
Does casting a byte to an int or declaring an int carry more overhead in C#?
seems like you think its a good idea to save 3 bytes by making foo a byte. Its not, it wont make any speed or space difference and it will simply confuse readers of the code (including you), and add subtle bugs when you change the code later.
If you were storing lots of numbers and you know the were all 1 byte then yes this would save space an you should do it. But not in the trivial case you show
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.
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.
Why 'int' by default, but not 'byte'?
Because the Java Language Specification says so.
Section 3.10.1. Integer Literals says:
An integer literal is of type
long
if it is suffixed with an ASCII letterL
orl
(ell); otherwise it is of typeint
(§4.2.1).
So your numeric literal 10
is of type int
.
Related Topics
How to Deserialize Xml Document
Proper Use of the Idisposable Interface
Return Multiple Values to a Method Caller
When Do You Use the "This" Keyword
Is Using Random and Orderby a Good Shuffle Algorithm
Casting VS Using the 'As' Keyword in the Clr
What's Wrong With Using Thread.Abort()
How to Calculate Someone'S Age Based on a Datetime Type Birthday
What Is the Purpose of "Return Await" in C#
How to Create a Dropdownlist from an Enum in ASP.NET MVC
Pre & Post Increment Operator Behavior in C, C++, Java, & C#
Why Does C# Not Provide the C++ Style 'Friend' Keyword