Int and Byte


#1

var x : Int = 5 val y : Int = 6 x = x + y

OK

var x : Byte = 5
val y : Byte = 6
x = x + y

KO - Type mismatch: inferred type is jet.Int but jet.Byte was expected

Why?


#2

You'll be surprized, perhaps:

``

// Java:
byte x = 1;
byte y = 2;
x = x + y; // type mismatch

It works this way because adding two bytes together does not always yield a result that fits into a byte. The same holds for other integer types, but we couldn’t make the hierarchy of integer types infinite, so it’s cut iff at int (and once more at long).
A more techincal reason is that JVM performs all the computations in ints, and requres explicit coersion to store a result in a byte (the main reason for this, I think, is to have fewer byte codes for arithmetic).


#3

Yes, looking at Java docs i found the answer (and .Net works this way as well). Thanks, i apologize for the silly question.