0% found this document useful (0 votes)
8 views23 pages

Operators in Java

Operators in Java
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
8 views23 pages

Operators in Java

Operators in Java
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 23

Operators in Java

S.Kavitha
Head & Assistant Professor
Department of Computer Science
Sri Sarada Niketan College of Science for
Women,Karur.
The operators available in Java are divided in the
following categories
• Relational Operators
• Arithmetic Operators
• Logical Operators
• Bitwise Operators
• Misc Operators
• Assignment Operators
The Arithmetic Operators:
Variables for performing arithmetic operations
class Main
{
public static void main(String[] args)
{
// declare variables
int a = 12, b = 5;
// addition operator
System.out.println("a + b = " + (a + b));
// subtraction operator
System.out.println("a - b = " + (a - b));
// multiplication operator
System.out.println("a * b = " + (a * b));
// division operator
System.out.println("a / b = " + (a / b));
// modulo operator
System.out.println("a % b = " + (a % b));
}}
Output
a + b = 17 a - b = 7 a * b = 60 a / b = 2 a % b = 2
The Relational Operators:
Relational operators are used to check the
relationship between two operands.
class Main
{
public static void main(String[] args)
{
// create variables
int a = 7, b = 11;
// value of a and b
System.out.println("a is " + a + " and b is " + b);
// == operator
System.out.println(a == b); // false
// != operator
System.out.println(a != b); // true
// > operator
System.out.println(a > b); // false
// < operator
System.out.println(a < b); // true
// >= operator
System.out.println(a >= b); // false
// <= operator
System.out.println(a <= b); // true
}}
The Bitwise Operators:
Bitwise operators in Java are used to perform
operations on individual bits
• For instance, if you consider the example of an
integer x, which has the value 60. Therefore,
the binary equivalent of x is 00111100.
Consider another variable y, with the value 13
or 00001101. If we perform the bitwise
operation & on these two numbers, then you
will get the following result: x&y = 0000 1100
The Logical Operators:
Logical operators are used to check whether an
expression is true or false. They are used in
decision making.
class Main
{
public static void main(String[] args)
{
// && operator
System.out.println((5 > 3) && (8 > 5)); // true System.out.println((5
> 3) && (8 < 5)); // false
// || operator
System.out.println((5 < 3) || (8 > 5)); // true System.out.println((5 >
3) || (8 < 5)); // true System.out.println((5 < 3) || (8 < 5)); // false
// ! operator
System.out.println(!(5 == 3)); // true System.out.println(!(5 > 3)); //
false
}}
Working of Program
• (5 > 3) && (8 > 5) returns true because both (5 > 3) and (8
> 5) are true.
• (5 > 3) && (8 < 5) returns false because the expression (8 <
5) is false.
• (5 < 3) || (8 > 5) returns true because the expression (8 >
5) is true.
• (5 > 3) || (8 < 5) returns true because the expression (5 >
3) is true.
• (5 < 3) || (8 < 5) returns false because both (5 < 3) and (8
< 5) are false.
• !(5 == 3) returns true because 5 == 3 is false.
• !(5 > 3) returns false because 5 > 3 is true.
Assignment Operators
• Assignment operators are used in Java to
assign values to variables. For example,
• int age; age = 5;
• Here, = is the assignment operator. It assigns
the value on its right to the variable on its left.
That is, 5 is assigned to the variable age.
class Main
{
public static void main(String[] args)
{ // create variables
int a = 4;
int var;
// assign value using = var = a;
System.out.println("var using =: " + var);
// assign value using =+ var += a;
System.out.println("var using +=: " + var);
// assign value using =* var *= a;
System.out.println("var using *=: " + var);
}}
Output
• var using =: 4
• var using +=: 8
• var using *=: 32
Java Unary Operators
• Unary operators are used with only one
operand.
• For example, ++ is a unary operator that
increases the value of a variable by 1. That
is, ++5 will return 6.
Increment and Decrement Operators
• increases the value of the operand by 1,
while -- decrease it by 1. For example,
• int num = 5;
• // increase num by 1 ++num;Here, the value
of num gets increased to 6 from its initial
value of 5.
class Main
{
public static void main(String[] args)
{
// declare variables
int a = 12, b = 12;
int result1, result2;
// original value S
ystem.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); } }
Run Code
Output
Value of a: 12
After increment: 13
Value of b: 12
After decrement: 11
Java instanceof Operator
The instanceof operator checks whether an object is an
instanceof a particular class
class Main
{
public static void main(String[] args)
{
String str = "Programiz";
boolean result;
// checks if str is an instance of
// the String class
result = str instanceof String;
System.out.println("Is str an object of String? " + result);
}}
• Output
Is str an object of String? True

Here, str is an instance of the String class.


Hence, the instanceof operator returns true
Java Ternary Operator
• The ternary operator (conditional operator) is
shorthand for the if-then-else statement.
For example,
• variable = Expression ? expression1 :
expression2Here's how it works.
• If the Expression is true, expression1 is
assigned to the variable.
• If the Expression is false, expression2 is
assigned to the variable.
class Java
{
public static void main(String[] args)
{
int februaryDays = 29;
String result;
// ternary operator
result = (februaryDays == 28) ? "Not a leap year" :
"Leap year";
System.out.println(result);
}}

You might also like