What does "+=" operator do? [duplicate] - java

This question already has answers here:
What does the "+=" operator do in Java?
(8 answers)
Closed 5 years ago.
I've seen code example that had
x += y
and I can't seem to find any explanation for this.
Please help. Thanks.

From:
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op1.html
You can also combine the arithmetic operators with the simple assignment operator to create compound assignments. For example, x+=1; and x=x+1; both increment the value of x by 1.

Related

What does this statement in Java mean? [duplicate]

This question already has answers here:
What is the Java ?: operator called and what does it do?
(17 answers)
Closed 4 years ago.
I am confused by this statement. Please help me explain this statement:
return third == Long.MIN_VALUE ? (int)first : (int)third;
Considering your top tags in SO is Python, let's explain this statement with Python:
return first if third == sys.maxint else third
Of course the Long.MIN_VALUE is not necessarily equal to sys.maxint in Python.

How to set sth. on infinity in Java? [duplicate]

This question already has answers here:
How to implement infinity in Java?
(11 answers)
Closed 5 years ago.
I want to implement the dijkstra algorithm and have to set each note at the beginning to infinity.
I would like to know, if there is any function in Java which makes it easy.
Double.POSITIVE_INFINITY if you are using Double to store your data.
Also note that this is not a number, which is nice depending on what you want to do. Double supports this concept.

Java Newbie - When to use it? Relating to Boxing/Unboxing I think [duplicate]

This question already has answers here:
Which is better: letting Java do autoboxing or using valueOf()
(3 answers)
Closed 6 years ago.
Integer x = 5;
Integer x = Integer.valueOf(5);
Is there any scenario where I would want to use the 2nd one specifically or is it redundant altogether and shouldn't not bother about it?
After Java 5 (because of autoboxing / unboxing) there is no difference except the first one is shorter.
Both statements are equivalent.
The statement Integer x = 5 would be compiled to
Integer x = Integer.valueOf(5);
The compiler will do that for you behind the scene, so the only difference is the number of character in source file.

Java Operator confusion [duplicate]

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

What is the difference between i+=d and i=i+d in Java? [duplicate]

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

Categories