Session 4

Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

Different Types of Operators

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

Note: When an integer and a floating-point number are used as operands


to a single arithmetic operation, the result is a floating point. The integer is
implicitly converted to a floating-point number before the operation takes
place.
Different Types of Operators
Sample Program in the usage of Arithmetic operators:

public class Addition


{
public static void main(String[] args)
{
//a few numbers
int i = 37;
int j = 42;
double x = 27.475;
double y = 7.22; Output of the program:
Variable values...
System.out.println("Variable values..."); i = 37
System.out.println(" i = " + i); j = 42
System.out.println(" j = " + j); x = 27.475
System.out.println(" x = " + x); y = 7.22
System.out.println(" y = " + y); Adding...
i + j = 79
//adding numbers x + y = 34.695
System.out.println("Adding...");
System.out.println(" i + j = " + (i + j));
System.out.println(" x + y = " + (x + y));
}
}
Different Types of Operators
Sample Program in the usage of these operators:

public class Subtraction


{
public static void main(String[] args)
{
//a few numbers
int i = 37;
int j = 42;
double x = 27.475;
double y = 7.22;
Output of the program:
System.out.println("Variable values..."); Variable values...
System.out.println(" i = " + i); i = 37
System.out.println(" j = " + j); j = 42
System.out.println(" x = " + x); x = 27.475
System.out.println(" y = " + y); y = 7.22
Subtracting...
//subtracting numbers i - j = -5
System.out.println("Subtracting..."); x - y = 20.255
System.out.println(" i - j = " + (i - j));
System.out.println(" x - y = " + (x - y));
}
}
Different Types of Operators
Sample Program in the usage of these operators:

public class Multiplication


{
public static void main(String[] args)
{
//a few numbers
int i = 37;
int j = 42;
double x = 27.475;
double y = 7.22;
Output of the program:
System.out.println("Variable values..."); Variable values...
System.out.println(" i = " + i); i = 37
System.out.println(" j = " + j); j = 42
System.out.println(" x = " + x); x = 27.475
System.out.println(" y = " + y); y = 7.22
Multiplying...
//multiplying numbers i * j = 1554
System.out.println("Multiplying..."); x * y = 198.37
System.out.println(" i * j = " + (i * j));
System.out.println(" x * y = " + (x * y));
}
}
Different Types of Operators
Sample Program in the usage of these operators:

public class Division


{
public static void main(String[] args)
{
//a few numbers
int i = 37;
int j = 42;
double x = 27.475;
double y = 7.22;
Output of the program:
System.out.println("Variable values..."); Variable values...
System.out.println(" i = " + i); i = 37
System.out.println(" j = " + j); j = 42
System.out.println(" x = " + x); x = 27.475
System.out.println(" y = " + y); y = 7.22
Dividing...
//dividing numbers i/j=0
System.out.println("Dividing..."); x / y = 3.8054
System.out.println(" i / j = " + (i / j));
System.out.println(" x / y = " + (x / y));
}
}
Different Types of Operators
Sample Program in the usage of these operators:

public class Modulus


{
public static void main(String[] args)
{
//a few numbers
int i = 37;
int j = 42; Output of the program:
int x = 27; Variable values...
int y = 7; i = 37
j = 42
System.out.println("Variable values..."); x = 27
System.out.println(" i = " + i); y=7
System.out.println(" j = " + j); Computing the remainder...
System.out.println(" x = " + x); i % j = 37
System.out.println(" y = " + y); x%y=6

//computing the remainder resulting from dividing numbers


System.out.println("Computing the remainder...");
System.out.println(" i % j = " + (i % j));
System.out.println(" x % y = " + (x % y));
}
}
Different Types of Operators
Sample Program in the usage of these operators:

public class MixOperation


{
public static void main(String[] args)
{
//a few numbers
int i = 37;
int j = 42; Output of the program:
double x = 27.475; Variable values...
double y = 7.22; i = 37
j = 42
System.out.println("Variable values..."); x = 27.475
System.out.println(" i = " + i); y = 7.22
System.out.println(" j = " + j); Mixing types...
System.out.println(" x = " + x); j + y = 49.22
System.out.println(" y = " + y); i * x = 1016.575

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

Operator Use Description


= Num2 = Num1 Would assign the value of Num1 to the
variable Num2
+= Num2 += Num1 Num2 = Num2 + Num1
-= Num2 -= Num1 Num2 = Num2 - Num1
*= Num2 *= Num1 Num2 = Num2 * Num1
/= Num2 /= Num1 Num2 = Num2 / Num1
%= Num2 %= Num1 Num2 = Num2 % Num1
Different Types of Operators
Sample Program in the usage of assignment operators:
public class AssignmentOperatorDemo
{
public static void main(string args[ ] )
{
int num1 = 10;
int num2 = 20;
num2 = num1;
System.out.println("= Output: "+num2);
num2 += num1; Output:
System.out.println("+= Output: "+num2);
= Output: 10
num2 -= num1; += Output: 20
System.out.println("-= Output: "+num2); -= Output: 10
num2 *= num1; *= Output: 100
System.out.println("*= Output: "+num2); /= Output: 10
%= Output: 0
num2 /= num1;
System.out.println("/= Output: "+num2);
num2 %= num1;
System.out.println("%= Output: "+num2);
}
}
Different Types of Operators

3. Increment and Decrement Operators:

• Unary increment operator (++)


• Unary decrement operator (--)
• Increment and decrement operators increase and decrease a value
stored in a number variable by 1.

Example: count = count + 1;


//increment the value of count by 1 is equivalent to, count ++;

Operator Use Description


++ op++ Increments op by 1; evaluates to the value of op
before it was incremented
++ ++op Increments op by 1; evaluates to the value of op
after it was incremented
-- op-- Decrements op by 1; evaluates to the value of op
before it was decremented
-- --op Decrements op by 1; evaluates to the value of op
after it was decremented
Different Types of Operators
Operator Use Description
++ op++ Increments op by 1; evaluates to the value of op before it
was incremented
++ ++op Increments op by 1; evaluates to the value of op after it was
incremented
-- op-- Decrements op by 1; evaluates to the value of op before it
was decremented
-- --op Decrements op by 1; evaluates to the value of op after it
was decremented

• The increment and decrement operators can be placed before or after an


operand.
• When used before an operand, it causes the variable to be incremented or
decremented by 1, and then the new value is used in the expression in which it
appears.
Example: int I = 10;
int j = 3;
int k = 0;
k = ++j + I; //will result to k = 4 + 10 = 14
• When the increment and decremented operators are placed after the operand, the old
value of the variable will be used in the expression where it appears.
Example: int I = 10;
int j = 3;
int k = 0;
k = j++ + I; // will result to k = 3 + 10 = 13
Different Types of Operators
4. Relational Operators:
• Relational operators compare two values and determines the relationship
between those values.
• The output of evaluation are the boolean values true or false.
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 operators:


public class RelationalDemo
{
public static void main(String[] args)
{
//a few numbers
int i = 37;
int j = 42; Output:
int k = 42;
System.out.println("Variable values...");
Variable values...
System.out.println(" i = " + i); i = 37
System.out.println(" j = " + j); j = 42
System.out.println(" k = " + k); k = 42
}
}
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 Greater than:


public class RelationalDemo
{
public static void main(String[] args)
{ Output:
//a few numbers Greater than...
int i = 37; i > j = false
int j = 42; j > i = true
int k = 42; k > j = false
//greater than
System.out.println("Greater than...");
System.out.println(" i > j = " + (i > j)); //false
System.out.println(" j > i = " + (j > i)); //true
System.out.println(" k > j = " + (k > j)); //false
}
}
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 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

• Sample program that uses relational operator Equal to:


public class RelationalDemo
{
public static void main(String[] args)
{ Output:
//a few numbers Equal to...
int i = 37; i == j = false
int j = 42; k == j = true
int k = 42;
//equal to
System.out.println("Equal to...");
System.out.println(" i == j = " + (i == j)); //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

• Sample program that uses relational operator Not equal to:


public class RelationalDemo
{
public static void main(String[] args)
{
//a few numbers Output:
int i = 37; Not equal to...
int j = 42; i != j = true
int k = 42; k != j = false
//not equal to
System.out.println("Not equal to...");
System.out.println(" i != j = " + (i != j)); //true
System.out.println(" k != j = " + (k != j)); //false
}
}
Different Types of Operators
5. Logical operators:
• Logical operators have one or two boolean operands that yield a boolean
result.
• Logical operators:
&& (logical AND)
& (boolean logical AND)
|| (logical OR)
| (boolean logical inclusive OR)
! (logical NOT)
• The basic expression for a logical operation is, x1 op x2 where, x1,x2
- can be boolean expressions, variables or constants op – is either &&,
&, ||, |, or ! operator

&& (logical) and & (boolean logical) AND


Given an expression,
exp1 && exp2
&& will evaluate the expression exp1, and immediately return a false
value if exp1 is false. If exp1 is false, the operator never evaluates exp2
because the result of the operator will be false regardless of the value of
exp2. In contrast, the & operator always evaluates both exp1 and exp2
before returning an answer.
Different Types of Operators
5. Logical operators:
Sample source code that uses logical and boolean AND,
public class TestAND
{
public static void main( String[] args )
{
int i = 0;
int j = 10;
boolean test= false;
//demonstrate &&
test = (i > 10) && (j++ > 9); Output:
System.out.println(i); 0
System.out.println(j); 10
System.out.println(test); false
//demonstrate & 0
test = (i > 10) & (j++ > 9); 11
System.out.println(i); false
System.out.println(j);
System.out.println(test);
}
}
Note, that the j++ on the line containing the && operator is not evaluated
since the first expression (i>10) is already equal to false.
Different Types of Operators
5. Logical operators:

|| (logical) and | (boolean logical) inclusive OR

Given an expression,

exp1 || exp2

|| will evaluate the expression exp1, and immediately return a


true value if exp1 is true. If exp1 is true, the operator never
evaluates exp2 because the result of the operator will be true
regardless of the value of exp2. In contrast, the | operator always
evaluates both exp1 and exp2 before returning an answer.
Different Types of Operators
5. Logical operators:
Sample source code that uses logical and boolean OR,
public class TestOR
{
public static void main( String[] args )
{
int i = 0;
int j = 10;
boolean test= false;
//demonstrate ||
test = (i < 10) || (j++ > 9); Output:
System.out.println(i); 0
System.out.println(j); 10
System.out.println(test); true
//demonstrate | 0
test = (i < 10) | (j++ > 9); 11
System.out.println(i); true
System.out.println(j);
System.out.println(test);
}
}
Note, that the j++ on the line containing the || operator is not evaluated
since the first expression (I < 10) is already equal to true.
Different Types of Operators
5. Logical operators:
! (logical NOT)
The logical NOT takes in one argument, wherein that argument can be an
expression, variable or constant.

Sample source code that uses the logical NOT operator,


public class TestNOT
{
public static void main( String[] args )
{
boolean val1 = true;
boolean val2 = false;
System.out.println(!val1);
System.out.println(!val2);
}
}

The output of the program is,


false
true
Different Types of Operators
6. Conditional operator (?:)
The conditional operator ?: is a ternary operator. This means that it takes
in three arguments that together form a conditional expression. The
structure of an expression using a conditional operator is,
exp1?exp2:exp3
wherein exp1 is a boolean expression whose result must either be true or
false. If exp1 is true, exp2 is the value returned. If it is false, then exp3 is
returned.
Flowchart using the ?: operator
Different Types of Operators
6. Conditional operator (?:)

Sample program code #1

public class ConditionalOperator


{
public static void main( String[] args )
{
String status = "";
int grade = 80;

//get status of the student


status = (grade >= 60)?"Passed":"Fail";

//print status
System.out.println( status );
}
}

Output of the program: Passed


Different Types of Operators
6. Conditional operator (?:)

Sample program code #2


public class TernaryOperatorDemo
{
public static void main(String args[])
{
int num1, num2;
num1 = 25;
num2 = (num1 == 10) ? 100: 200;
System.out.println( "num2: "+num2);
/* num1 is not equal to 10 that's why the second value
* after colon is assigned to the variable num2
*/
num2 = (num1 == 25) ? 100: 200;
System.out.println( "num2: "+num2);
/* num1 is equal to 25 that's why the first value
* is assigned to the variable num2
*/
}
}
Output: num2: 200
num2: 100

You might also like