What Does += Mean In Java - Java2Blog
Maybe your like
In this post, we will see what does += mean in java.
Table of Contents
- Java += operator
- Using += in loops
- Difference between a+=b and a=a+b
- Using += for String concatenation
Java += operator
+= 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 += in java.
In the case of number, += is used for addition and concatenation is done in case of String.
a+=b is similar to a=a+b with one difference which we will discuss later in the article.
Let’s see with the help of example:
int i = 4; i += 2; System.out.println(i)| 12345 | inti=4;i+=2;System.out.println(i) |
Output: // Prints 6
2Using += in loops
You can use += in for loop when you want to increment value of variable by more than 1. In general, you might have used i++, but if you want to increment it by 2, then you can use i+=2.
Let’s understand with the help of example: If you want to print even number from 0 to 10, then you could use += operator as below:
package org.arpit.java2blog; public class PrintEvenNumberMain { public static void main(String[] args) { for (int i=0;i<=10;i+=2) { System.out.print(i+" "); } } }| 12345678910111213 | packageorg.arpit.java2blog; publicclassPrintEvenNumberMain{ publicstaticvoidmain(String[]args){for(inti=0;i<=10;i+=2){System.out.print(i+" ");}}} |
Output:
0 2 4 6 8 10Difference between a+=b and a=a+b
If a and b are of different types, the behavior of a+=b and a=a+b will differ due to rule of java language.
Let’s understand with the help of example:
int x = 2; x += 3.2; // compile fine; will cast 3.2 to 3 internally x = x + 3.2; // won't compile! 'cannot convert from double to int'| 12345 | intx=2;x+=3.2;// compile fine; will cast 3.2 to 3 internallyx=x+3.2;// won't compile! 'cannot convert from double to int' |
Here, += does implicit cast, where as + operator requires explicit cast for second operand, otherwise it won’t compile.
As per oracle docs for compound assignment expression:
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.
So,
int x = 2; x += 3.2;| 1234 | intx=2;x+=3.2; |
is equivalent to:
int x = 2; x = (int)(x + 3.2); System.out.println(x) // prints 5| 12345 | intx=2;x=(int)(x+3.2);System.out.println(x)// prints 5 |
Using += for String concatenation
You can use += operator for String concatenation as well.
package org.arpit.java2blog; public class StringConcatenationWithAddition { public static void main(String[] args) { String blogName = "java"; blogName += "2blog"; System.out.println(blogName); } }| 123456789101112 | packageorg.arpit.java2blog; publicclassStringConcatenationWithAddition{ publicstaticvoidmain(String[]args){StringblogName="java";blogName+="2blog";System.out.println(blogName);}} |
Output:
java2blogThat’s all about what does += mean in java.
Was this post helpful?
Let us know if this post was helpful. Feedbacks are monitored on daily basis. Please do provide feedback as that\'s the only way to improve. Yes NoRelated posts:
- XOR operator in java
- Modulo operator in java
- Question mark operator in java
Tag » What Does A Mean In Java
-
What Does "?" Mean In Java? - Ternary Operator - Stack Overflow
-
Java - Basic Operators - Tutorialspoint
-
What Does % Mean In Java? - Quora
-
What Does :: Mean In Java? - Tech With Maddy
-
What Does ! Mean In Java With Examples
-
What Does This Mean In Java? - Linux Hint
-
What Is += Addition Assignment Operator In Java? - DigitalOcean
-
What Does
Mean In Java? - DEV Community -
Java Operators - W3Schools
-
Java This Keyword - W3Schools
-
What Does @ Means In Java?? | Sololearn: Learn To Code For FREE!
-
A Java Symbol Decoder
-
Java Main() Method - Public Static Void Main(String[] Args)
-
What Does "!==" Mean In JAVA? - Codecademy