Difference between Simple and Compound Assignment in Java Last Updated : 09 Nov, 2022 Comments Improve Suggest changes Like Article Like Report Many programmers believe that the statement "x += i" is simply a shorthand for "x = x + i". This isn’t quite true. Both of these statements are assignment expressions. The second statement uses the simple assignment operator (=), whereas the first uses a compound assignment operator. The compound assignment operators are +=, -=, *=, /=, %= etc. The Java language specification says that the compound assignment E1 op= E2 is equivalent to the simple assignment, E1 = (T) ((E1) op (E2)), where T is the type of E1. In other words, compound assignment expressions automatically cast the result of the computation they perform to the type of the variable on their left-hand side. If the type of the result is identical to the type of the variable, the cast has no effect. If, however, the type of the result is wider than that of the variable, the compound assignment operator performs a silent narrowing primitive conversion. Attempting to perform the equivalent simple assignment would generate a compilation error. Consider the following examples- Java // A Java program that uses compound assignment // for different types. class GFG{ public static void main(String s[]) { short x = 0; int i = 123456; x += i; // Casts result to short System.out.println(x); } } Output: -7616 You might expect the value of x to be 123456 after this statement executes, but it isn’t; it’s -7616. The int value 123456 is too big to fit in a short. The automatically generated cast. It silently removes the two high-order bytes of the int value. Java // A Java program that uses simple addition // for different types. class GFG{ public static void main(String s[]) { short x = 0; int i = 123456; x = x + i; // Causes compilation error System.out.println(x); } } Output: prog.java:5: error: incompatible types: possible lossy conversion from int to short x = x + i; ^ It is clear from above examples that compound assignment expressions can cause undesirable results and should be used carefully for types like byte, short, or char. If we use them, we must ensure that the type of expression on right is not of higher precision. Comment More infoAdvertise with us Next Article Difference between Simple and Compound Assignment in Java S Shubham Juneja Improve Article Tags : Misc Java Difference Between Java-Operators Practice Tags : JavaJava-OperatorsMisc Similar Reads Difference between Arrays and Collection in Java An array in Java is a group of like-typed variables referred to by a common name. Arrays in Java work differently than they do in C/C++. Following are some important points about Java arrays. On the other hand, any group of individual objects which are represented as a single unit is known as the co 3 min read Difference between Array and String in Java An array is a collection of similar type of elements that are stored in a contiguous memory location. Arrays can contain primitives(int, char, etc) as well as object(non-primitives) references of a class depending upon the definition of the array. In the case of primitive data type, the actual value 5 min read Difference between List, Set and Map in Java List interface in Java is a sub-interface of the Java collections interface. It contains the index-based methods to insert, update, delete, and search the elements. It can have duplicate elements also. We can also store the null elements in the list. List preserves the insertion order, it allows pos 4 min read Compound Assignment Operators in Java In Java, compound-assignment operators provide a shorter syntax for assigning the result of an arithmetic or bitwise operator. They perform the operation on two operands before assigning the result to the first operand.The following are all possible assignment operators in Java:1. += (compound addit 6 min read Difference Between == Operator and equals() Method in Java In Java, the equals() method and the == operator are used to compare objects. The main difference is that string equals() method compares the content equality of two strings while the == operator compares the reference or memory location of objects in a heap, whether they point to the same location 4 min read Like