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
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:
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.
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:
What is the difference between a += b and a =+ b , also a++ and ++a?
(9 answers)
Closed 7 years ago.
Is += the same as =+?
I can't find any reason for why the plus sign is reversible.
For what reasons would I need to use one of the other? Where can i find docs on this i tried searching but didnt see the use of both.
It's not the same.
x+=5 is equivalent to x=x+5.
x=+5 (or x=(+5)) is equivalent to x=5.
This question already has answers here:
Raising a number to a power in Java
(10 answers)
Closed 8 years ago.
I am trying to calculate the power as below but it is giving me 'bad operands type for binary operator '^'. I am guessing that it is a precedence issue but it still doesn't fix with inserting additional brackets
double pw = ((N - (df + 1))^2);
You should use java.lang.Math.pow(x,y)
Example: java.lang.Math.pow(2,3) returns 8
See this
http://www.tutorialspoint.com/java/lang/math_pow.htm
java.lang.Math.pow(double a, double b)
You can use static import for this.