Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Why can not I add two bytes and get an int and I can add two final bytes get a byte? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Why can not I add two bytes and get an int and I can add two final bytes get a byte?" Watch "Why can not I add two bytes and get an int and I can add two final bytes get a byte?" New topic
Author

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

Joined: Jun 23, 2011
Posts: 86



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
Marshal

Joined: Mar 22, 2005
Posts: 41149
    
  45
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.


Ping & DNS - my free Android networking tools app
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575



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

Joined: Jun 23, 2011
Posts: 86

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.



More information From the JLS 5.2 Assignment Conversion
Joey Sanchez
Ranch Hand

Joined: Jun 23, 2011
Posts: 86

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
Sheriff

Joined: Sep 28, 2004
Posts: 18545
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Mark Moge
Ranch Hand

Joined: Mar 14, 2009
Posts: 96

For the record: the code with the explicit casting compiles.


SCJP6 | http://programmerutilities.blogspot.com/
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18545
    
  40

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

Joined: Nov 01, 2012
Posts: 17

Seetharaman Venkatasamy wrote:[code=java]

does that make sense?


Of course it does
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why can not I add two bytes and get an int and I can add two final bytes get a byte?
 
Similar Threads
well a doubt !
under what conditions can an int be assigned to a byte?
casting error #2
implicit casting of final int to byte ???
Why is this thread closed?