for example,this program :
Code :public class test{ public static void main(String[] args) { byte i = (byte) 550; System.out.println(i); } }
outputs the number 38 instead of 550,can somebody explain that to me?
thanks in advance :)
Printable View
for example,this program :
Code :public class test{ public static void main(String[] args) { byte i = (byte) 550; System.out.println(i); } }
outputs the number 38 instead of 550,can somebody explain that to me?
thanks in advance :)
That is because a byte is 8 bits while a int is 32 bits. Also, Java primitives are signed, which means the largest number a byte can hold is 127. So, stuffing the number 550 into a byte overflows allotted space gives you a different number.
Read more here:
Primitive Data Types (The Java™ Tutorials > Learning the Java Language > Language Basics)
well,i know the size of each type,but can you explain to me why am i getting this number in particular,and not anything else?
Ok here we go :D
Lowest byte: -128
Highest byte: 127
The thing is that when you assign 128 to a byte it will in fact be -128 as the sign bit changes and 129 will effectively be -127, 130 will be -126 etc.
Since this is the case, you can calculate this fairly simple.
550 - 128 - 128 - 128 - 128 = 38
Its sort of hard to explain, you need to play around with it to understand it.
Everything above 127 will start over from -128 so to speak.
550 - 128 = 422
422 - 128 = 294
294 - 128 = 166
166 - 128 = 38 // Now this is the first value that fits in the byte so thats the value we get
I'm sure this makes little or no sense, but there you go, play around with it :)
// Json
You might see a question which tries to store 128 into a byte and you just need to recognise that it will be a compiler error because its a too large number for a byte.
// Json
Quote:
You might see a question which tries to store 128 into a byte and you just need to recognise that it will be a compiler error because its a too large number for a byte
what if 127? it will compile ryt?
Java will only implicitly cast upwards:
bytes -> shorts -> ints -> longs -> floats -> doubles -> String
(technically, char's can be cast to int's implicitly)
Anything else must be explicitly casted (Note: String's can't be explicitly casted down!)
Code :byte a = 128; // compiler error byte a = (byte) 128; // compiles fine
Casting is casting. Implicit casting just means you don't have to specify what you're casting to, so Java will "implicitly" cast it for you.
Code :int a = 5; double b = a;
Java will see this as:
Code :int a = (int) 5; double b = (double) a;
You asked when the compiler would throw an error, and I just told you.
Quote:
Code :byte a = 128; // compiler error byte a = (byte) 128; // compiles fine
how is it possible when this?Code :byte a = (byte) 128
what will be the logic behind this?..
i mean...how did it became fine? when the range of byte is only -128 to 127?
Java lets you cast down explicitly because it's useful sometimes. Remember, what we're really dealing with here are bits. So, in a 32 bit integer, 128 is 00000000000000000000000010000000. Casting down to a byte tells the computer to take the 8 furthest right bits (also known as the least significant bits, or LSB) because a byte contains 8 bits. So, a will contain 10000000. Because of the way Java handles signed values, this translates to -128.
The reason Java doesn't do this implicitly is because it's not what you'd expect to happen normally (-128 equals 128?). You can tell the compiler this is what you want by making the explicit cast.