Java equals plus operator [duplicate] - java

This question already has answers here:
What is the difference between a += b and a =+ b , also a++ and ++a?
(9 answers)
Closed 8 years ago.
I need to explain this strange operator =+ (equal plus)
Example #1:
Double a = new Double(5);
Double b = new Double(10);
a += b
result:
a=15.0
b=10.0
Example #2:
Double a = new Double(5);
Double b = new Double(10);
a =+ b
result:
a=10.0
b=10.0
I understand the first example, but please explain me what this =+ operator did in example no.2.
And another interesting fact is, that these operators are valid and compilable:
+=, -=, *=, /=
but any of these two won't compile:
=*, =/

=+ is the assignment operation and the unary + afterwards. It's perfectly valid and what happens is:
a = (+b);
It's pretty much the same when you want to assign the negative value of a variable to another variable:
a = (-b); //a will be assigned with -10
Also, =* doesn't compile, because there's no * unary operator.

Related

Why i=i+j is not identical to i+=j [duplicate]

This question already has answers here:
Why don't Java's +=, -=, *=, /= compound assignment operators require casting?
(11 answers)
Closed 6 years ago.
I have below code
int i = 5;
long j = 5;
1. i = i + j; // Throwing an exception "Type mismatch: cannot convert from long to int"
2. i += j; // This working fine
As you can see 1st case throwing an exception but 2nd case working fine.
Why 2nd case working fine without throwing an any exception?
+= is a compound statement and Compiler internally casts it. Where as in first case direct statement and compiler cries.

Cannot convert from long to int error only occurs when using simple assignment [duplicate]

This question already has answers here:
Type mismatch: cannot convert from long to int
(3 answers)
Closed 7 years ago.
Java is doing something here that I don't understand. If I have
int sum = 0;
long num = 1234;
Using the assignment operator += will work fine
sum += num % 10;
However, using simple assignment will cause a "Cannot convert long to int" error
sum = sum + num % 10;
Why is this ??
When you do += that's a compound assignment and compiler internally have some mechanism to evaluate it. Where as in first case the compiler given an error since it normal statement.
JSL on how it treats compound assignment.
http://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.26.2
In your first case, compiler translate your code to
sum += (int) (num % 10);
So there is a cast internally by compiler. Hence you are not prompted to an error. Compiler doing your job there :)

how to eliminate any number after the floating point? [duplicate]

This question already has answers here:
How to cast a double to an int in Java by rounding it down?
(9 answers)
Closed 8 years ago.
Is there any function returns only the real number without the floating point? For an example,
func(1.xxx)->1
func(52.xx)->52
func(0.xx)->0
Is there any function does so?
Simply casting to int would truncate everything past the decimal point.
float f1 = 10.345;
int i1 = (int) f1; // Gives 10
float f2 = 10.897;
int i2 = (int) f2; // Also gives 10
You can do :
double d = 100.675;
System.out.println((int) d);
this gives you 100.
System.out.println(Math.round(d));
gives you 101.
You can also use :
new java.text.DecimalFormat("#").format(10.0); // => "10"
now the choice is yours that how you want to do and main thing depend on that what is your expected output is.

Java typecasting confusion [duplicate]

This question already has answers here:
Why don't Java's +=, -=, *=, /= compound assignment operators require casting?
(11 answers)
Closed 8 years ago.
Take these two snippet for example:
Case 1:
Scanner s = new Scanner(System.in);
int n = s.nextInt(); /** take user input **/
n *= Double.NEGATIVE_INFINITY;
and
Case 2:
int k=10;
double kk = 10.10;
int rst = k*kk;
In both the cases, I am not doing any typecasting from my side, but Case 1 executes and prints value of n correctly but Case 2 throws an error, can not convert from double to int. Why this difference?
The first works and the second doesn't because the *= += -= etc add an automatic cast.
If, for example, you were to change your second example to;
int k=10;
double kk = 10.10;
k*= kk;
It should work. Alternatively you could just add an explicit cast like so rst = (int)(k*kk);
The arithmetic promotion in Java happens when you apply an arithmetic
operation on two variables with different data-types. In this case the
compiler will convert the data type of one operand in the binary
arithmetic operation to the type of the other operand.
In your case, for multiplying an int and a double, the int is promoted to double, and the result is double. So, it can't be stored into an int.
See more at: http://www.codemiles.com/java/arithmetic-promotion-t3487.html#sthash.sdHtt7pG.dpuf
The result of the multiplication of an integer (int) and a floating point in java will always result in a floating point (double). You are assigning this result to the integer rst which requires casting.

Byte incrementing operation in Java [duplicate]

This question already has answers here:
why byte += 1 compile but byte = byte + 1 not?
(8 answers)
Closed 6 years ago.
What happens when we try to increment a byte variable using the increment operator and also by the addition operator.
public class A {
public static void main(final String args[]) {
byte b = 1;
b++;
b = b + 1;
}
}
Please give me the source where we can find such small things unleashed? Please help me out.
The difference is that there is an implicit casting in the ++ operator from int to byte, whereas, you would have to do that explicitly in case if you use b = b + 1
b = b + 1; // Will not compile. Cannot cast from int to byte
You need an explicit cast:
b = (byte) (b + 1);
Whereas b++ will work fine. The ++ operator automatically casts the value b + 1, which is an int to a byte.
This is clearly listed in JLS - ยง15.26.2 Compound Assignment Operators : -
A compound assignment expression of the form E1 op= E2 is equivalent
to E1 = (T) ((E1) op (E2)), where T is the type of E1, except that E1
is evaluated only once
Please note that operation b + 1 will give you a result of type int. So, that's why you need an explicit cast in your second assignment.
What happens? Actually b = b + 1 won't compile.
You must explicitly convert it to byte, because b + 1 evaluates to int. And it is not guaranteed that an int can fit into a byte.
b = (byte)(b + 1);
The variable will be incremented twice
b++; and b=b+1; are equivalent and will result in the same bytecode.
b will be equal to 3 before the main method is finished.
Edit: actually they are not equivalent: b=b+1; is wrong and should be b=(byte) (b+1); [cast to a byte, otherwise it is an int]

Categories

Resources