Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

well a doubt !

 
vivek sivakumar
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)
 
Alfred Kemety
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
check the output

HTH
 
david eberhardt
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic