Session 4
Session 4
Session 4
In Java, there are different types of operators. There are arithmetic operators,
relational operators, logical operators and conditional operators. These
operators follow a certain kind of precedence so that the compiler will know
which operator to evaluate first in case multiple operators are used in one
statement.
1. Arithmetic Operators:
Operator Use Description
+ op1 + op2 Adds op1 and op2
* op1 * op2 Multiplies op1 by op2
/ op1 / op2 Divides op1 by op2
% op1 % op2 Computes the remainder of dividing op1 by op2
- op1 – op2 Subtracts op2 from op1
System.out.println("Mixing types...");
System.out.println(" j + y = " + (j + y));
System.out.println(" i * x = " + (i * x));
}
}
Different Types of Operators
2. Assignment Operators
• Sample program that uses relational operator Greater than or equal to:
public class RelationalDemo
{
public static void main(String[] args)
{ Output:
//a few numbers Greater than or equal to...
int i = 37; i >= j = false
int j = 42; j >= i = true
int k = 42; k >= j = true
//greater than or equal to
System.out.println("Greater than or equal to...");
System.out.println(" i >= j = " + (i >= j)); //false
System.out.println(" j >= i = " + (j >= i)); //true
System.out.println(" k >= j = " + (k >= j)); //true
}
}
Different Types of Operators
4. Relational Operators:
Operator Use Description
> op1 > op2 op1 is greater than op2
>= op1 >= op2 op1 is greater than or equal to op2
< op1 < op2 op1 is less than to op2
<= op1 <= op2 op1 is less than or equal to op2
== op1== op2 op1 and op2 are equal
!= op1 != op2 op1 and op2 are not equal
• Sample program that uses relational operator Less than or equal to:
public class RelationalDemo
{
public static void main(String[] args)
{ Output:
//a few numbers Less than or equal to...
int i = 37; i <= j = true
int j = 42; j <= i = false
int k = 42; k <= j = true
//less than or equal to
System.out.println("Less than or equal to...");
System.out.println(" i <= j = " + (i <= j)); //true
System.out.println(" j <= i = " + (j <= i)); //false
System.out.println(" k <= j = " + (k <= j)); //true
}
}
Different Types of Operators
4. Relational Operators:
Operator Use Description
> op1 > op2 op1 is greater than op2
>= op1 >= op2 op1 is greater than or equal to op2
< op1 < op2 op1 is less than to op2
<= op1 <= op2 op1 is less than or equal to op2
== op1== op2 op1 and op2 are equal
!= op1 != op2 op1 and op2 are not equal
Given an expression,
exp1 || exp2
//print status
System.out.println( status );
}
}