This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Why int i = 2147483647 + 1; is ok, but byte b = 127 + 1; is not compilable?
Im a beginner with java so please appreciate this beginner question. But why is my compiler not fine with byte b = 127 + 1; but compiles fine with int i = 2147483647 + 1;
Your compiler complains because it sees two ints being added together (int 127) and (int 1) and then it worries that some precision will be lost as it attempts to store the result (int 128) into a byte.
The numbers you selected tend to hint that you think it is related to overflow. It is not, as even if it is important to keep overflow in mind when programming, the compiler never complains about overflow issues.
because when the compiler sees 127 it treats it as an int, not a byte. You need a cast to fit the result back into a byte.
Related
This question already has answers here:
Java: What does ~ mean
(5 answers)
Closed 5 years ago.
int x=10;
System.out.println(~x);
//this will print -11
//how to do the calculation manually using complement arithmetic
This is a Negation operator it will consider ~x= -(10+1), so you will get -11 as the output. Refer some C books you can get more explanation on this
Perhaps this helps you: You can print out the bits of the integer as following. There you can see that a int is represented as a 32-bit value. Explanations about bitwise not operator can be found on the web i guess;
int x = 10;
System.out.println(Integer.toBinaryString(x)); //00000000000000000000000000001010
System.out.println(Integer.toBinaryString(~x)); //11111111111111111111111111110101
System.out.println(~x); //-11
This question already has answers here:
How does bitshifting work in Java?
(10 answers)
Closed 7 years ago.
What are these double lesser than symbols. I've had to use them but I have no idea what they are. It seems to be comparing just like the == but I've never heard of it before. Thanks for any help.
<< (left shift)
Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand.
Example: Lets say A = 60. So,
A = ...000111100
A << 2 will give
A = ...011110000
which in decimal is 240.
Look at this example to learn more.
This question already has answers here:
final characters in Java [duplicate]
(2 answers)
Closed 7 years ago.
I'm just learning that you can add an int to a char. I've tried out the following expecting it to compile, but I'm getting an incompatible types error:
char a = 'e' + (number / 10)
I can't figure out why, if
char c = '1'
I've seen that similar questions suggest the use of 'final' to resolve, but I've modified it to apply that and it still gets the same error.....
works then the above doesn't....
Any ideas?
This occurs because you need to cast the chars into ints before you can actually perform calculations with them: int answer = (int) 'e' + (number / 10)
And then cast the answer back to a char:
answer = (char) answer
This question already has answers here:
What is the Java ?: operator called and what does it do?
(17 answers)
Closed 8 years ago.
I came across a code snippet online that used a notation that from what I gather seems to do a comparison and then returns back possible multiple outputs. I am still confused about it, even after research. Can someone re-write the code snippet to an equivalent, more basic version so that I can make sure I am understanding what I am seeing?
int mPart = i < mParts.length ? Integer.parseInt(mParts[i]) : 0;
Thanks in advance!
This is ternary IF operator. This line is equal to
int mPart;
if(i < mParts.length) {
mPart = Integer.parseInt(mParts[i]);
} else {
mPart = 0;
}
This question already has answers here:
why Integer.MAX_VALUE + 1 == Integer.MIN_VALUE?
(9 answers)
Closed 8 years ago.
After going through this question, i had one another in my mind.
Question: Why an integer variable value is set to Integer.MAX_VALUE.
eg.
int x = Integer.MIN_VALUE;
x--;
if (x == Integer.MAX_VALUE) {
System.out.println("Why....");
}
There must be some reason that is why this behavior implemented explicitly, otherwise throwing an Exception would be a better idea. I could not find/locate this behavior in JLS.
Because of underflow. Computers have worked like this for years, throwing an Exception would be a horrible idea here.
http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.2
"The integer operators do not indicate overflow or underflow in any way."