0% found this document useful (0 votes)
10 views

Java - Assignment Operators With Examples

Uploaded by

bikashghoshh41
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Java - Assignment Operators With Examples

Uploaded by

bikashghoshh41
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Java - Assignment Operators with Examples

Java Assignment Operators


Following are the assignment operators supported by Java language −

Operator Description Example

Simple assignment operator. Assigns values C = A + B will assign value of A + B


=
from right side operands to left side operand. into C

Add AND assignment operator. It adds right


+= operand to the left operand and assign the C += A is equivalent to C = C + A
result to left operand.

Subtract AND assignment operator. It


-= subtracts right operand from the left operand C -= A is equivalent to C = C − A
and assign the result to left operand.

Multiply AND assignment operator. It


*= multiplies right operand with the left operand C *= A is equivalent to C = C * A
and assign the result to left operand.

Divide AND assignment operator. It divides


/= left operand with the right operand and assign C /= A is equivalent to C = C / A
the result to left operand.

Modulus AND assignment operator. It takes


%= modulus using two operands and assign the C %= A is equivalent to C = C % A
result to left operand.

<<= Left shift AND assignment operator. C <<= 2 is same as C = C << 2

>>= Right shift AND assignment operator. C >>= 2 is same as C = C >> 2

&= Bitwise AND assignment operator. C &= 2 is same as C = C & 2

bitwise exclusive OR and assignment


^= C ^= 2 is same as C = C ^ 2
operator.

|= bitwise inclusive OR and assignment operator. C |= 2 is same as C = C | 2


The following programs are simple examples which demonstrate the assignment operators.
Copy and paste the following Java programs as Test.java file, and compile and run the programs

Example 1
In this example, we're creating three variables a,b and c and using assignment operators. We've
performed simple assignment, addition AND assignment, subtraction AND assignment and
multiplication AND assignment operations and printed the results.

public class Test {

public static void main(String args[]) {


int a = 10;
int b = 20;
int c = 0;

c = a + b;
System.out.println("c = a + b = " + c );

c += a ;
System.out.println("c += a = " + c );

c -= a ;
System.out.println("c -= a = " + c );

c *= a ;
System.out.println("c *= a = " + c );
}
}

Output

c = a + b = 30
c += a = 40
c -= a = 30
c *= a = 300

Example 2
In this example, we're creating two variables a and c and using assignment operators. We've
performed Divide AND assignment, Multiply AND assignment, Modulus AND assignment, bitwise
exclusive OR AND assignment, OR AND assignment operations and printed the results.

public class Test {

public static void main(String args[]) {


int a = 10;
int c = 15;

c /= a ;
System.out.println("c /= a = " + c );

c = 15;
c %= a ;
System.out.println("c %= a = " + c );

c = 15;
c &= a ;
System.out.println("c &= a = " + c );

c = 15;
c ^= a ;
System.out.println("c ^= a = " + c );

c = 15;
c |= a ;
System.out.println("c |= a = " + c );
}
}

Output

c /= a = 1
c %= a = 5
c &= a = 10
c ^= a = 5
c |= a = 15

Example 3
In this example, we're creating two variables a and c and using assignment operators. We've
performed Left shift AND assignment, Right shift AND assignment, operations and printed the
results.

public class Test {

public static void main(String args[]) {


int a = 10;
int c = 0;

c <<= 2 ;
System.out.println("c <<= 2 = " + c );

c = 15;
c >>= 2 ;
System.out.println("c >>= 2 = " + c );
}
}

Output

c <<= 2 = 0
c >>= 2 = 3

You might also like