This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes diff between += and =+ different output Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "diff between += and =+ different output " Watch "diff between += and =+ different output " New topic
Author

diff between += and =+ different output

marco marco
Greenhorn

Joined: Dec 11, 2004
Posts: 5
public class desk {
public static void main(String args[]) {
int b = 1;
int a =0;
for(int i=0; i< 10; i++){
a =+ b;
// a += b;
}
}
}

Running a =+ b; and then a += b; gives you two different answers.

I know that a =+ b Equivalent to a = a + b

what is a +=b equivalent to ?

Why is that ?
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
I believe that what's happening here is the following

a =+ b is not an increment operator.

I believe it is like saying a = (+b).

So we aren't incrementing a.
ak pillai
author
Ranch Hand

Joined: Feb 11, 2006
Posts: 288
It is a good practice to start your names with uppercase i.e. "Desk" not "desk".

a += b means a = a+b

but a =+ b means a = b;
and a =- b means a = -b;



java j2ee job interview questions with answers | Learn the core concepts and the key areas
marco marco
Greenhorn

Joined: Dec 11, 2004
Posts: 5
Great. Thank you for your heplp
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11404
    
  81

Bit late, but moving this to JiG (Beginners).


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: diff between += and =+ different output
 
Similar Threads
method overloading
need help
array declaration
Infinite for loop....???
implementing multiple interfaces