Module1 - Chapter6 - Core Java
Module1 - Chapter6 - Core Java
MANUAL V8.3
MODULE CODE:
DL.A.01.01
ANUDIP FOUNDATION
Trainer
Manual Core Java
1
Trainer
Manual Core Java
Chapter 6
2
Trainer
Manual Core Java
Chapter 6
Operators in Java
In Java, an operator is a symbol used for performing operations on values and variables. Some examples of operators
include +, -, *, /.
The value on which an operator is applied is known as an operand. The operator plays a defining role in determining
i) Arithmetic Operators
Arithmetic operators in Java are utilised for performing actions like addition (+), multiplication (*), subtraction (-) and
division (/) and modulus (%). Hence, this operator can be used to perform some of the most fundamental
class OperatorExample{
public static void main(String args[]){
int a=20;
int b=10;
3
Trainer
Manual Core Java
System.out.println(a+b); → 30
System.out.println(a-b); → 10
System.out.println(a*b); → 200
System.out.println(a/b); → 2
}}
Output:
30
10
200
class OperatorExample{
public static void main(String args[]){
System.out.println(10*10/5+3-1*4/2);
}}
Output:
21
Logical operators in Java are used for carrying out “logical AND” and “logical OR” operations. This operator is
used to enable the checking of multiple conditions simultaneously. They are also known as Boolean operators as they
4
Trainer
Manual Core Java
&& (Logical AND) - If two operands are true, only then can a ‘logical AND operator’ be considered as ‘true’.
|| (Logical OR) - This operator produces a ‘true’ result only if one of its operands is true. The result remains
! (Logical Not) - It is a Unary operator and functions with individual operands. This operator is used for reversing
operand values. It produces a false result for a true value, and vice versa.
import java.util.*;
{
String x = "Bob";
String y = "Cat";
System.out.print("Enter username:");
System.out.print("Enter password:");
System.out.println("Welcome");
5
Trainer
Manual Core Java
else {
System.out.println("Wrong password or uid");
}
}
}
Output:
Enter username:Bob
Enter password:Cat
Welcome.
Relational operators in Java are utilised for checking relations such as greater than, less than and equality. These
operators perform comparisons and produce boolean outputs. Relational operators are utilised for conditional ‘if
== (equal to): Output is true if the left hand side value is equal to the right hand side value.
!= (not equal to): Output is true if the left hand side value is not equal to the right hand side value.
< (less than): Output is true if the left hand side value is less compared to the right hand side value.
<= (less than or equal to): Output is true if the left hand side value is less than/equal to the right hand side value.
> (greater than): Output is true if the left hand side value is greater compared to the right hand side value.
>= (greater than or equal to): Output is true if the left hand side value is greater than/equal to the right hand side
value.
6
Trainer
Manual Core Java
{
int a = 10, b = 5;
int ar[] = { 1, 2, 3 };
int br[] = { 1, 2, 3 };
System.out.println("condition==true :"
+ (condition == true));
}
}
Output:
a == b :false
a b :true
a >= b :true
7
Trainer
Manual Core Java
a != b :true
x == y : false
condition==true :true
See the example programme for Java arithmetic operators below. Write the same programme with values of int a =
18, and int b = 7 and show the resulting output. Rewrite the programme for int a = 10 and int b = 3.
class OperatorExample{
int a=20;
int b=10;
System.out.println(a+b); → 30
System.out.println(a-b); → 10
System.out.println(a*b); → 200
System.out.println(a/b); → 2
}}
8
Trainer
Manual Core Java
Instructions: The progress of students will be assessed with the exercises mentioned below.
MCQ
a) a sign
b) a symbol
c) a callsign
a) arithmetic
b) relational
c) logical
a) Character
b) Decimal
c) Operand
a) FOR
9
Trainer
Manual Core Java
b) OR
c) IF
d) IF-ELSE
a) Mathematical
b) Transitional
c) Boolean
a) relational
b) logical
c) arithmetic
a) arithmetic
b) logical
c) relational
10
Trainer
Manual Core Java
b) not equal to
c) equal to
a) false
b) sometimes true
c) partially false
a) Enumeration
b) explanation
c) expression
11