• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

well a doubt !  RSS feed

 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well a simple question .
byt b = 200; // this does not compile ofcourse the limit for byte is only till 127 ,
but this works int x = 1234567890; how does this work (except that they say the precision is lost)
 
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
check the output

HTH
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by vivek sivakumar:
well a simple question .
byt b = 200; // this does not compile ofcourse the limit for byte is only till 127 ,
but this works int x = 1234567890; how does this work (except that they say the precision is lost)


1) Alfred is correct.
2) no precision is lost
3) if you were to assign int x = 10234567890;
then compiler error will show up as:
C:\BIN>javac Test.java
Test.java:3: integer number too large: 10234567890
int x = 10234567890;
^
1 error

----------------------------------------------
if you try to assign to a byte b = 200; (which exceeds the max value of 127) you get an error worded slightly different:
C:\BIN>javac Test.java
Test.java:5: integer number too large: 10234567890
int x = 10234567890;
^
Test.java:4: possible loss of precision
found : int
required: byte
byte b = 200;
^
2 errors

both are compiler errors and you will not have made a working class file that is runnable.
In the case of the error of the byte assignment, you are trying to assign "200" which is an literal number which is an int by default!
The compiler is really telling you 2 things with the message "loss of precision" error - first, the number needs an explicit cast to a byte, and second - if you make the cast you will lose precision.
byte b = (byte)200;
System.out.println("byte b = " + b);
output is : byte b = -56
 
vivek sivakumar
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thankx alfred and david for clearing.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!