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.
Related
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?
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.
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 7 years ago.
String TypedMaxNumber = MaxValue.getText();
if(TypedMaxNumber == "100")
System.out.println(TypedMaxNumber+" = 100");
I think it is a silly problem, but when I am running to run this program and I type 100 in the text Field it is not going inside the loop. What could be the reason.
I am running to run this program and I type 100 in the text Field it
is not going inside the loop.
Its
if(TypedMaxNumber.equals("100"))
Since TypedMaxNumber is of type String. equals() check for value equality
This question already has answers here:
How does auto boxing/unboxing work in Java?
(4 answers)
Closed 7 years ago.
I know how boxing is done. For example, when you write Integer a = 4, Integer.valueOf(int) is being called. Likewise, can i see the implementation code for unboxing as well?
Update : It is answered in the suggested question.
In such cases,intValue() gets called to get the primitive value.
Integer a= new Integer(4);
int val = a.intValue();
Like the integer, all the wrapper classes have this method to get it's primitive value.
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