Compound Assignment Operators in Java
Java Programming

Compound Assignment Operators in Java

 

The compound assignment operator(+=,-=,*=,/=,%=,&=,|=,^=,<<=,>>=,>>>=) is a combination of the assignment operator and another operator such as an arithmetic operator. So they are also known as Shorthand assignment operators.

Syntax:

  Variable operator variable or constant

The following Table shows the compound assignment operators and their usage

Operator                    Expression(shorthand)                   meaning

+=                             vall+=increasee;                            vall=vall+increasee;

-=                             firstt-=25;                                          firstt=firstt-25;

/=                             firstt/=sec;                                         firstt=firstt/sec;

*=                            costt*=qtty+1;                                costt=costt*(qtty+1);

%=                          FIRR%=SECC;                                         FIRR=FIRR%SECC;

&=                          firstt&=seec;                                         firstt=firstt&seec;

|=                           firstt|=seec;                                          firstt=firstt|seec;

^=                           firstt^=seec;                                          firstt=firstt^seec;

<<=                        cpricee<<=spricee;                            cpricee=cpricee<<spricee;

>>=                        firstt>>=seec;                                         firstt=firstt>>seec;

>>>=                     biib>>>=niib;                                       biib=biib>>>niib;

Compound Assignment Operators with example

class CompoundAssignment

{

public static void main(String aa[])

{

int a,b,c;

a=1;

b=2;

c=3;

a+=5;

b*=4;

c+=a*b;

c%=6;

System.out.println(“a=”+a);

System.out.println(“b=”+b);

System.out.println(“c=”+c);

System.out.println(“c%=”+c);

}

}

Compound Assignment Operators in Java

Recommended Posts

C++ Programming

Visibility modes in C++

In C++, visibility modes refer to the accessibility of class members (such as variables and functions) from different parts of a program. C++ provides three visibility modes: public, private, and protected. These modes control the access levels of class members concerning the outside world and derived classes. Public: Members declared as public are accessible from […]

Rekha Setia 
C++ Programming

Inheritance in C++

In C++, inheritance is a fundamental concept of object-oriented programming (OOP) that allows you to create a new class based on an existing class, known as the base or parent class. The new class is called the derived or child class. Inheritance facilitates code reuse and supports the creation of a hierarchy of classes. There […]

Rekha Setia 
C++ Programming

C++ Classes and Objects

In C++, classes and objects are fundamental concepts that support object-oriented programming (OOP). Here’s a brief overview of classes and objects in C++: Classes: In C++, a class is a user-defined data type that allows you to encapsulate data members and member functions into a single unit. Classes are the building blocks of object-oriented programming […]

Rekha Setia 

Leave A Comment