Java Operator confusion [duplicate] - java

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

Related

Java how to see if String has an integer in it [duplicate]

This question already has answers here:
Java - char, int conversions
(4 answers)
Closed 3 years ago.
I'm trying to determine if my ticket number has a "9" then change the price to 5. This is the code I have. The code itself compiles, but doesn't work as intended. I imagine it's because I'm using a wrong operator. If anyone could let me know how I could change it for it to work that'd be greatly appreciated.
for(int x=0; x<Integer.toString(e1.getNumber()).length(); x++)
{
if(Integer.toString(e1.getNumber()).charAt(x) == 9)
{
System.out.println("The price is $5");
}
}
Thank you! I just had to change 9 to '9'! Its working now!!!
it depends on specific requirements. what if 9 is more then one times in string? what you do in such case? if you are sure that 9 appears once in your ticket price. just you next code.
if (yourString.contains("9")){
yourString = yourString.replace("9", "5");
}
Just had to put '9' so it looks for a char!

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

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.

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.

How does postfix operator works in this particular case (JAVA)? [duplicate]

This question already has answers here:
What is x after "x = x++"?
(18 answers)
Closed 8 years ago.
Could you help me a little bit? First of all, this is the code:
package helloworldapp;
public class HelloWorldApp
{
public static void main(String[] args)
{
int jaja = 1;
jaja = (jaja++)*2*2;
System.out.println(jaja);
}
}
I would like to understand this line:
jaja = (jaja++)*2*2;
As far as I know, postfix increment operator evaluates to the variable after the statement is done. Why does it give 4 as a result? Maybe I shouldn't use the same variable this way but I'm curious about that how it works. I thought that, firstly it multiply 'jaja' by 2, repeat it, the statement is over, and then add 1 to jaja. It would be 5 but I misunderstand something.
Um, it is my first comment here and also my English is really bad. Please forgive me for this :)
Yes, jaja++ will increment jaja to 2, but the result of that expression is still 1, and *2*2 will yield 4, which is assigned to jaja, overwriting the 2.

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