Core Java

Basic Operators – Java += Operator

Hello readers, in this tutorial, we will learn the Java += operator.

1. Introduction

Java programming offers Operators that are used to perform various operations in Java. In this tutorial, we’ll only cover the += operator that falls under the Compound Assignment operators’ category.

1.1 Compound assignment operators in Java

Following table list the different compound assignment operators.

OperatorDescriptionSyntaxIs equivalent to?
+=Compound addition assignment operatorele2 += ele1ele2 = ele2+ele1
-=Compound subtraction assignment operatorele2 -= ele1ele2 = ele2-ele1
*=Compound multiplication assignment operatorele2 *= ele1ele2 = ele2*ele1
/=Compound division assignment operatorele2 /= ele1ele2 = ele2/ele1
%=Compound modulo assignment operatorele2 %= ele1ele2 = ele2%ele1

To start with the captioned tutorial, we are hoping that users at present have their preferred Ide installed on their machines. For easy usage, I am using Eclipse Ide on a Windows operating system.

2. Basic Operators – Java += Operator

In this example, we’ll demonstrate the += operator in java. For a better understanding, developers can execute the below code in Eclipse Ide.

Example.java

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
package com.example;
public class Example {
    // Basic operators - Java += Operator.
    public static void main(String[] args) {
        int ele1 = 5;
        int ele2 = 5;
        ele2 += ele1;   // ele2 += ele1 is equal to ele2 = ele2+ele1;
        System.out.println("+= Output is: "+ ele2);
String str1 = "Java";
        String str2 = "Geek";
        str2 += str1;   // str2 += str1 is equal to str2 = str2+str1;
        System.out.println("+= Output is: "+ str2);
    }
}

If everything goes well, the following output will be printed in the Ide console.

Output

1
2
3
+= Output: 10
+= Output: GeekJava

That is all for this tutorial and I hope the article served you whatever you were looking for. Happy Learning and do not forget to share!

3. Java += Operator – Summary

In this tutorial, we learned the += operator in Java. Developers can download the sample application as an Eclipse project in the Downloads section.

4. Download the Source Code

This was an example of += operator in Java programming language.

Download
You can download the full source code of this example here: Basic Operators – Java += Operator

Yatin

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button