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

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.

Related

Any difference between `var x` and `int x`? [duplicate]

This question already has answers here:
What is 'var' in Java 10, and is it comparable to JavaScript?
(3 answers)
Closed 3 years ago.
Is there a difference between declaring var x and int x?
//Java 8
int x = 10;
//Java 10
var x = 10;
There is no run-time difference. var is compile-time syntactical sugar. If you replace var with the inferred type (int) you get identical results.

Why doesn't java store large value in long datatype directly but allows to store it indirectly (without using literals)? [duplicate]

This question already has answers here:
Initialize a long in Java
(4 answers)
The literal xyz of type int is out of range
(5 answers)
Why, In Java arithmetic, overflow or underflow will never throw an Exception?
(4 answers)
Closed 4 years ago.
Here are my 2 examples:
1. Directly assigned value to long data type:
long a = 12243221112432;
I get an error:
integer number too large
But when assisgned like this:
ArrayList<Integer> al = new ArrayList<Integer>();
al.add(12);
al.add(24);
al.add(32);
al.add(21);
al.add(11);
al.add(24);
al.add(32);
long a=0;
for(int i=0; i<al.size(); i++){
a = a*100 + al.get(i);
}
System.out.println(a);
I get this output:
12243221112432
Why doesn't java throw an error in second example?
It isn't allowing to assign large value directly(example 1) but indirectly(example 2) it stores it and also allows me to use it too!
What is the reason for this to occur?
Is it because i am using integer in arraylist or something else?
UPDATE
Why is the large value stored in 'long a' in second example without using literal L?
It should have given me an error during 5th or 6th iteration of for loop...
Note
My question is not regarding the first example... I am asking why it worked for the second example...
Dont mark the question duplicate, since the other questions do not have my answer..stated above
For your assignment to work, you need to write it that way
long a = 12243221112432L;
This will indicate the compiler that your input is a long, not an int
integer number too large
as the error said, the number 12243221112432 is too large for an integer, it does not says that this number cannot fit into a long
Te be able to have this number, you have to make it a long by using the l or L indice : 12243221112432L
long : l L : 1716L
float : f F : 3.69F
double : d D : 6936D

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 :)

Java equals plus operator [duplicate]

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.

i++ still working for immutable Integer in Java? [duplicate]

This question already has answers here:
Is Integer Immutable
(12 answers)
Closed 9 years ago.
I know Integer is immutable in Java. But I tried this:
Integer i = 4;
i++;
System.out.println(i); // output is 5
Why the self-increment is still working? Did Java create a new Integer object?
i++;
is equivalent to:
i = i + 1;
which i now refers to a different Integer object (5).

Categories

Resources