A literal integer (such as 8) is always implicitly an int in Java. We all are aware of the fact that an int in java is a 32 bit value. Now since by default the literal integer is int hence when we assign a literal integer to an int or a long variable, the java compiler does not complain.
What happens when we try to assign the same literal integer to a variable which is smaller than int (e.g- a byte, a char or a small variable)? The question is how will the compiler react?
Write any small java program and try out the following assignment:
What happens when we try to assign the same literal integer to a variable which is smaller than int (e.g- a byte, a char or a small variable)? The question is how will the compiler react?
Write any small java program and try out the following assignment:
byte b=12;
Did java compiler complain on the above assignment?
No, it did not. But is not this strange that here we tried to assign 12 which is a implicitly an int i.e a 32 bit value to a byte variable which is a 8 bit value holder?
Why did not we get a compiler error?
Is 12 not implicitly an int?
yes, it is an int.
But our assignment was legal because the compiler narrows the literal value to byte. In order to be more clearer the compiler puts a cast. The preceding code is identical to following:
No, it did not. But is not this strange that here we tried to assign 12 which is a implicitly an int i.e a 32 bit value to a byte variable which is a 8 bit value holder?
Why did not we get a compiler error?
Is 12 not implicitly an int?
yes, it is an int.
But our assignment was legal because the compiler narrows the literal value to byte. In order to be more clearer the compiler puts a cast. The preceding code is identical to following:
byte b=(byte)12;
Everything that I mentioned above applies equally to char and short.
Now again try out the following assignment in the same java program:
Now again try out the following assignment in the same java program:
byte b=128;
Did java compiler complain on the above assignment now?
The answer is yes it did.
The compiler error that comes now on this assignment is as follows:
The answer is yes it did.
The compiler error that comes now on this assignment is as follows:
TestBytes.java:5:possible loss of precision
found : int
required : byte
byte b=128;
found : int
required : byte
byte b=128;
Well why did the compilation fail this time?
Because java compiler is smart enough.
A byte can hold upto 127, because it is a 8 bit value holder. Here we were trying to assign a literal that is too large for the variable. Here the compiler knows that this value is too large to fit into the variable and hence it complains us.
However we can resolve this issue by using a cast like following:
Because java compiler is smart enough.
A byte can hold upto 127, because it is a 8 bit value holder. Here we were trying to assign a literal that is too large for the variable. Here the compiler knows that this value is too large to fit into the variable and hence it complains us.
However we can resolve this issue by using a cast like following:
byte b=(byte)128;
What value does actually get stored in byte variable?
The casts such as above are resolved in java by truncating the higher order bits that wont fit.
128 which is an int will take 32 bits and hence it is represented as follows:
The casts such as above are resolved in java by truncating the higher order bits that wont fit.
128 which is an int will take 32 bits and hence it is represented as follows:
00000000000000000000000010000000
Since byte is just 8 bits hence the leftmost 24 bits will be truncated leaving behind the rightmost 8 bits. Hence we are left with:
10000000
Now remember that byte is signed where the leftmost bit represents the sign. The value as 1 for the sign bit represents a negative number therefore here we have ended up with a negative number. In order to get the value of the negative number replace 0's with 1's and 1's with 0's and then add 1 to the outcome:
10000000
+1
----------
1000000
+1
----------
1000000
Which is equal to -128.
Try out one more assignment:
Try out one more assignment:
byte a=3;
byte b=4;
byte c=a+b;
byte b=4;
byte c=a+b;
What was the result?
Was the compilation successful?
No.
We may wonder that 3+4 i.e. a+b is equal to 7 which is smaller than 127, isn't it? Then why did the compiler complain us? It should not have complained because the value is in the range of bytes, but it did give us a compilation error.
Let us try to find out why is it so.
Now remember that in java involving an int size or smaller in always an int. Therefore when we added two bytes the result was an int and as a result the compiler gave us an error.
Again this issue can be resolved by applying a cast:
Was the compilation successful?
No.
We may wonder that 3+4 i.e. a+b is equal to 7 which is smaller than 127, isn't it? Then why did the compiler complain us? It should not have complained because the value is in the range of bytes, but it did give us a compilation error.
Let us try to find out why is it so.
Now remember that in java involving an int size or smaller in always an int. Therefore when we added two bytes the result was an int and as a result the compiler gave us an error.
Again this issue can be resolved by applying a cast:
byte a=3;
byte b=4;
byte c=(byte)(a+b);
byte b=4;
byte c=(byte)(a+b);
Similarly:
byte b=4;
b=b+7;
results in compilation failure. b=b+7;
However
In fact +=, -=,*=, /= will all put an implicit cast and hence will not result in any compilation error.
Hope you might have now an idea about where will the compiler put an implicit cast and where will it act smartly to give an error message.
byte b=4;
b+=7;
will compile without error.b+=7;
In fact +=, -=,*=, /= will all put an implicit cast and hence will not result in any compilation error.
Hope you might have now an idea about where will the compiler put an implicit cast and where will it act smartly to give an error message.