0% found this document useful (0 votes)
35 views16 pages

Lesson 5 Operators

Operators are symbols that perform logical or mathematical functions on operands like variables, constants, and objects in Java. There are several types of operators including arithmetic, relational, conditional/logical, shortcut assignment, and increment/decrement operators. Arithmetic operators perform math operations like addition and subtraction. Relational operators compare values. Conditional/logical operators work on boolean operands and return boolean values. Shortcut assignment operators can assign and perform operations in one statement. Increment/decrement operators increase or decrease a variable by one. The comma operator allows multiple variables to be declared and updated within a for loop.

Uploaded by

Jhezryll May
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views16 pages

Lesson 5 Operators

Operators are symbols that perform logical or mathematical functions on operands like variables, constants, and objects in Java. There are several types of operators including arithmetic, relational, conditional/logical, shortcut assignment, and increment/decrement operators. Arithmetic operators perform math operations like addition and subtraction. Relational operators compare values. Conditional/logical operators work on boolean operands and return boolean values. Shortcut assignment operators can assign and perform operations in one statement. Increment/decrement operators increase or decrease a variable by one. The comma operator allows multiple variables to be declared and updated within a for loop.

Uploaded by

Jhezryll May
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

OPERATORS IN

JAVA
LESSON 5
WHAT IS A JAVA OPERATOR?

• Operators are symbols that perform logical or


mathematical functions on operands such as
variables, constants, and objects. Unary operators
require only one operand.
OPERANDS ARE THE VALUES ON WHICH THE OPERATORS ACT
UPON.

An operand can be:


 A numeric variable – integer, floating point or character
 Any primitive type variable – numeric and Boolean
 Reference variable – to an object
 A literal – numeric value, Boolean value. or string.
 An array element, “a[2]”
 char primitive, which in numeric operations is treated as an unsigned
two byte integer
TYPES OF OPERATORS
1. Arithmetic Operators
2. Relational Operators
3. Conditional/Logical Operators
4. Shortcut Assignment Operators
5. Operator Precedence
6. Increment Decrement Operator
7. Comma Operators
1. ARITHMETIC OPERATORS - ARITHMETIC OPERATORS ARE THE
SYMBOLS THAT REPRESENT ARITHMETIC MATH OPERATIONS.

EXAMPLES:
Operators Description
+ Addition – adds two operands

- Subtraction - subtracts second operand


form first
* Multiplication – multiplies two operands

/ Division – divides numerator by denominator

% Modulo – remainder of division

++ Increment – increases integer value by one

-- Decrement - decreases integer value by one


2. RELATIONAL OPERATORS – A RELATIONAL OPERATOR COMPARES

TWO VALUES AND DETERMINES THE RELATIONSHIP BETWEEN

THEM.
• For example, != returns true if its two operands
are unequal. Relational operators are used to test
whether two values are equal, whether one value
is greater than another, and so forth.
Operator Description

== Checks if the values of two operands are equal or not, if yes the
condition becomes true.

!= Checks if the values of two operands are equal or not, if values are not
equal the condition becomes true.

> Checks if the value of the left operand is greater than the value of right
operand, if yes the condition becomes true.

< Checks if the value of left operand is less than the value of right
operand, if yes then condition becomes true.

>= Checks if the value of left operand is less than or equal to the value of
right operand, if yes then condition becomes true.

<= Checks if the value to left operand is less than or equal to the value of
right operand, if yes then condition becomes true.
EXAMPLE OF RELATIONAL OPERATORS:
public Less ThanExample
{
public static void main(String args[])
{
int a = 5;
int b = 10;
if(a<b)
{
System.out.println(“The Statement is true”);
}
}
}
3. CONDITIONAL/LOGICAL OPERATORS – THESE LOGICAL OPERATORS WORK ONLY ON
BOOLEAN OPERANDS. THEIR RETURN VALUES ARE ALWAYS BOOLEAN.

• Much like relational operators, the result of using logical operators is a


Boolean value: either true or false.
Operators Description
! logical NOT
II logical OR
&& logical AND
ogical AND
4. Shortcut Assignment Operators – An assignment operator is
the operator used to assign a new value to a variable, property,
event or indexer element in C# programming
language. Assignment operators can also be used for logical
operations such as bitwise logical operations or operations on
integral operands and Boolean operands.
THE ASSIGNMENT STATEMENTS HAS THE FOLLOWING SYNTAX:
ASSIGNING PRIMITIVE VALUE
Int a, b;
a = 2; // 2 is assigned to variable a
b = 5; // 5 is assigned to variable b

Various Types of assignment operators such as:


• =
• -=
• +=
• *=
• /=
• **=
5. INCREMENT DECREMENT OPERATOR – ARE USED TO INCREASE THE VALUE OF THE
VARIABLE BY ONE AND DECREMENT OPERATORS ARE USED TO DECREASE THE
VALUE OF THE VARIABLE BY ONE IN C PROGRAMS.

• Increment and Decrement Operators ++ AND - -


• Common Shorthand
•a = a + 1 ; a++; or ++a;
•a = a - 1 ; a--; or --a;
6. Comma Operators – Java has an often look past
feature within it’s for loop and this is the comma
operator. Usually when people think about commas in
the java language, they think of a way to split up
arguments within a functions parameters.
//: c03:CommaOperator.java
// From Thinking in Java, 3rd ed.’ (c) Bruce Eckel 2002
// www.BruceEckel.com. See copyright notice in CopyRight.txt.
public class CommaOperator {
public static void main(String[] args) {
for(int I = 1, j = I + 10; I < 5;
i++, j = I * 2) {
System.out.print(“i=” + i + “ + j);
}
}
} ///;”n
THANK YOU!!!

You might also like