{"id":11408,"date":"2020-12-27T00:19:33","date_gmt":"2020-12-26T18:49:33","guid":{"rendered":"https:\/\/java2blog.com\/?p=11408"},"modified":"2021-01-11T18:12:38","modified_gmt":"2021-01-11T12:42:38","slug":"addition-assignment-operator-java","status":"publish","type":"post","link":"https:\/\/java2blog.com\/addition-assignment-operator-java\/","title":{"rendered":"what does += mean in java"},"content":{"rendered":"<p>In this post, we will see what does += mean in java.<br \/>\n<div id=\"toc_container\" class=\"toc_light_blue no_bullets\"><p class=\"toc_title\">Table of Contents<\/p><ul class=\"toc_list\"><li><a href=\"#Java_operator\">Java += operator<\/a><\/li><li><a href=\"#Using_in_loops\">Using += in loops<\/a><\/li><li><a href=\"#Difference_between_ab_and_aab\">Difference between a+=b and a=a+b<\/a><\/li><li><a href=\"#Using_for_String_concatenation\">Using += for String concatenation<\/a><\/li><\/ul><\/div>\n\n<h2><span id=\"Java_operator\">Java += operator<\/span><\/h2>\n<p><code>+=<\/code> is compound addition assignment operator which adds value of right operand to variable and assign the result to variable. Types of two operands determine the behavior of <code>+=<\/code> in java. <\/p>\n<p>In the case of number, <code>+=<\/code> is used for addition and concatenation is done in case of String.<\/p>\n<p><code>a+=b<\/code> is similar to <code>a=a+b<\/code> with one difference which we will discuss later in the article.<\/p>\n<p>Let&#8217;s see with the help of example:<\/p>\n<pre code=\"java\">\nint i = 4;\ni += 2;\nSystem.out.println(i)\n<\/pre>\n<p><strong>Output:<\/strong> \/\/ Prints 6<\/p>\n<div class=\"content-box-purple\">\n2\n<\/div>\n<h2><span id=\"Using_in_loops\">Using += in loops<\/span><\/h2>\n<p>You can use <code>+=<\/code> in <a href=\"https:\/\/java2blog.com\/for-loop-java-example\/\" target=\"_blank\" rel=\"noopener noreferrer\">for loop<\/a> when you want to increment value of variable by more than 1. In general, you might have used <code>i++<\/code>, but if you want to increment it by 2, then you can use <code>i+=2<\/code>.<\/p>\n<p>Let&#8217;s understand with the help of example:<br \/>\nIf you want to print even number from 0 to 10, then you could use <code>+=<\/code> operator as below:<\/p>\n<pre code=\"java\">\npackage org.arpit.java2blog;\n\npublic class PrintEvenNumberMain {\n\n    public static void main(String[] args) {\n        for (int i=0;i<=10;i+=2)\n        {\n            System.out.print(i+\" \");\n        }\n    }\n}\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"content-box-purple\">\n0 2 4 6 8 10\n<\/div>\n<h2><span id=\"Difference_between_ab_and_aab\">Difference between <code>a+=b<\/code> and <code>a=a+b<\/code><\/span><\/h2>\n<p>If <code>a<\/code> and <code>b<\/code> are of different types, the behavior of <code>a+=b<\/code> and <code>a=a+b<\/code> will differ due to rule of java language.<\/p>\n<p>Let's understand with the help of example:<\/p>\n<pre code=\"java\">\nint x = 2;\nx += 3.2;    \/\/ compile fine; will cast 3.2 to 3 internally\nx = x + 3.2; \/\/ won't compile! 'cannot convert from double to int'\n<\/pre>\n<p>Here, <code>+=<\/code> does implicit cast, where as <code>+<\/code> operator requires explicit cast for second operand, otherwise it won't compile.<\/p>\n<p>As per oracle docs for <a href=\"https:\/\/docs.oracle.com\/javase\/specs\/jls\/se14\/html\/jls-15.html#jls-15.26.2\" target=\"_blank\" rel=\"noopener noreferrer\">compound assignment expression<\/a>:<\/p>\n<blockquote>\n<p>A compound assignment expression of the form E1 op= E2 is equivalent to E1 = (T) ((E1) op (E2)), where T is the type of E1, except that E1 is evaluated only once.<\/p>\n<\/blockquote>\n<p>So,<\/p>\n<pre code=\"java\">\nint x = 2;\nx += 3.2;\n<\/pre>\n<p>is equivalent to:<\/p>\n<pre code=\"java\">\nint x = 2;\nx = (int)(x + 3.2);\nSystem.out.println(x)  \/\/ prints 5\n<\/pre>\n<h2><span id=\"Using_for_String_concatenation\">Using += for String concatenation<\/span><\/h2>\n<p>You can use <code>+=<\/code> operator for String concatenation as well.<\/p>\n<pre code=\"java\">\npackage org.arpit.java2blog;\n\npublic class StringConcatenationWithAddition {\n\n    public static void main(String[] args) {\n        String blogName = \"java\";\n        blogName += \"2blog\";\n        System.out.println(blogName);\n    }\n}\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"content-box-purple\">\njava2blog\n<\/div>\n<p>That's all about what does += mean in java.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn about += operation in java. It is also known as compound addition assignment operator.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_mi_skip_tracking":false},"categories":[215],"tags":[],"_links":{"self":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/11408"}],"collection":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/comments?post=11408"}],"version-history":[{"count":0,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/11408\/revisions"}],"wp:attachment":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/media?parent=11408"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/categories?post=11408"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/tags?post=11408"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}