{"id":1512,"date":"2017-03-21T18:09:44","date_gmt":"2017-03-21T12:39:44","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=1512"},"modified":"2017-03-29T10:35:09","modified_gmt":"2017-03-29T05:05:09","slug":"compound-assignment-operators-java","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/compound-assignment-operators-java\/","title":{"rendered":"JAVA &#8211; compound assignment operators(+=, -=, *=, \/=) in java"},"content":{"rendered":"<p>Java provides a set of operators to manipulate variables.<\/p>\n<h4 id=\"we-can-divide-all-the-java-operators-into-the-following-groups\"><span style=\"color: #800000;\"><strong>We can divide all the Java operators into the following groups:<\/strong><\/span><\/h4>\n<ul>\n<li>Arithmetic Operators<\/li>\n<li>Relational Operators<\/li>\n<li>Bitwise Operators<\/li>\n<li>Logical Operators<\/li>\n<li>Assignment Operators<\/li>\n<li>Miscellaneous Operators<\/li>\n<\/ul>\n<h4 id=\"arithmetic-operators\"><span style=\"color: #000000;\"><strong>Arithmetic Operators:<\/strong><\/span><\/h4>\n<p>Arithmetic operators are used in mathematical expressions in the same way that they are used in algebra.<\/p>\n<table width=\"1068\">\n<tbody>\n<tr>\n<td width=\"356\"><b>Operator<\/b><\/td>\n<td width=\"356\"><b>Description<\/b><\/td>\n<td width=\"356\"><b>Example<\/b><\/td>\n<\/tr>\n<tr>\n<td width=\"356\">+ (Addition)<\/td>\n<td width=\"356\">Adds values on either side of the operator.<\/td>\n<td width=\"356\">A + B will give 30<\/td>\n<\/tr>\n<tr>\n<td width=\"356\">\u2013 (Subtraction)<\/td>\n<td width=\"356\">Subtracts right-hand operand from left-hand operand.<\/td>\n<td width=\"356\">A \u2013 B will give -10<\/td>\n<\/tr>\n<tr>\n<td width=\"356\">* (Multiplication)<\/td>\n<td width=\"356\">Multiplies values on either side of the operator.<\/td>\n<td width=\"356\">A * B will give 200<\/td>\n<\/tr>\n<tr>\n<td width=\"356\">\/ (Division)<\/td>\n<td width=\"356\">Divides left-hand operand by right-hand operand.<\/td>\n<td width=\"356\">B \/ A will give 2<\/td>\n<\/tr>\n<tr>\n<td width=\"356\">% (Modulus)<\/td>\n<td width=\"356\">Divides left-hand operand by right-hand operand and returns remainder<\/td>\n<td width=\"356\">B % A will give 0<\/td>\n<\/tr>\n<tr>\n<td width=\"356\">++ (Increment)<\/td>\n<td width=\"356\">Increases the value of operand by 1.<\/td>\n<td width=\"356\">B++ gives 21<\/td>\n<\/tr>\n<tr>\n<td width=\"356\">\u2014 (Decrement)<\/td>\n<td width=\"356\">Decreases the value of operand by 1.<\/td>\n<td width=\"356\">B\u2013 gives 19<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n[ad type=\u201dbanner\u201d]\n<h4 id=\"relational-operators\"><span style=\"color: #993300;\"><strong>Relational Operators<\/strong><\/span><\/h4>\n<p>There are following relational operators supported by Java language.<br \/>\nAssume variable A and B. A value is 20 and B value is 30<\/p>\n<table width=\"1117\">\n<tbody>\n<tr>\n<td width=\"372\"><b>Operator<\/b><\/td>\n<td width=\"372\"><b>Description<\/b><\/td>\n<td width=\"372\"><b>Example<\/b><\/td>\n<\/tr>\n<tr>\n<td width=\"372\"><b>== (equal to)<\/b><\/td>\n<td width=\"372\">Checks if the values of two operands are equal or not, if yes then condition becomes true.<\/td>\n<td width=\"372\">(A == B) is not true<\/td>\n<\/tr>\n<tr>\n<td width=\"372\"><b>!= (not equal to)<\/b><\/td>\n<td width=\"372\">Checks if the values of two operands are equal or not, if values are not equal then condition becomes true.<\/td>\n<td width=\"372\">(A != B) is true.<\/td>\n<\/tr>\n<tr>\n<td width=\"372\"><b>>(greater than)<\/b><\/td>\n<td width=\"372\">Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true.<\/p>\n<p>\u00a0<\/td>\n<td width=\"372\">(A > B) is not true.<\/td>\n<\/tr>\n<tr>\n<td width=\"372\"><b>< (less than)<\/b><\/td>\n<td width=\"372\">Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true.<\/td>\n<td width=\"372\">(A < B) is true.<\/td>\n<\/tr>\n<tr>\n<td width=\"372\"><b>>= (greater than or equal to) <\/b><\/td>\n<td width=\"372\">Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true.<\/td>\n<td width=\"372\">(A >= B) is not true.<\/td>\n<\/tr>\n<tr>\n<td width=\"372\"><b><= (less than or equal to)<\/b><\/td>\n<td width=\"372\">Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true.<\/td>\n<td width=\"372\">(A <= B) is true.<\/td>\n<\/tr>\n<tr>\n<td width=\"372\"><\/td>\n<td width=\"372\"><\/td>\n<td width=\"372\"><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4 id=\"bitwise-operators\"><span style=\"color: #0000ff;\"><strong>Bitwise Operators<\/strong><\/span><\/h4>\n<ul>\n<li>Java defines several bitwise operators, which can be applied to the integer types, long, int, short, char, and byte.<\/li>\n<li>Bitwise operator works on bits and performs bit-by-bit operation.<\/li>\n<\/ul>\n<p>Assume if a = 60 and b = 13;<br \/>\n<strong>In binary format :<\/strong><br \/>\na = 0011 1100 and b = 0000 1101<\/p>\n<p>a&b = 0000 1100<\/p>\n<p>a|b = 0011 1101<\/p>\n<p>a^b = 0011 0001<\/p>\n<p>~a = 1100 0011<\/p>\n<table width=\"1133\">\n<tbody>\n<tr>\n<td width=\"378\"><b>Operator<\/b><\/td>\n<td width=\"378\"><b>Description<\/b><\/td>\n<td width=\"378\"><b>Example<\/b><\/td>\n<\/tr>\n<tr>\n<td width=\"378\"><b>& (bitwise and)<\/b><\/td>\n<td width=\"378\">Binary AND Operator copies a bit to the result if it exists in both operands<\/td>\n<td width=\"378\">(A & B) will give 12 which is 0000 1100<\/td>\n<\/tr>\n<tr>\n<td width=\"378\"><b>| (bitwise or)<\/b><\/td>\n<td width=\"378\">Binary OR Operator copies a bit if it exists in either operand.<\/td>\n<td width=\"378\">(A | B) will give 61 which is 0011 1101<\/td>\n<\/tr>\n<tr>\n<td width=\"378\"><b>^ (bitwise XOR)<\/b><\/td>\n<td width=\"378\">Binary XOR Operator copies the bit if it is set in one operand but not both.<\/td>\n<td width=\"378\">(A ^ B) will give 49 which is 0011 0001<\/td>\n<\/tr>\n<tr>\n<td width=\"378\"><b>~ (bitwise compliment)<\/b><\/td>\n<td width=\"378\">Binary Ones Complement Operator is unary and has the effect of \u2018flipping\u2019 bits.<\/td>\n<td width=\"378\">(~A ) will give -61 which is 1100 0011 in 2\u2019s complement form due to a signed binary number.<\/td>\n<\/tr>\n<tr>\n<td width=\"378\"><b><< (left shift)<\/b><\/td>\n<td width=\"378\">Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand.<\/td>\n<td width=\"378\">A << 2 will give 240 which is 1111 0000<\/td>\n<\/tr>\n<tr>\n<td width=\"378\"><b>>> (right shift)<\/b><\/td>\n<td width=\"378\">Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand.<\/td>\n<td width=\"378\">A >> 2 will give 15 which is 1111<\/td>\n<\/tr>\n<tr>\n<td width=\"378\"><b>>>> (zero fill right shift)<\/b><\/td>\n<td width=\"378\">Shift right zero fill operator. The left operands value is moved right by the number of bits specified by the right operand and shifted values are filled up with zeros.<\/td>\n<td width=\"378\">A >>>2 will give 15 which is 0000 1111<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4 id=\"logical-operators\"><span style=\"color: #800080;\"><strong>Logical Operators<\/strong><\/span><\/h4>\n<p>Assume Boolean variables A holds true and variable B holds false, then<\/p>\n<table width=\"1108\">\n<tbody>\n<tr>\n<td width=\"253\">Operator<\/td>\n<td width=\"591\">Description<\/td>\n<td width=\"264\">Example<\/td>\n<\/tr>\n<tr>\n<td width=\"253\">&& (Logical AND operator)<\/td>\n<td width=\"591\">If both the operands are non-zero, then the condition becomes true.<\/td>\n<td width=\"264\">(A && B) is false<\/td>\n<\/tr>\n<tr>\n<td width=\"253\">|| (Logical OR Operator)<\/td>\n<td width=\"591\">If any of the two operands are non-zero, then the condition becomes true.<\/td>\n<td width=\"264\">(A || B) is true<\/td>\n<\/tr>\n<tr>\n<td width=\"253\">! (Logical NOT Operator)<\/td>\n<td width=\"591\">Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false.<\/td>\n<td width=\"264\">!(A && B) is true<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4 id=\"assignment-operators\"><span style=\"color: #ff6600;\"><strong>Assignment Operators:<\/strong><\/span><\/h4>\n<p>The assignment operators are supported by Java language.<\/p>\n<table width=\"1186\">\n<tbody>\n<tr>\n<td width=\"298\">Operator<\/td>\n<td width=\"591\">Description<\/td>\n<td width=\"298\">Example<\/td>\n<\/tr>\n<tr>\n<td width=\"298\">=(Simple assignment operator)<\/td>\n<td width=\"591\">Assigns values from right side operands to left side operand.<\/td>\n<td width=\"298\">C = A + B will assign value of A + B into C<\/td>\n<\/tr>\n<tr>\n<td width=\"298\">+=(Add AND assignment operator)<\/td>\n<td width=\"591\">It adds right operand to the left operand and assign the result to left operand.<\/td>\n<td width=\"298\">C += A is equivalent to C = C + A<\/td>\n<\/tr>\n<tr>\n<td width=\"298\">-=(Subtract AND assignment operator)<\/td>\n<td width=\"591\">It subtracts right operand from the left operand and assign the result to left operand.<\/td>\n<td width=\"298\">C -= A is equivalent to C = C \u2013 A<\/td>\n<\/tr>\n<tr>\n<td width=\"298\">*=(Multiply AND assignment operator)<\/td>\n<td width=\"591\">It multiplies right operand with the left operand and assign the result to left operand.<\/td>\n<td width=\"298\">C *= A is equivalent to C = C * A<\/td>\n<\/tr>\n<tr>\n<td width=\"298\">\/=(Divide AND assignment operator)<\/td>\n<td width=\"591\">It divides left operand with the right operand and assign the result to left operand.<\/td>\n<td width=\"298\">C \/= A is equivalent to C = C \/ A<\/td>\n<\/tr>\n<tr>\n<td width=\"298\">%=(Modulus AND assignment operator)<\/td>\n<td width=\"591\">It takes modulus using two operands and assign the result to left operand.<\/td>\n<td width=\"298\">C %= A is equivalent to C = C % A<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0<\/p>\n<table width=\"1199\">\n<tbody>\n<tr>\n<td width=\"400\"><<=<\/td>\n<td width=\"400\">Left shift AND assignment operator.<\/td>\n<td width=\"400\">C <<= 2 is same as C = C << 2<\/td>\n<\/tr>\n<tr>\n<td width=\"400\">>>=<\/td>\n<td width=\"400\">Right shift AND assignment operator.<\/td>\n<td width=\"400\">C >>= 2 is same as C = C >> 2<\/td>\n<\/tr>\n<tr>\n<td width=\"400\">&=<\/td>\n<td width=\"400\">Bitwise AND assignment operator.<\/td>\n<td width=\"400\">C &= 2 is same as C = C & 2<\/td>\n<\/tr>\n<tr>\n<td width=\"400\">^=<\/td>\n<td width=\"400\">bitwise exclusive OR and assignment operator.<\/td>\n<td width=\"400\">C ^= 2 is same as C = C ^ 2<\/td>\n<\/tr>\n<tr>\n<td width=\"400\">|=<\/td>\n<td width=\"400\">bitwise inclusive OR and assignment operator.<\/td>\n<td width=\"400\">C |= 2 is same as C = C | 2<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4 id=\"miscellaneous-operators\"><span style=\"color: #003300;\"><strong>Miscellaneous Operators<\/strong><\/span><\/h4>\n<p>There are few other operators supported by Java Language.<\/p>\n<ul>\n<li>Conditional Operator ( ? : )<\/li>\n<li>Conditional operator is also known as the\u00a0ternary operator.<\/li>\n<li>This operator consists of three operands and is used to evaluate Boolean expressions.<\/li>\n<li>The goal of the operator is to decide, which value should be assigned to the variable.<\/li>\n<\/ul>\n<p>The operator is written as:<br \/>\nvariable x = (expression) ? value if true : value if false<\/p>\n<h4 id=\"instanceof-operator\"><span style=\"color: #ff6600;\"><strong>instanceof Operator<\/strong><\/span><\/h4>\n<ul>\n<li>This operator is used only for object reference variables.<\/li>\n<li>The operator checks whether the object is of a particular type (class type or interface type).<\/li>\n<\/ul>\n<p><strong>instanceof operator is written as:<\/strong><br \/>\n( Object reference variable ) instanceof (class\/interface type)<\/p>\n<ul>\n<li>If the object referred by the variable on the left side of the operator passes the IS-A check for the class\/interface type on the right side, then the result will be true.<\/li>\n<li>Arithmetic Compound Assignment Operators In Java<\/li>\n<\/ul>\n<p><strong>Consider General Syntax:<\/strong><br \/>\nnum1 = num1 + 2<br \/>\n<strong>Now after using arithmetic Compound Assignment Statement , Equivalent Statement for above statement is written as:<\/strong><br \/>\nnum1 += 2<\/p>\n<ul>\n<li>Step 1 : Write Statement (With using Arithmetic Operator inside Two Operands)<\/li>\n<li>Step 2 : Write Arithmetic Operator before Assignment Sign.<\/li>\n<li>Step 3 : Remove First Operand which is same as \u201cLeft Value\u201d.<\/li>\n<li>Step 4 : We will get Arithmetic Compound Assignment Statement Expression.<\/li>\n<\/ul>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter wp-image-1520 size-full\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/03\/Picture3-2.png\" alt=\"\" width=\"313\" height=\"439\" \/><\/p>\n<p><strong>Examples :<\/strong><\/p>\n<p>Arithmetic Compound Assignment Operators<\/p>\n<table width=\"889\">\n<tbody>\n<tr>\n<td width=\"298\">Operator<\/td>\n<td width=\"591\">Use of operator<\/td>\n<\/tr>\n<tr>\n<td width=\"298\">n1 += 2<\/td>\n<td width=\"591\">n1 = n1 + 2<\/td>\n<\/tr>\n<tr>\n<td width=\"298\">n1 -= 2<\/td>\n<td width=\"591\">n1 = n1 \u2013 2<\/td>\n<\/tr>\n<tr>\n<td width=\"298\">n1 *= 2<\/td>\n<td width=\"591\">n1 = n1 * 2<\/td>\n<\/tr>\n<tr>\n<td width=\"298\">n1 \/= 2<\/td>\n<td width=\"591\">n1 = n1 \/ 2<\/td>\n<\/tr>\n<tr>\n<td width=\"298\">n1 %= 2<\/td>\n<td width=\"591\">n1 = n1 % 2<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Example 1:<\/strong><\/p>\n[pastacode lang=\u201djava\u201d manual=\u201d%0Aclass%20WikitechyCompoundAssignment%20%7B%0A%20%20public%20static%20void%20main(String%20args%5B%5D)%20%7B%0A%20%20%20%20int%20a%20%3D%204%3B%0A%20%20%20%20int%20b%20%3D%205%3B%0A%20%20%20%20int%20c%20%3D%203%3B%0A%0A%20%20%20%20a%20%2B%3D%201%3B%0A%20%20%20%20b%20*%3D%201%3B%0A%20%20%20%20c%20%25%3D%201%3B%0A%0A%20%20%20%20System.out.println(%22a%20%3D%20%22%20%2B%20a)%3B%0A%20%20%20%20System.out.println(%22b%20%3D%20%22%20%2B%20b)%3B%0A%20%20%20%20System.out.println(%22c%20%3D%20%22%20%2B%20c)%3B%0A%20%20%7D%0A%7D%0A\u201d message=\u201dJava Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><strong>OUTPUT:<\/strong><br \/>\na = 5<br \/>\nb = 5<br \/>\nc = 0<\/p>\n<p><strong>Example 2:<\/strong><\/p>\n[pastacode lang=\u201djava\u201d manual=\u201dclass%20WikitechyCompoundAssignmentDemo%20%7B%0A%20%20public%20static%20void%20main(String%20args%5B%5D)%20%7B%0A%20%20%20%20int%20a%20%3D%204%3B%0A%20%20%20%20int%20b%20%3D%205%3B%0A%20%20%20%20int%20c%20%3D%206%3B%0A%0A%20%20%20%20a%20%2B%3D%20b%20*%20c%3B%0A%0A%20%20%20%20System.out.println(%22a%20%3D%20%22%20%2B%20a)%3B%0A%20%20%7D%0A%7D%0A\u201d message=\u201dJava Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n<p><strong>Output :<\/strong><\/p>\n<p>a = 34<\/p>\n<p><strong>Explanation :<\/strong><\/p>\n<ul>\n<li>We have used multiplication Operator inside Expression.<\/li>\n<li>Multiplication Operator have High Priority than Compound Assignment.<\/li>\n<li>It will be executed first and after completing multiplication , Value is Added with \u201ca\u201d.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Java provides a set of operators to manipulate variables. We can divide all the Java operators into the following groups: Arithmetic Operators Relational Operators Bitwise Operators Logical Operators Assignment Operators Miscellaneous Operators Arithmetic Operators: Arithmetic operators are used in mathematical expressions in the same way that they are used in algebra. Operator Description Example + [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2139],"tags":[3091,3090,3085,3070,3072,3081,3080,3088,3074,3078,3071,3077,3073,3075,3079,3076,3086,3087,3082,3083,2519,3089,3084],"class_list":["post-1512","post","type-post","status-publish","format-standard","hentry","category-java","tag-for-short-primitives","tag-but-no-non-assignment-operators","tag-3085","tag-arithmetic-compound-assignment-operators-in-java","tag-compound-assignment-operators-in-java-example","tag-increment-and-decrement-operators-in-java","tag-increment-operator-java-definition","tag-java-order-of-operations-using-two-assignment-operators-in-a-single-line","tag-java-meaning","tag-java-operator-vs-equals","tag-java-arithmetic-compound-operator","tag-java-assignment-operators","tag-java-basic-operators","tag-java-compound-assignment-operators","tag-java-conditional-operator","tag-java-operator-question-mark","tag-operator-overloading","tag-what-does-the-c-operator-do","tag-what-is-the-not-not-operator-in-javascript","tag-what-is-the-operator-in-c","tag-which-equals-operator-vs-should-be-used-in-javascript-comparisons","tag-why-are-there-assignment-operators","tag-why-doesnt-java-have-compound-assignment-versions-of-the-conditional-and-and-conditional-or-operators"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/1512","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=1512"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/1512\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=1512"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=1512"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=1512"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}