You are on page 1of 5

Java Operators

Definition of Operators
Operators are symbols that performs logical or mathematical functions on operands such as variables, constants,
and objects.

Types of Operators
 Unary Operators – Require only one operand
o In Java, there at least four unary operators:
 Negation (-)
 Bitwise Complement (~)
 Increment (++)
 Decrement (--)
o These operators change the values of their operand without using an assignment operator (=)
 Binary Operator – Require two operand

Arithmetic Operator
These are operators that performs mathematical operations to an operand

Operator Description
+ Addition – adds two operands
- Subtraction – subtracts second operand from the first
* Multiplication – multiplies two operands
/ Division – divides numerator by denominator
% Modulo – remainder of the division
Relational Operators
These are operators perform a comparison between two operands
Operator Description

== It checks whether the left operand is equal to the right operand


> It checks whether if the left operand is greater than the right operand
< It checks whether if the left operand is less than the right operand
>= It checks whether if the left operand is greater than or equal the right operand
<= It checks whether if the left operand is less than or equal the right operand
!= It checks whether the left operand is “not” equal to the right operand

Increment and Decrement Operators


These are operators that increases or decreases the value by 1
Operator Description
++ It increases the value of an operand by 1
-- It decreases the value of an operand by 1

Logical Operators
These operators further expand the relational operator since they add more control to it

Operator Description
&& An operator that returns true only when both operands are true
|| An operator that returns true when at least 1 value is true
! It negates the value of an operand

Truth Table
Truth Tables will help us demonstrate the result of a logical operator.
 NOT Gate (!)

A Output
True False
False True
 OR Gate (||)
A B Output
True True True
True False True
False True True
False False False

 .Exclusive OR or XOR Gate (^)


o The XOR operator is a binary operator that returns True when both operands have different values.
Otherwise, it will be False
A B Output
True True False
True False True
False True True
False False False

 AND Gate (&&)


A B Output
True True True
True False False
False True False
False False False

Assignment Operators
These are operators that assign a value from the right operand to the left operand
Operator Description
+= Assignment with Addition
-= Assignment with Subtraction
*- Assignment with Multiplication
/= Assignment with Division
%= Assignment with Modulo

Operator Precedence
Is a hierarchy by which you can evaluate a given expression.
Chapter 5 – Basic Control Structure (Sequence)

Introduction

• The computer executes Java statements one after the other in the order in which they’re written

• The basic structure in algorithm.

• Instructions simplest and action was processed in sequential way (top down approach).

Characteristics

• Each instructions was processed one by one

• No repetition in each instructions

• The last instructions is the end of the algorithm

• Sequence of instructions is same with sequence of instructions in algorithm


Examples

 Baking a Simple Bread

 Filling a cup of tea

You might also like