[go: up one dir, main page]

0% found this document useful (0 votes)
10 views25 pages

Week 1 Java Operators

Week 1 Java Operators

Uploaded by

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

Week 1 Java Operators

Week 1 Java Operators

Uploaded by

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

Data Types, Operators and

Expressions
e1

Operators in Java

What is an operator?
• An operator takes one or more arguments (operands) and
produces a new value
• An operator in Java can be
– Unary: operates on a single operand
– Binary: operates on 2 operands
– Ternary: operates on 3 operands
• A Java operator can be further classified in accordance with the
scheme as shown in the slide that follows

CTO38-3-2 Object Oriented Development with Java Data types. Operators and expressions
e1

Operators in Java

Java supports a rich set of operators which can be


classified into six categories:
1. Arithmetic operators
2. Relational operators
3. Logical operators
4. Increment/Decrement operators
5. Assignment operators

CTO38-3-2 Object Oriented Development with Java Data types. Operators and expressions
e1

Operators in Java

Arithmetic Operators
– Java language supports the arithmetic operators as listed
below for all integer and real numbers :
Operator Use Description
+ op1 + op2 Adds op1 and op2
- op1 - op2 Subtracts op1 from op2
* op1 * op2 Multiplies op1 by op2
/ op1 / op2 DIvide op1 by op2
% op1 % op2 The remainder of dividing op1 by op2

CTO38-3-2 Object Oriented Development with Java Data types. Operators and expressions
e2

Operators in Java

Relational
A relational operator compares 2 values and determines
the relationship between them.
O p e r ato r Use Description
> o p 1 > op2 O p 1 is greater t h a n o p 2
>= o p 1 > = op2 O p 1 is greater t h a n or e q u a l to op2
< o p 1 < op2 O p 1 is s maller t h a n op2
<= o p 1 < = op2 O p 1 is s maller t h a n or e q u a l to o p 2
== o p 1 = = op2 O p 1 is e q u a l to op2
!= o p 1 != op2 O p 1 is not e q u a l to o p 2

CTO38-3-2 Object Oriented Development with Java Data types. Operators and expressions
e2

Operators in Java

Logical Operators
Relational operators are often used with logical operators to
construct more complex decision-making expressions.
Operator Use Description
op1 and op2 are both true,
&& op1 && op2 conditionally evaluates op2
Either op1 and op2 are true,
|| op1 || op2 conditionally evaluates op2
! ! op op is false
op1 and op2 are both true, always
& op1 & op2 evaluates op2
Either op1 and op2 are true, always
| op1 | op2 evaluates op2
Shorthand for and if-else statement
The ?: operator evaluates expression
?: expression ? op1 : op2
and returns op1 if it’s true and op2 if
it’s false

CTO38-3-2 Object Oriented Development with Java Data types. Operators and expressions
e2

Operators in Java

Increment/decrement Operators

++ increase value by 1
- - decrease value by 1
Eg.
i++ OR ++i
k- - OR - -k

CTO38-3-2 Object Oriented Development with Java Data types. Operators and expressions
e2

Operators in Java

Assignment Operators
Assignment operators are used to assign one value
to another. Listed below are the basic assignment
operators (=) as well as the shortcut assignment
operators.
Operator Use Description
= op1 = op2 Op1 = op2
+ = op1 += op2 op1 = op1 + op2
- = o p 1 -= o p 2 op1 = op1 - op2
* = op1 *= op2 op1 = op1 * op2
/ = op1 /= op2 op1 = op1 / op2
%= op1 % = op2 op1 = op1 % op2
&= op1 &= op2 op1 = op1 & op2
| = o p 1 |= op2 op1 = op1 | op2

CTO38-3-2 Object Oriented Development with Java Data types. Operators and expressions

You might also like