{"id":1513,"date":"2026-11-21T06:06:15","date_gmt":"2026-11-21T06:06:15","guid":{"rendered":"https:\/\/gyanipandit.com\/programming\/?p=1513"},"modified":"2026-11-21T06:06:15","modified_gmt":"2026-11-21T06:06:15","slug":"assignment-operators-java","status":"publish","type":"post","link":"https:\/\/gyanipandit.com\/programming\/assignment-operators-java\/","title":{"rendered":"Assignment Operators in Java (=, +=, -=, *=, \/=, %= and More) with Examples"},"content":{"rendered":"<p>Assignment operators in Java store a value in a variable. The basic one is <code>=<\/code>. Java also has <strong>compound assignment operators<\/strong> such as <code>+=<\/code> and <code>*=<\/code>, which do a calculation and store the result in the same variable in one step. This page covers all of them with examples, outputs and the mistakes beginners usually make.<\/p>\n<h2>List of Assignment Operators in Java<\/h2>\n<table>\n<thead>\n<tr>\n<th>Operator<\/th>\n<th>Name<\/th>\n<th>Example<\/th>\n<th>Same as<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>=<\/code><\/td>\n<td>Simple assignment<\/td>\n<td><code>x = 5<\/code><\/td>\n<td>&#8211;<\/td>\n<\/tr>\n<tr>\n<td><code>+=<\/code><\/td>\n<td>Add and assign<\/td>\n<td><code>x += 5<\/code><\/td>\n<td><code>x = x + 5<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>-=<\/code><\/td>\n<td>Subtract and assign<\/td>\n<td><code>x -= 5<\/code><\/td>\n<td><code>x = x - 5<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>*=<\/code><\/td>\n<td>Multiply and assign<\/td>\n<td><code>x *= 5<\/code><\/td>\n<td><code>x = x * 5<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>\/=<\/code><\/td>\n<td>Divide and assign<\/td>\n<td><code>x \/= 5<\/code><\/td>\n<td><code>x = x \/ 5<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>%=<\/code><\/td>\n<td>Remainder (modulus) and assign<\/td>\n<td><code>x %= 5<\/code><\/td>\n<td><code>x = x % 5<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>&amp;=<\/code><\/td>\n<td>Bitwise AND and assign<\/td>\n<td><code>x &amp;= 5<\/code><\/td>\n<td><code>x = x &amp; 5<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>|=<\/code><\/td>\n<td>Bitwise OR and assign<\/td>\n<td><code>x |= 5<\/code><\/td>\n<td><code>x = x | 5<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>^=<\/code><\/td>\n<td>Bitwise XOR and assign<\/td>\n<td><code>x ^= 5<\/code><\/td>\n<td><code>x = x ^ 5<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>&lt;&lt;=<\/code><\/td>\n<td>Left shift and assign<\/td>\n<td><code>x &lt;&lt;= 2<\/code><\/td>\n<td><code>x = x &lt;&lt; 2<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>&gt;&gt;=<\/code><\/td>\n<td>Right shift and assign<\/td>\n<td><code>x &gt;&gt;= 2<\/code><\/td>\n<td><code>x = x &gt;&gt; 2<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>&gt;&gt;&gt;=<\/code><\/td>\n<td>Unsigned right shift and assign<\/td>\n<td><code>x &gt;&gt;&gt;= 2<\/code><\/td>\n<td><code>x = x &gt;&gt;&gt; 2<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&#8220;Same as&#8221; is true for the result, with one important difference about types, explained further below.<\/p>\n<h2>Example: Arithmetic Assignment Operators<\/h2>\n<pre class=\"wp-block-code\"><code>public class AssignmentDemo {\n    public static void main(String[] args) {\n        int num = 10;      \/\/ simple assignment\n\n        num += 5;          \/\/ 10 + 5\n        System.out.println(num);\n\n        num -= 3;          \/\/ 15 - 3\n        System.out.println(num);\n\n        num *= 2;          \/\/ 12 * 2\n        System.out.println(num);\n\n        num \/= 5;          \/\/ 24 \/ 5 (integer division)\n        System.out.println(num);\n\n        num %= 3;          \/\/ remainder of 4 \/ 3\n        System.out.println(num);\n    }\n}<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>15\n12\n24\n4\n1<\/code><\/pre>\n<p>Notice the fourth line: <code>24 \/ 5<\/code> gives <code>4<\/code>, not <code>4.8<\/code>, because both values are <code>int<\/code>. With a <code>double<\/code> variable, <code>\/=<\/code> keeps the decimal part:<\/p>\n<pre class=\"wp-block-code\"><code>double d = 10;\nd \/= 4;\nSystem.out.println(d);   \/\/ 2.5\n\nint i = 10;\ni \/= 4;\nSystem.out.println(i);   \/\/ 2<\/code><\/pre>\n<h2>Example: Bitwise and Shift Assignment Operators<\/h2>\n<pre class=\"wp-block-code\"><code>int flags = 12;      \/\/ binary 1100\n\nflags &amp;= 10;         \/\/ 1100 &amp; 1010 = 1000\nSystem.out.println(flags);\n\nflags |= 3;          \/\/ 1000 | 0011 = 1011\nSystem.out.println(flags);\n\nflags ^= 5;          \/\/ 1011 ^ 0101 = 1110\nSystem.out.println(flags);\n\nflags &lt;&lt;= 2;         \/\/ shift left by 2 = multiply by 4\nSystem.out.println(flags);\n\nflags &gt;&gt;= 3;         \/\/ shift right by 3 = divide by 8\nSystem.out.println(flags);<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>8\n11\n14\n56\n7<\/code><\/pre>\n<p>The difference between <code>&gt;&gt;=<\/code> and <code>&gt;&gt;&gt;=<\/code> shows up with negative numbers. <code>&gt;&gt;<\/code> keeps the sign; <code>&gt;&gt;&gt;<\/code> fills the left side with zeros:<\/p>\n<pre class=\"wp-block-code\"><code>int a = -16;\na &gt;&gt;= 2;\nSystem.out.println(a);   \/\/ -4\n\nint b = -16;\nb &gt;&gt;&gt;= 2;\nSystem.out.println(b);   \/\/ 1073741820<\/code><\/pre>\n<h2>Compound Assignment Does an Automatic Cast<\/h2>\n<p>This is the one place where <code>x += y<\/code> is <em>not<\/em> exactly the same as <code>x = x + y<\/code>. A compound assignment automatically converts the result back to the variable&#8217;s type.<\/p>\n<pre class=\"wp-block-code\"><code>byte b = 10;\nb += 5;          \/\/ compiles: result is cast back to byte\n\/\/ b = b + 5;    \/\/ compile error: possible lossy conversion from int to byte\nSystem.out.println(b);   \/\/ 15\n\nint x = 10;\nx += 2.7;        \/\/ 12.7 is cast to int\nSystem.out.println(x);   \/\/ 12\n\nchar c = 'A';\nc += 2;\nSystem.out.println(c);   \/\/ C<\/code><\/pre>\n<p>The hidden cast is convenient, but it can also hide overflow:<\/p>\n<pre class=\"wp-block-code\"><code>short s = 30000;\ns += 30000;              \/\/ 60000 does not fit in a short\nSystem.out.println(s);   \/\/ -5536<\/code><\/pre>\n<h2>+= with Strings<\/h2>\n<p><code>+=<\/code> also joins (concatenates) strings:<\/p>\n<pre class=\"wp-block-code\"><code>String site = \"Gyani\";\nsite += \"Pandit\";\nSystem.out.println(site);   \/\/ GyaniPandit<\/code><\/pre>\n<p>Inside a long loop, prefer <code>StringBuilder<\/code>, because every <code>+=<\/code> on a <code>String<\/code> creates a new object.<\/p>\n<h2>Chained Assignment<\/h2>\n<p>The <code>=<\/code> operator works from right to left, so you can give the same value to several variables in one line:<\/p>\n<pre class=\"wp-block-code\"><code>int p, q, r;\np = q = r = 7;\nSystem.out.println(p + \" \" + q + \" \" + r);   \/\/ 7 7 7<\/code><\/pre>\n<h2>Common Mistakes<\/h2>\n<ul>\n<li><strong>Writing <code>=+<\/code> instead of <code>+=<\/code>.<\/strong> <code>x =+ 5;<\/code> compiles, but it means <code>x = +5<\/code>, so <code>x<\/code> becomes 5.<\/li>\n<li><strong>Confusing <code>=<\/code> with <code>==<\/code>.<\/strong> <code>=<\/code> assigns a value; <code>==<\/code> compares two values.<\/li>\n<li><strong>Expecting decimals from <code>\/=<\/code> on an <code>int<\/code>.<\/strong> Use a <code>double<\/code> variable if you need the fraction.<\/li>\n<li><strong>Using a variable before giving it a value.<\/strong> Local variables must be assigned before <code>+=<\/code> can use them.<\/li>\n<\/ul>\n<h2>Frequently Asked Questions<\/h2>\n<h3>How many assignment operators are there in Java?<\/h3>\n<p>Twelve: the simple <code>=<\/code> and eleven compound operators (<code>+=<\/code>, <code>-=<\/code>, <code>*=<\/code>, <code>\/=<\/code>, <code>%=<\/code>, <code>&amp;=<\/code>, <code>|=<\/code>, <code>^=<\/code>, <code>&lt;&lt;=<\/code>, <code>&gt;&gt;=<\/code>, <code>&gt;&gt;&gt;=<\/code>).<\/p>\n<h3>Is x += 1 the same as x++?<\/h3>\n<p>Both increase <code>x<\/code> by 1. The difference is in the value of the expression: <code>x++<\/code> returns the old value, while <code>x += 1<\/code> returns the new value.<\/p>\n<h3>Why does b += 5 work for a byte but b = b + 5 does not?<\/h3>\n<p>In <code>b + 5<\/code>, Java promotes <code>b<\/code> to <code>int<\/code>, so the result is an <code>int<\/code>. A compound assignment casts the result back to <code>byte<\/code> automatically; a plain assignment does not.<\/p>\n<p>Related: <a href=\"https:\/\/gyanipandit.com\/programming\/arithmetic-operators-in-java\/\">Arithmetic operators in Java<\/a> \u00b7 <a href=\"https:\/\/gyanipandit.com\/programming\/java-math\/\">Java Math class<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Assignment operators in Java store a value in a variable. The basic one is =. Java also has compound assignment operators such as += and *=, which do a calculation and store the result in the same variable in one step. This page covers all of them with examples, outputs and the mistakes beginners usually &#8230; <a title=\"Assignment Operators in Java (=, +=, -=, *=, \/=, %= and More) with Examples\" class=\"read-more\" href=\"https:\/\/gyanipandit.com\/programming\/assignment-operators-java\/\" aria-label=\"Read more about Assignment Operators in Java (=, +=, -=, *=, \/=, %= and More) with Examples\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[12,11],"class_list":["post-1513","post","type-post","status-publish","format-standard","hentry","category-java-tutorial","tag-java","tag-java-tutorial"],"_links":{"self":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/1513","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/comments?post=1513"}],"version-history":[{"count":6,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/1513\/revisions"}],"predecessor-version":[{"id":6150,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/1513\/revisions\/6150"}],"wp:attachment":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/media?parent=1513"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/categories?post=1513"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/tags?post=1513"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}