Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

What is compound assignment operator +=, -=, *= and /= in Java? Example Tutorial

What Is Compound Assignment Operator +=, -=, *= And /= In Java? Example Tutorial
Many Java programmers think that a += b is just a shortcut of a = a + b, which is only half correct. Though it expands exactly like that and in many cases it produce same result, especially when type of both variable is same e.g. both being int or long, but you will see real difference when you try to add to integer types which are different e.g. long and int, short and int, short and long etc. What many programmer miss there is type casting. Compound statement provides you implicit cast, which often goes unnoticed. Which means a += b actually translates into a = (type of a) (a + b). I have asked this question a couple of times while interviewing Java developers, and most of the time, developer who are Oracle Java Certified got it right than others. 


This post first appeared on Javarevisited: Blog About Java Programmin, please read the originial post: here

Share the post

What is compound assignment operator +=, -=, *= and /= in Java? Example Tutorial

×

Subscribe to Javarevisited: Blog About Java Programmin

Get updates delivered right to your inbox!

Thank you for your subscription

×