2.1-Datatype, Operators, Expression - PPT in Java
2.1-Datatype, Operators, Expression - PPT in Java
• Overview of
– Identifiers
– Data types
– Operators
– Expressions
CTO38-3-2 Object Oriented Development with Java Data types. Operators and expressions
Slide 3 of 35
Learning Outcomes
CTO38-3-2 Object Oriented Development with Java Data types. Operators and expressions
Slide 5 of 35
Identifiers/Variables
Variables
int temperature; // The Fahrenheit
temperature
Think of variable like a container for a value :
32
temperature
temperature = 32; // temperature contains the value 32
Identifiers/Variables
Identifiers/Variables
int Temp;
temp=3;
cause Java to give the error
CTO38-3-2 Object Oriented Development with Java Data types. Operators and expressions
Slide 8 of 35
Scope of Variables
CTO38-3-2 Object Oriented Development with Java Data types. Operators and expressions
Slide 9 of 35
Keywords
CTO38-3-2 Object Oriented Development with Java Data types. Operators and expressions
Slide 10 of 35
Data types
CTO38-3-2 Object Oriented Development with Java Data types. Operators and expressions
Slide 11 of 35
Data types
CTO38-3-2 Object Oriented Development with Java Data types. Operators and expressions
Slide 12 of 35
CTO38-3-2 Object Oriented Development with Java Data types. Operators and expressions
Slide 13 of 35
CTO38-3-2 Object Oriented Development with Java Data types. Operators and expressions
Slide 14 of 35
• Reference types :
– Arrays
– Classes
CTO38-3-2 Object Oriented Development with Java Data types. Operators and expressions
Slide 15 of 35
CTO38-3-2 Object Oriented Development with Java Data types. Operators and expressions
Slide 16 of 35
CTO38-3-2 Object Oriented Development with Java Data types. Operators and expressions
Slide 17 of 35
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
Slide 18 of 35
Operators in Java
CTO38-3-2 Object Oriented Development with Java Data types. Operators and expressions
Slide 19 of 35
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
Slide 20 of 35
Operators in Java
Relational
A relational operator compares 2 values and determines
the relationship between them.
Operator Use Description
> op1 > op2 Op1 is greater than op2
>= op1 >= op2 Op1 is greater than or equal to op2
< op1 < op2 Op1 is smaller than op2
<= op1 <= op2 Op1 is smaller than or equal to op2
== op1 == op2 Op1 is equal to op2
!= op1 != op2 Op1 is not equal to op2
CTO38-3-2 Object Oriented Development with Java Data types. Operators and expressions
Slide 21 of 35
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
Slide 26 of 35
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
Slide 27 of 35
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
- = op1 -= op2 op1 = op1 - op2
* = op1 *= op2 op1 = op1 * op2
/ = op1 /= op2 op1 = op1 / op2
%= op1 %= op2 op1 = op1 % op2
&= op1 &= op2 op1 = op1 & op2
| = op1 |= op2 op1 = op1 | op2
CTO38-3-2 Object Oriented Development with Java Data types. Operators and expressions
Slide 28 of 35
Expressions
Expressions
CTO38-3-2 Object Oriented Development with Java Data types. Operators and expressions
Slide 30 of 35
Expressions
CTO38-3-2 Object Oriented Development with Java Data types. Operators and expressions
Slide 31 of 35
Follow Up Assignment
ax2 + bx + c = 0
CTO38-3-2 Object Oriented Development with Java Data types. Operators and expressions
Slide 32 of 35
– Identifiers
– Data types
– Operators
– Expressions
CTO38-3-2 Object Oriented Development with Java Data types. Operators and expressions
Slide 34 of 35
Q&A
CTO38-3-2 Object Oriented Development with Java Data types. Operators and expressions
Slide 35 of 35
Next Session
• Conditional constructs
– If… else construct
– Nested if…else constructs
– Switch…. Case
• Break and continue statements
CTO38-3-2 Object Oriented Development with Java Data types. Operators and expressions