0% found this document useful (0 votes)
11 views6 pages

1_Java Operators_6

The document provides an overview of Java operators, categorizing them into five types: Arithmetic, Assignment, Relational, Logical, and Unary operators. It includes examples demonstrating the use of each operator type, such as performing mathematical operations, assigning values, comparing variables, and making logical decisions. Additionally, it introduces the ternary operator as a concise alternative to if-else statements.

Uploaded by

Mani Baluchamy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views6 pages

1_Java Operators_6

The document provides an overview of Java operators, categorizing them into five types: Arithmetic, Assignment, Relational, Logical, and Unary operators. It includes examples demonstrating the use of each operator type, such as performing mathematical operations, assigning values, comparing variables, and making logical decisions. Additionally, it introduces the ternary operator as a concise alternative to if-else statements.

Uploaded by

Mani Baluchamy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Java Operators

Operators are symbols that perform operations on variables and values.

For example, + is an operator used for addition,


while * is also an operator used for multiplication.

Operators in Java can be classified into 5 types:

Arithmetic Operators
Assignment Operators
Relational Operators
Logical Operators
Unary Operators

Arithmetic Operators

Arithmetic operators are used to perform common mathematical operations.

Example : Arithmetic Operators

public class Main {


public static void main(String args[]) {

int a = 5;
int b = 3;
int c;
c= a + b;
int d=b-a;
int e=a*b;
int f = a/b;
int g = a%b;
int h= ++b;
int j= --a;
System.out.println("Addtion of two numbers : "+c);
System.out.println("Subtrction of two numbers : "+d);
System.out.println("Multiplication of two numbers : "+e);
System.out.println("Division of two numbers : "+f);
System.out.println("Returns the division remainder : "+g);
System.out.println("Increases the value of a variable by 1 : "+h);
System.out.println("Decreases the value of a variable by 1 : "+j);
}
}

Output :

Addtion of two numbers : 8


Subtrction of two numbers : -2
Multiplication of two numbers : 15
Division of two numbers : 1
Returns the division remainder : 2
Increases the value of a variable by 1 : 4
Decreases the value of a variable by 1 : 4

Assignment operators

Assignment Operators are mainly used to assign the values to the variable

Example : Assignment operators

public class Main {


public static void main(String[] args) {
int a = 5;
System.out.println("This number assinged to a :"+a);
a += 3;
System.out.println("This number assinged to a :"+a);
a -= 3;
System.out.println("This number assinged to a :"+a);
a *= 3;
System.out.println("This number assinged to a :"+a);
double x= 5;
x /= 3;
System.out.println("This number assinged to a :"+x);
}
}

Output :

This number assinged to a :5


This number assinged to a :8
This number assinged to a :5
This number assinged to a :15
This number assinged to a :1.6666666666666667

Relational / Comparison Operators

Comparison operators are used to compare two values (or variables). This is important in
programming, because it helps us to find answers and make decisions.

The return value of a comparison is either true or false. These values are known as Boolean values

public class Main {


public static void main(String[] args) {
int x = 50;
int y = 30;
System.out.println(x == y);
System.out.println(x != y);
System.out.println(x > y);
System.out.println(x >= y);
System.out.println(x < y);
System.out.println(x <= y);
}
}

Output :

false
true
true
true
false
false

Logical Operators

Logical Operators in Java check whether the expression is true or false. It is generally used
for making any decisions in Java programming. Not only that but Jump statements in Java are also
used for checking whether the expression is true or false. It is generally used for making any
decisions in Java programming.

Logical Operators
&& expression1 && expression2 only if both of the expressions are true
[ logical AND ]
|| expression1 || expression2 if one of the expressions in true
[ logical OR ]
! !expression f the expression is false and vice-versa
[ logical NOT ]

Example : Logical Operators

public class HelloWorld {


public static void main(String[] args)
{

// && operator
System.out.println((6 > 3) && (8 > 6)); // true
System.out.println((6 > 3) && (8 < 6)); // false

// || operator
System.out.println((6 < 3) || (8 > 6)); // true
System.out.println((6 > 3) || (8 < 6)); // true
System.out.println((6 < 3) || (8 < 6)); // false

// ! operator
System.out.println(!(6 == 3)); // true
System.out.println(!(6 > 3)); // false
}
}
Output :

true
false
true
true
false
true
false

Unary Operator

class Main
{
public static void main(String[] args)
{
// declare variables
int a = 13, b = 13;
int result1, result2;

// original value
System.out.println("Value of a: " + a);

// increment operator
result1 = ++a;
System.out.println("After increment: " + result1);

System.out.println("Value of b: " + b);

// decrement operator
result2 = --b;

System.out.println("After decrement: " + result2);


}
}

Output :

Value of a: 13
After increment: 14
Value of b: 13
After decrement: 12

Ternary Operators

The only conditional operator that accepts three operands is the ternary operator in Java.
Java programmers frequently use it as a one-line alternative to the if-then-else expression. The
ternary operator can be used in place of if-else statements, and it can even be used to create switch
statements with nested ternary operators. The conditional operator uses less space and aids in
writing if-else statements as quickly as possible even if it adheres to the same algorithm as an if-else
statement

public class TernaryOperatorExample {


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

// Using the ternary operator to find the maximum of two numbers


int max = (num1 > num2) ? num1 : num2;

System.out.println("The maximum number is: " + max);


}
}
Output :

The maximum number is: 20

You might also like