Operators in Java

Operators in Java, Operator Precedence in Java, Arithmetic Operators, Relational Operators, Assignment Operators, and Logical Operators.

Java Operators

Operators are used to perform Arithmetic, Comparison, and Logical Operations.

Operators are used to perform operations on variables and values.

Example:

public class JavaOperators {

public static void main(String[] args) {
int a=25, b=20;

int c=a+b;
int d=30*40;
System.out.println(c);//45
System.out.println(d);//70
System.out.println(100+200);//300
System.out.println(“Selenium” + ” Testing”);//Selenium Testing

if (a>b){
System.out.println(“A is a Big Number”);
}

for (int i=1; i<=5; i++){
System.out.println(i);
}
//System.out.println(i);// i is local variable
System.out.println(b);
}
}

Important Categories of Operators in Java:

1. Arithmetic Operators

2. Unary Operators

3. Relational Operators

4. Assignment Operators

5. Logical Operators

1. Arithmetic Operators

Mathematical Operations

Addition

Subtraction

Multiplication

Division

Integer Division

Modules

Exponentiation
——————————-
Java Arithmetic Operators

a) Addition + (Addition, String Concatenation)

b) Subtraction – (Subtraction, Negation)

c) Multiplication *

d) Division /

e) Modules %

f) Increment ++

g) Decrement —
——————————-
Example:

public static void main(String[] args) {
int a =10, b=3, e=-100;
String c =”Selenium”, d = ” Testing”;

System.out.println(“Addition of a, b is “+ (a+b));
System.out.println(c+d);//Selenium Testing

System.out.println(a-b);//7
System.out.println(e);

System.out.println(a*b);//30

System.out.println(a/b);//3
System.out.println(10.0/3);//3.333333

System.out.println(a % b);//1

b=10;
a = ++b;
System.out.println(a);//11

b=10;
a = –b;
System.out.println(a);//9

double x = Math.pow(10, 4); //10000
System.out.println(x);
//————————————-
float y =1.2F, p=2.34f;
double z =1.345678, q=234.456787;

System.out.println(y+p);//3.54
System.out.println(z+q);
}
}

2. Unary Operators

The Java Unary operators require only one operand, Unary operators are used to performing various operators like increment, decrements, etc…

Java Unary Operators

++, —

Example:

int x=10;

System.out.println(x);//10
//System.out.println(x++);//10
x++;
System.out.println(x);//11

System.out.println(++x);//12
++x;
System.out.println(x);//13
x–;
System.out.println(x);//12

System.out.println(–x);//11

for (int i=1; i<=5; i++) {
System.out.println(i);
}
System.out.println(“”);
for (int i=5; i>=1; i–) {
System.out.println(i);
}
}
}

3. Relational Operators

= (Assignment)
== (Comparison/Relation)
——————————-
a) ==

b) !=

c) >

d) >=

e) <

f) <=
—————————–
Note: Relational Operators return Boolean / Logical Result (true/false)

Example:

public static void main(String[] args) {
int a, b;
a=10; b=20;

System.out.println(a == b);//false
System.out.println(a != b);//true
System.out.println(a > b);//false
System.out.println(a >= b); //false
System.out.println(a < b); //true
System.out.println(a <= b); //true
}

4. Assignment Operators

a) Assignment =

b) Add and Assign +=

c) Subtract and Assign -=

d) Multiply and Assign *=
——————————-
Example:

public static void main(String[] args) {
int a =10;
System.out.println(a);//10

a += 10;
System.out.println(a);//20

a -= 10;
System.out.println(a);//10

a *= 10;
System.out.println(a);//100
}

5. Logical Operators

a) Logical Not operator !

b) Logical And operator &&

c) Logical Or operator ||
——————————-
Result Criteria for Logical Not Operator

Operand 1 Operand 2 Result
———————————-
true             true               false
true             false              true
false            true               true
false            false              true
———————————-
Result Criteria for Logical And Operator

Operand 1 Operand 2 Result
——————————-
true true true
true false false
false true false
false false false
——————————-
Result Criteria for Logical Or Operator

Operand 1 Operand 2 Result
——————————-
true true true
true false true
false true true
false false false
——————————-
Example:

public static void main(String[] args) {
boolean a = true, b = false;
System.out.println(!(a && b));//true
System.out.println(a && b);//false
System.out.println(a || b);//true
}
——————————-
Example 2:

public static void main(String[] args) {
int a =10000, b =5000, c =700;
if ((a > b) && (a > c)){
System.out.println(“A is a Big Number”);
}
else {
System.out.println(“A is Not a Big Number”);
}
}
——————————-
public static void main(String[] args) {
int a =10000, b =50000, c =70000;
if ((a > b) || (a > c)){
System.out.println(“A is a Big Number”);
}
else {
System.out.println(“A is Not a Big Number”);
}
}
———————————-
public static void main(String[] args) {
int a =100, b =50, c =70;
if (!(a > b) && (a > c)){
System.out.println(“A is a Big Number”);
}
else {
System.out.println(“A is Not a Big Number”);
}
}


Java Programming Language Syllabus

Java Step by Step Videos

Follow me on social media: