Important alert: (current site time 7/15/2013 2:34:13 PM EDT)
 

article

Diff. b/w x=x+a and x+=a

Email
Submitted on: 5/31/2001 3:36:08 AM
By: MLi  
Level: Intermediate
User Rating: By 1 Users
Compatibility: Java (JDK 1.1), Java (JDK 1.2)
Views: 10929
(About the author)
 
     Important Note : There is a difference between x+=a and x = x+a.

 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
  1. You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
				Consider the following code.
int x;
x = x+ 0.1;
// The above code generates an error that // explicit type conversion is required.
// Right, anybody can tell this.
// Now Consider this for a difference.
int x;
x+=0.1;
//It doesn't generate an error.
// Why ?


Other 2 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

6/5/2001 7:23:16 AMSudheer Dhurjati

The Answer to your question is in Java Language specification 15.26.2 under the topic "Compound Assignment Experssions".
   short x = 3;
   x += 4.6;

    //and results in x having the    value    7 because it is equivalent to:
short x1 = 3;
x1 = (short)(x1 + 4.6);


(If this comment was disrespectful, please report it.)

 
6/5/2001 7:24:20 AMSudheer Dhurjati

Good One...
(If this comment was disrespectful, please report it.)

 
4/12/2002 9:48:12 AMVictor G.

The variable is not been combined with another int type. Since the first is using both int and double types the system assumes you made the mistake, and that's why it returns the error:)
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.