The question is:
Why in this case i get compilation error in Java?
byte x = 0;
x = 128;
But this is legal:
x+= 999l;
I use eclipse, jdk 7.
Thank You
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In your first one:
A byte is a signed integral type, 8-bits wide, and can express the range of
-128to+127.x = 128means “assignxto128“, and by default,128is of typeint, so you’re trying to assign aninttobytewhich would causePossible loss of precisionerrors, becauseintis wider thanbyte. To get this to work, you would have to explicitly cast the value of128.For your second example, adding values to
xis fine, but you just overflow the values.