Operators:
Java supports various types of
operators, which are used to perform
operations on variables and values.
Common Operators In Java:
Arithmetic Operators
Arithme c operators are used in
mathema cal expressions in the same way that they
are used in algebra. The following table lists the
arithme c operators:
Arithmetic Operators:
+ (addition),
- (subtraction),
* (multiplication),
/ (division),
% (modulus).
Arithmetic Operators Program:
class arith
{
public sta c void main(String args[])
{
int a=30,b=3;
System.out.println("sum is "+(a+b));
System.out.println("difference is "+(a-b));
System.out.println("product is "+(a*b));
System.out.println("division is "+(a/b));
System.out.println("remainder is "+(a%b));
}
}
Assignment Operators:
= , += , = , = , /= , %= .
Assignment Operators Program:
class Assignment
{
public sta c void main(String[] args)
{
// Declaring variables
int no;
String centre;
String address;
// Assigning values
no = 75;
centre = "G-Star Computers";
address = "Kaliyamman Kovil St,15.Velampalayam";
// Displaying the assigned values
System.out.println("Door No is assigned: " + no);
System.out.println("Centre Name is assigned: " + centre);
System.out.println("Address is assigned: " + address);
}
}
Increment and Decrement Operators:
++ (increment), -- (decrement).
Increment Operators:
Increment operators are used in programming languages
to increase the value of a variable by one. There are two types of
increment operators: the prefix increment operator (++x) and the
pos ix increment operator (x++).
Prefix Increment Operator (++x):
The prefix increment operator increases the value of the
variable by 1 before the value is used in the expression. X= 5
Syntax: ++x Now Value is – 1+5 = 6 , X =6
Postfix Increment Operator (x++):
The pos ix increment operator increases the value of the
variable by 1 a er the value is used in the expression.
Syntax: x++ Now Value is – 5 +1 =6 , X = 6
Prefix & Postfix Increment Program
class increment
{
public sta c void main(String[] args)
{
int x = 5;
System.out.println("X Value - " + ++x);
int y = 10;
System.out.println("Y Value - " + y++);
System.out.println("******************");
System.out.println("X Value Prefix increment " + x);
System.out.println("Y Value Post increment " + y);
}
}
Prefix & Postfix Decrement Program
class Decrement
{
public sta c void main(String[] args)
{
int x = 50;
System.out.println("X Value - " + --x);
int y = 60;
System.out.println("Y Value - " + y--);
System.out.println("******************");
System.out.println("X Value Prefix increment " + x);
System.out.println("Y Value Post increment " + y);
}
}
Relational Operators:
== (equals),
!= (not equals),
> (Greater then)
< (Less then)
>= (Greater then Equal to)
<= (Less then Equal to)
Relational Operators Program:
class Rela onal
public sta c void main (String args[])
int a = 10;
int b = 30;
System.out.println("a==b = " +(a==b));
System.out.println("a!=b = " + (a!=b));
System.out.println("a>b = " +(a>b));
System.out.println("a<b = "+(a<b));
System.out.println("a>=b = "+(a>=b));
System.out.println("a<=b = "+(a<=b));
}
Logical Operators:
&& (logical AND),
|| (logical OR),
! (logical NOT).
Logical Operators And Program:
class LogicalAnd
public sta c void main(String args[])
int a=10,b=30,c=20;
System.out.println("Greatest of given 3 numbers");
if(a>b&&a>c)
System.out.println(a);
else if(b>c)
System.out.println(b);
else
System.out.println(c);
}
Logical And / Or / Not Program:
class LogicalOptr
public sta c void main (String args[])
boolean a = true;
boolean b = false;
System.out.println("a||b = " +(a||b));
System.out.println("a&&b = "+(a&&b));
System.out.println("a! = "+(!a));