Java: double to int automatic conversion

int i = 1;
i = i + 1.2; 
would result in compile time error. I understand that i + 1.2 would result in a float which can't be assigned to integer.

This code is error free and the result is an integer.
int i = 1;
i += 1.2; 
This automatic narrowing is mind boggling.

Edit:
Had been searching for answer the whole day and someone finally answered it.

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.
from: #-Link-Snipped-#

Replies

  • PraveenKumar Purushothaman
    PraveenKumar Purushothaman
    It is possible with an increment operator but instead you can do one thing. Type casting it to UpCast or DownCast, makes the program 100% error free!
    Eg:
    int i = 1;
    i = (int)(i + 1.2);

You are reading an archived discussion.

Related Posts

Computers has become a part of our life.There is no world without computers.We depend on computers for everything.Computers are almost used in all the fields.We cannot ignore the importance of...
1.Why did you resign from your previous job?? 2.Why have you been out of work for so long?? 3.Why have you had so many jobs?? 4.Tell me about a situation...
1.Why do you want to work at our company?? 2.What is the difference between hard work and smart work? 3.How do you feel about working nights and week end? 4.can...
Second international conference on Advances in Computer science will be held during dec 20-21, 2011 in Noida.In this conference some of the theory and applications of computational engineering,computer and telecommunication...
Hello engineers I need some help and advice! (I do my internship and I got this task to do. I need it soon, but I got stucked…) I am creating...