Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# Why can not I add two bytes and get an int and I can add two final bytes get a byte?

Joey Sanchez
Ranch Hand
Posts: 88

If the result of an expression involving anything int-sized or smaller is always an int even if the sum of two bytes fit in a byte.

Why does it happen when we add two final bytes that fit in a byte? There is no compiler error.

Ulf Dittmer
Rancher
Posts: 42968
73
Because, being final, the compiler can tell that the sum of x and y will fit into a byte without loss of precision. That's not the case with the non-final a and b.

As an experiment, compile with "x=100" and "y=200" and see what happens.

Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
• 1

because x+y is a compile time constants, since x and y is a compile time constant;
so compiler make sure that x+y dont overflow

here a+b is runtime expression. compiler cant evaluate them at compile time. it may not be fit in to byte .
in java arithmetic operation performed on operands that are below int ranges are resulted as int, normally.

does that make sense?

Joey Sanchez
Ranch Hand
Posts: 88
Yes, It makes sense!

Here, since x and y are declared final so the value of expression on the RHS is known at compile time, which is fixed at (1 + 2 = 3) and cannot vary. So, you don't need to typecast it explicitly

Whereas, in this case, value of a and b are not declared final. So, the value of expression is not known at compile time, rather is evaluated at runtime. So, you need to do an explicit cast.

However, even in the 1st code, if the value of a + b comes out to be outside the range -128 to 127, it will fail to compile.

Joey Sanchez
Ranch Hand
Posts: 88
Ulf Dittmer wrote:Because, being final, the compiler can tell that the sum of x and y will fit into a byte without loss of precision. That's not the case with the non-final a and b.

As an experiment, compile with "x=100" and "y=200" and see what happens.

If it doesn't fit in a byte, it will produce Compilation fails!

Henry Wong
author
Marshal
Posts: 21754
85
• 1
Joey Sanchez wrote:Yes, It makes sense!

Here, since x and y are declared final so the value of expression on the RHS is known at compile time, which is fixed at (1 + 2 = 3) and cannot vary. So, you don't need to typecast it explicitly

Be careful with this generalization. Yes, compile time constant variables are always declared final, but ... No, a variable that is declare final is not necessarily a compile time constant variable.

Henry

Mark Moge
Ranch Hand
Posts: 100
For the record: the code with the explicit casting compiles.

Henry Wong
author
Marshal
Posts: 21754
85
Mark Moge wrote:For the record: the code with the explicit casting compiles.

Yeah, it is legal to explicitly cast from any primative type to any other primative type.

Henry

Buddhi Vikasitha
Greenhorn
Posts: 17
Seetharaman Venkatasamy wrote:[code=java]

does that make sense?

Of course it does