
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Assignment Operator Examples
The following program is a simple example that demonstrates the assignment operators. Copy and paste the following Java program in Test.java file. Compile and run this program −
Example
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 ); a = 10; c = 15; c /= a ; System.out.println("c /= a = " + c ); a = 10; c = 15; c %= a ; System.out.println("c %= a = " + c ); c <<= 2 ; System.out.println("c <<= 2 = " + c ); c >>= 2 ; System.out.println("c >>= 2 = " + c ); c >>= 2 ; System.out.println("c >>= 2 = " + 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
This will produce the following result −
c = a + b = 30 c += a = 40 c -= a = 30 c *= a = 300 c /= a = 1 c %= a = 5 c <<= 2 = 20 c >>= 2 = 5 c >>= 2 = 1 c &= a = 0 c ^= a = 10 c |= a = 10
- Related Questions & Answers
- Java Unary Operator Examples
- Java Arithmetic Operator Examples
- Java Shift Operator Examples
- Java AND Operator Examples
- Java OR Operator Examples
- Java Ternary Operator Examples
- What is Assignment Operator (=) in JavaScript?
- What is Multiplication Assignment Operator (*=) in JavaScript?
- What is Addition Assignment Operator (+=) in JavaScript?
- What is an assignment operator in C#?
- Copy constructor vs assignment operator in C++
- Java Assignment Operators
- What is Bitwise OR Assignment Operator (|=) in JavaScript?
- What is Bitwise XOR Assignment Operator (^=) in JavaScript?
- How to use an assignment operator in C#?
Advertisements