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.
Reason is simple, when you prepare for OCPJP you are exposed to this kind of subtle details, and that's just one more reason to do Java Certification. Now let's see some more example of compound assignment operator and implicit casting in Java.
short s = 2;
int i = 20;
s += i; // Compiles fine - Implicit Casting of Compound assignment operator
s = s + i // Compilation error - Type mismatch: cannot convert from int to short
Second statement didn't compile because result of adding a short and int is of type int, which cannot be stored in a short variable without casting. So you need to cast result back too short to compile it, i.e.
s = (short) (s + i);
In first case, you don't face this issue because of implicit casting introduced by compound statement. So this is the case where compound assignment is different than regular assignment operator in Java.
Compound Assignment Operator Example in Java
As I said, compound assignment operator becomes tricky when you try to add two different but compatible types e.g. numeric types like int, long, short etc. Let's see what happens when you add a short and int variable and stores result back to short variable in Java.short s = 2;
int i = 20;
s += i; // Compiles fine - Implicit Casting of Compound assignment operator
s = s + i // Compilation error - Type mismatch: cannot convert from int to short
Second statement didn't compile because result of adding a short and int is of type int, which cannot be stored in a short variable without casting. So you need to cast result back too short to compile it, i.e.
s = (short) (s + i);
In first case, you don't face this issue because of implicit casting introduced by compound statement. So this is the case where compound assignment is different than regular assignment operator in Java.
Similarly you will face same issue, while adding an int and long variable and storing result back to an int. here is an example of that
long _long = 2;
int _int = 20;
_int += _long; // Compiles Ok
_int = _int + _long; // Compilation Error - Type mismatch: cannot convert from long to int
Once again you need to explicitly cast result into int to make second statement compile in Java, i.e. _int = (int) (_int + _long);
If you pay attention, you will notice that compound statement operator does look similar to regular arithmetic assignment and only differs, when you try to add two compatible types and store result in smaller types e.g. storing result in short variable in first example and storing result in int variable in second example.
That's exactly because these are edge cases where casting comes into picture.
Now, why should you learn this, you can effectively write Java programs without ever using compound assignment operator. Well, yes but I have found those cool guys, who are fond of writing short and crisp code, uses Java feature like this and if you happen to work along with them or maintain their code, then your misconception may cause subtle issues on your application.
Now, why should you learn this, you can effectively write Java programs without ever using compound assignment operator. Well, yes but I have found those cool guys, who are fond of writing short and crisp code, uses Java feature like this and if you happen to work along with them or maintain their code, then your misconception may cause subtle issues on your application.
To be frank, I even didn't know about this feature during my first year of programming and I learn them, while preparing for OCPJP and doing mock exams. It not only gives you pleasure of learning details of Java programming but also builds your confidence in coding and reading code. It also helps during Job interviews.
That's all on this compound assignment operator example in Java guys, let me know if you have any questions. This is one of the important concept to know for Java programmers and it not just help in interview and Java certification but also on reading code as part of your day to day job. If a developer use this compound statement and you don't know how it work then you cannot modify that code property and neither can you understand, hence you should pay attention to learn such core Java concepts.
That's all on this compound assignment operator example in Java guys, let me know if you have any questions. This is one of the important concept to know for Java programmers and it not just help in interview and Java certification but also on reading code as part of your day to day job. If a developer use this compound statement and you don't know how it work then you cannot modify that code property and neither can you understand, hence you should pay attention to learn such core Java concepts.
And lastly one question for you, if you add a long and int variable what would be the type of result?
No comments:
Post a Comment