• 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
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Conversion

 
Ranch Hand
Posts: 167
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Why does not this code give a compile error?(because of final?)
Thanks
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Areca!

It's normal not to give a compile error because 100 can be converted to a byte.
Bytes numbers are represented on 8 bits=> means a byte number is between -2^7..2^7 - 1 (x^y means x multiplied with x for y times) = -128..127.
Because 100 can be represented on a byte (-128<100<127) the compiler gives no error and converts the int number 100 to the byte number 100.
If you tried to convert an int number greater than 127 or less than -128 (let's say 144) to a byte number, the compiler will give you an error.


The code example above gives you an error compile.
So, the final modifier has nothing to do with the conversion.

I hope this answer lets you.
Do let me know if you have troubles understanding it.
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The final modifier certainly has to do something with conversion Emilia. In the first example provided the value is within the range of byte values (-128 < 100 < 127) whereas in the second example the value is outside of the range of byte values (144 > 127). Since the variable is final the runtime value is known at compile time and can thus be verified to be in the range of byte. Hence a cast is not required in the first example but is required in the second.
 
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Iakhnin u r correct ....

if u try the same code not using final then it gives an error of loss of precision inspite of 100 being fit into the byte ...what Iakhnin said i suppose if u declare the variable as final then its value is computed at compile time ...

class MyClass{
public static void main(String []args) {
int i = 100;
byte b = i;
System.out.println(b);
}}


thanks & regards

srikanth
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic