Opeartors:
Operators are special symbols used to perform operations on variables and values.
Types of Operators:
1.Assignment Operators:
- Used to assign values. Eg:
package project1;
public class assignment_operator
{
public static void main(String[] args)
{
int a=10 , b=2;
System.out.println(a=b);
}
}
Output:
2
2.Arithmetic Opeartors:
- Used for mathematical calculations.
1. Operator Meaning Example
2. + Addition a + b
3. - Subtraction a - b
4. * Multiplication a * b
5. / Division a / b
6. % Modulus (remainder) a % b
Eg:
package project1;
public class arrithmetic_operator {
public static void main(String[] args)
{
int a=10 , b=2;
System.out.println(a+b);
System.out.println(a-b);
System.out.println(a*b);
System.out.println(a/b);
System.out.println(a%b);
}
}
Output:
12
8
20
5
0
3.Unary Operators
-
Operate on single operand.
1.++ -> Increment operator 2.-- -> Decrement operator 3.no++ --> post increment 4.++no --> Pre increment 5.no-- --> post Decrement 6.--no --> Pre DecrementEg:
package project1;
public class unary_operator
{
public static void main(String[] args)
{
int i = 10;
System.out.println(i++);// post Increment operator 10 + 1
System.out.println(i);//11
int j = 10;
System.out.println(j--); //Post Decrement operator 10 -1
System.out.println(j);
int a = 10;
System.out.println(++a);//Pre increment operator 10+1=11
int b = 20;
System.out.println(--b); //Pre Decrement operator 20-1=19
}
}
Output:
10
11
10
9
11
19
4.Equality and Relational Operators :
- Used to compare two values → result is boolean.
- Operator Meaning
- == Equal to
- != Not equal
- > Greater than
- < Less than
- >= Greater than or equal
- <= Less than or equal
Eg:
package project1;
public class relational_operator
{
public static void main(String[] args)
{
int tamil_mark = 70;
int english_mark = 70;
System.out.println(tamil_mark == english_mark); // boolean
System.out.println(tamil_mark < english_mark); // Boolean
System.out.println(tamil_mark > english_mark);
System.out.println(tamil_mark != english_mark);
}
}
Output:
true
false
false
false
5.Conditional Operators :
- Used with boolean values.
1. && = AND operator
2.|| = OR operator
Top comments (0)