File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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
Eclipse IDE Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Eclipse IDE Java Windows XP
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Eclipse IDE Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 88
Eclipse IDE Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the record: the code with the explicit casting compiles.
 
Henry Wong
author
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Java Netbeans IDE Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seetharaman Venkatasamy wrote:[code=java]

does that make sense?


Of course it does
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic