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

Operators in Java

The document discusses different types of operators in Java including arithmetic, assignment, increment/decrement, logical, comparison, and bitwise operators. It provides examples of using the basic arithmetic operators like addition, subtraction, multiplication, and division. Assignment operators are also covered along with examples showing how they update a variable, such as using += to add and assign a value in one step. Examples are given to demonstrate calculating the sum of two numbers using the addition operator.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views

Operators in Java

The document discusses different types of operators in Java including arithmetic, assignment, increment/decrement, logical, comparison, and bitwise operators. It provides examples of using the basic arithmetic operators like addition, subtraction, multiplication, and division. Assignment operators are also covered along with examples showing how they update a variable, such as using += to add and assign a value in one step. Examples are given to demonstrate calculating the sum of two numbers using the addition operator.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Operators in Java

BY CHAITANYA SINGH | FILED UNDER: LEARN JAVA

An operator is a character that represents an action, for example + is an


arithmetic operator that represents addition.

Types of Operator in Java


1) Basic Arithmetic Operators
2) Assignment Operators
3) Auto-increment and Auto-decrement Operators
4) Logical Operators
5) Comparison (relational) operators
6) Bitwise Operators
7) Ternary Operator

1) Basic Arithmetic Operators


Basic arithmetic operators are: +, -, *, /, %
+ is for addition.

– is for subtraction.

* is for multiplication.

/ is for division.

% is for modulo.


Note: Modulo operator returns remainder, for example 10 % 5 would return 0

Example of Arithmetic Operators


public class ArithmeticOperatorDemo {
public static void main(String args[]) {
int num1 = 100;
int num2 = 20;

System.out.println("num1 + num2: " + (num1 + num2) );


System.out.println("num1 - num2: " + (num1 - num2) );
System.out.println("num1 * num2: " + (num1 * num2) );
System.out.println("num1 / num2: " + (num1 / num2) );
System.out.println("num1 % num2: " + (num1 % num2) );
}
}
Output:

num1 + num2: 120


num1 - num2: 80
num1 * num2: 2000
num1 / num2: 5
num1 % num2: 0

2) Assignment Operators
Assignments operators in java are: =, +=, -=, *=, /=, %=
num2 = num1 would assign value of variable num1 to the variable.

num2+=num1 is equal to num2 = num2+num1

num2-=num1 is equal to num2 = num2-num1

num2*=num1 is equal to num2 = num2*num1

num2/=num1 is equal to num2 = num2/num1

num2%=num1 is equal to num2 = num2%num1

Example of Assignment Operators


public class AssignmentOperatorDemo {
public static void main(String args[]) {
int num1 = 10;
int num2 = 20;

num2 = num1;
System.out.println("= Output: "+num2);

num2 += num1;
System.out.println("+= Output: "+num2);

num2 -= num1;
System.out.println("-= Output: "+num2);

num2 *= num1;
System.out.println("*= Output: "+num2);

num2 /= num1;
System.out.println("/= Output: "+num2);

num2 %= num1;
System.out.println("%= Output: "+num2);
}
}
Output:

= Output: 10
+= Output: 20
-= Output: 10
*= Output: 100
/= Output: 10
%= Output: 0

First Example: Sum of two numbers


public class AddTwoNumbers {

public static void main(String[] args) {

int num1 = 5, num2 = 15, sum;


sum = num1 + num2;

System.out.println("Sum of these numbers: "+sum);


}
}
Output:

Sum of these numbers: 20

You might also like