• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 101
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 101
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 101
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
author
Posts: 23949
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 101
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For the record: the code with the explicit casting compiles.
 
Henry Wong
author
Posts: 23949
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 17
Netbeans IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Seetharaman Venkatasamy wrote:[code=java]

does that make sense?



Of course it does
 
and POOF! You're gone! But look, this tiny ad is still here:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic