This question already has answers here:
What does an assignment expression evaluate to in Java?
(5 answers)
Java calling method and using ternary operator and assign in the parameters?
(6 answers)
Closed 8 days ago.
This post was edited and submitted for review 8 days ago and failed to reopen the post:
Original close reason(s) were not resolved
I'm doing technical interview prep and came across this line in a CodeSignal solution.
This is in Java and a, b, and m are ints.
I'm sure this is a noob question, but I can't figure out how to google it!
Edit: Thanks for the answers. I'm realizing my question is actually more specific.
This is the full line:
b = Math.max(m + a, a = b);
It doesn't work the same as:
b = Math.max(m + a, b);
a = b;
Why?
Related
This question already has an answer here:
Using =+ won't work in for loop
(1 answer)
Closed 8 years ago.
What is the difference between
+=
and
=+
in Java? I tried searching but got no results.
How do those two work specifically? Any help will be appreciated!
It is quite easy:
a+=b is the same as a = a+(b)
=+ simply does not exist.
However, you might see a =+ b. You should read it as a = (+b). I.e., the parser will never parse =+ as a single token, it will parse it as = and +, so the following expression may start with a plus. The same goes for =-:
int a =-b; // a = -b
int a =+b; // a = +b
This question already has answers here:
Difference between a += 10 and a = a + 10 in java? [duplicate]
(5 answers)
Closed 9 years ago.
I know that both this statements evaluate to same answer but
is there any performance issue related to this statements?
The generated code will be the same for both. The only difference is to the readability of the code.
This question already has answers here:
compare and update a string
(11 answers)
Closed 9 years ago.
I already opened a similar question, unfortunetly I didnt explain my problem correctly, sorry about that.
This time I hope everyone understand what I mean.
My intention is to Update a String A with a String B.
For example:
String A:
A
B
C//Good morning, sir
D//A comment
E
String B:
A
B//Yes
C
D
DD
E
The result should be:
A
B//Yes
C//Good morning, sir
D//A comment
DD
E
I do not know where the differences are, I just want to keep everything in String A and just add the new things from String B (to the correct positions).
Has anyone an idea if java can do this?
Well, how do you know where to insert the parts of your string? You could use an array and update write all words of your strings on an index by using a simple loop.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Java += operator
We can add a value into any variable either b+=8 or b=b+8 both will return the value adding 8 into the variable b. I got the question in my interview, it was
byte b=7;
b=b+8; //compile error
What would be output, I ticked compile time error, since adding byte and int will be int (I believe) and since, we are trying to store int value into byte. But, when I tried below code myself
byte b=7;
b+=8; //OK
Then, the above code compiles and run perfectly without any error and return 15. Now, my question is why and how ? I mean, why it doesn't requires explicit casting ?
That's the only difference in b = b + 8 and b += 8
Compiler puts the cast automatically.
This question already has answers here:
Closed 13 years ago.
Duplicate:
java += question
Why aren’t op-assign operators type safe in java?
When i is an integer and d is a double, i+=d works but i= i+d does not.
Why is this?
i = i + d does not work because you would be assigning a double to an int and that is not allowed.
The += operator casts the double automatically to an int, so that is why it works.
This is the link to the information on the spec:
http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#5304