This question already has answers here:
Why don't Java's +=, -=, *=, /= compound assignment operators require casting?
(11 answers)
Closed 6 years ago.
I was solving a problem in leet code and noticed the following code was not allowed in Java,
char c = 's';
c = c^c;
While the following was
char c = 's';
c^=c;
Is there a particular reason? Thanks you.
This is also true for plus or minus. c^c is evaluated as an int so the right hand side is int and can not be assigned into a char.
In the ^= case, the right hand side is char, and can be applied onto a char.
This is not the most obvious behavior.
Related
This question already has answers here:
Why don't Java's +=, -=, *=, /= compound assignment operators require casting?
(11 answers)
Closed last year.
int s=0;
s+=Math.pow(2,3);
this line does not show possible lossy conversion whereas
int s=0
s=s+Math.pow(2,3)
the above line shows error.
Any reason for this?
The problem is due to the method signature for Math.pow;. It returns the result as a double. You then attempte to return the double as an int, and that is a lossy conversion.
This question already has an answer here:
Ternary operator in java vs c [duplicate]
(1 answer)
Closed 4 years ago.
Why is the following statement legal in C but not in Java?
int k = 1;
(10 < 20) ? k++ : k--;
This is because in C all expressions can be made into expression-statements by adding a semicolon ;.
In Java not all expressions can be made into expression statements. They must be assignment expressions, use postfix/prefix operators, be method invocations, or new expressions. See more here
Further, a ternary operator in Java requires that each operand be a non void expression and the value returned must be assigned.
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:
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 an answer here:
Using =+ won't work in for loop
(1 answer)
Closed 8 years ago.
I am wondering what is the difference between these two assignment operators. A simple and right to the point answer would be greatly appreciated.
Expression a =+ b is equal to a = +b, which is the same as a = b.
Expression a += b is equal to a = a + b