Вы находитесь на странице: 1из 21

Object Oriented Development with

Java
(CT038-3.5-2)

Programming Concepts
Data Types, Operators & Expression
Decision making & Looping

Recap
Nil

CT038-3.5-2 Object Oriented Development with Java

Programming Concepts

Learning Outcome
At the end of this lesson you will be able to:
understand data types and object type
use primitive data types and wrapper classes
explain standard operators
apply if, if-else, else-if and nested if and switch
apply while, do-while, for, enhanced-for

CT038-3.5-2 Object Oriented Development with Java

Programming Concepts

Primitive Data Types


Every data type has a range of values.
The compiler allocates memory spaces to store
each variable or constant according to its data
type.
Java has six numeric types:
- byte (8 bit)
- short (16 bit)
- int (32 bit)

CT038-3.5-2 Object Oriented Development with Java

- long (64 bit)


- float (32 bit)
- double (64 bit)

Programming Concepts

Numeric Wrapper Class


Primitive data types are NOT used as objects in Java due
to performance considerations.
Reason: overhead of processing objects.
E.g., wrapping int into the Integer class.
The corresponding classes called wrapper class.

CT038-3.5-2 Object Oriented Development with Java

Programming Concepts

String class
The char represents only one character.
A string is a set of characters, usually represents
by a String class.
It is a predefined class in Java library. (special
class for data type).

CT038-3.5-2 Object Oriented Development with Java

Programming Concepts

Numeric Literal
A literal is a constant value that appears directly
in a program. E.g.,

int i = 43;
long k = 100000;
double d = 5.0;
String s = Welcome to Java;

An integer literal can be assigned to an integer


variable as long as it can fit into the variable.
Otherwise, compilation error if the literal is too
large for the variable to hold.
CT038-3.5-2 Object Oriented Development with Java

Programming Concepts

Numeric Operator

CT038-3.5-2 Object Oriented Development with Java

Programming Concepts

Equality and Relational Operator

CT038-3.5-2 Object Oriented Development with Java

Programming Concepts

Conditional Operator
Symbol

Description

&

Logical AND

&&

Conditional AND

Logical OR

||

Conditional OR

Exclusive OR (XOR)

Logical negation (NOT)

CT038-3.5-2 Object Oriented Development with Java

Programming Concepts

10

instanceof Operator
The Type Comparison Operator
instanceof
The instanceof operator compares an
object to a specified type. You can use it to
test if an object is an instance of a class, an
instance of a subclass, or an instance of a
class that implements a particular interface.

CT038-3.5-2 Object Oriented Development with Java

Programming Concepts

11

Selection Statement
Simple if statement
if(boolean){
statements

If..else statement
if(boolean){
statements

}else{
statements

CT038-3.5-2 Object Oriented Development with Java

Programming Concepts

12

Selection Statement
Nested if statement
if(boolean){
if(boolean){
statements
}
}else if(boolean){
statements
}else{
statements
}

CT038-3.5-2 Object Oriented Development with Java

Programming Concepts

13

Selection Statement
A switch works with the byte, short, char, and
int primitive data types.
It also works with enumerated types, the String class,
and a few special classes that wrap certain primitive
types:
Character, Byte, Short, and Integer.
switch (month) {
case 1: statement
break;
case 2: statement
break;
default: statement
break;
}
CT038-3.5-2 Object Oriented Development with Java

Programming Concepts

14

Loop Statement
while loop
while(loop-continuation-condition){
//loop body

do-while loop
do{
//loop body

}while(loop-continuation-condition);

CT038-3.5-2 Object Oriented Development with Java

Programming Concepts

15

Loop Statement
for loop
for(initial-action; loop-continuation-condition;
action-after-each-iteration){
//loop body

CT038-3.5-2 Object Oriented Development with Java

Programming Concepts

16

Loop Statement
Enhanced-for
The enhanced for-loop is a popular feature
introduced with the Java SE platform in
version 5.0.
Its simple structure allows one to simplify
code by presenting for-loops that visit each
element of an array/collection without
explicitly expressing how one goes from
element to element.

CT038-3.5-2 Object Oriented Development with Java

Programming Concepts

17

Loop Statement
Enhanced-for
for (int i=0; i < array.length; i++) {
System.out.println("Element: " + array[i]);
}

to the newer form,


for (String element : array) {
System.out.println("Element: " + element);
}

CT038-3.5-2 Object Oriented Development with Java

Programming Concepts

18

Summary
We discussed the module covering the following:
data types and object type
primitive data types and wrapper classes
standard operators
if, if-else, else-if and nested if and switch
while, do-while, for, enhanced-for

CT038-3.5-2 Object Oriented Development with Java

Programming Concepts

19

Q&A

CT038-3.5-2 Object Oriented Development with Java

Programming Concepts

20

Next Lecture
Principles of Visual Modeling from
Software Development Perspective

CT038-3.5-2 Object Oriented Development with Java

Programming Concepts

21

Вам также может понравиться