Anda di halaman 1dari 68

Review on

java basics

CC103- Intermediate Programming


LESSON II – Java Programing Review

LEARNING OUTCOMES:
At the end of the session, the students should be
able to:
• Recall the fundamental programming constructs in
JAVA
• can design, test debug basic java constructs
• create programs in java with loops and arrays.

CC103- INTERMEDIATE PROGRAMMING


2
LESSON II – Java Programing Review

Variable declaration basics:


// Declares three ints, a, b, and c.
int a, b, c;

// Example of initialization
int a = 10, b = 10;

// initializes a byte type variable B.


byte B = 22;

CC103- INTERMEDIATE PROGRAMMING


3
LESSON II – Java Programing Review

Variable declaration basics:


// declares and assigns a value of PI.
double pi = 3.14159;

// the char variable a is initialized with


//value 'a'
char a = 'a';

CC103- INTERMEDIATE PROGRAMMING


4
LESSON II – Java Programing Review

Local variable
• Local variables are declared in methods, constructors,
or blocks.
• Local variables are created when the method,
constructor or block is entered and the variable will be
destroyed once it exits the method, constructor, or
block.
• Access modifiers cannot be used for local variables.

CC103- INTERMEDIATE PROGRAMMING


5
LESSON II – Java Programing Review

Local variable
• Local variables are visible only within the declared
method, constructor, or block.
• Local variables are implemented at stack level
internally.
• There is no default value for local variables, so local
variables should be declared and an initial value should
be assigned before the first use.

CC103- INTERMEDIATE PROGRAMMING


6
LESSON II – Java Programing Review

Local variable
• Local variables are visible only within the declared
method, constructor, or block.
• Local variables are implemented at stack level
internally.
• There is no default value for local variables, so local
variables should be declared and an initial value should
be assigned before the first use.

CC103- INTERMEDIATE PROGRAMMING


7
LESSON II – Java Programing Review

Local variable sample


public class Test {
public void pupAge() {
int age = 0;
age = age + 7;
System.out.println("Puppy age is :" + age);
}

public static void main(String args[]) {


Test test = new Test();
test.pupAge();
}
}

CC103- INTERMEDIATE PROGRAMMING


8
LESSON II – Java Programing Review

Instance Variables
• Instance variables are declared in a class, but outside a
method, constructor or any block.
• When a space is allocated for an object in the heap, a
slot for each instance variable value is created.
• Instance variables are created when an object is
created with the use of the keyword 'new' and
destroyed when the object is destroyed.

CC103- INTERMEDIATE PROGRAMMING


9
LESSON II – Java Programing Review

Instance Variables
• Instance variables hold values that must be referenced
by more than one method, constructor or block, or
essential parts of an object's state that must be
present throughout the class.
• Instance variables can be declared in class level before
or after use.
• Access modifiers can be given for instance variables.

CC103- INTERMEDIATE PROGRAMMING


10
LESSON II – Java Programing Review

Instance Variables
• The instance variables are visible for all methods,
constructors and block in the class. Normally, it is
recommended to make these variables private (access
level). However, visibility for subclasses can be given for
these variables with the use of access modifiers.
• Instance variables have default values. For numbers,
the default value is 0, for Booleans it is false, and for
object references it is null. Values can be assigned
during the declaration or within the constructor.

CC103- INTERMEDIATE PROGRAMMING


11
LESSON II – Java Programing Review

Instance Variables
• Instance variables can be accessed directly by calling
the variable name inside the class. However, within
static methods (when instance variables are given
accessibility), they should be called using the fully
qualified name.

ObjectReference.VariableName.

CC103- INTERMEDIATE PROGRAMMING


12
LESSON II – Java Programing Review

Class/Static Variables
• Class variables also known as static variables are
declared with the static keyword in a class, but outside
a method, constructor or a block.
• There would only be one copy of each class variable per
class, regardless of how many objects are created from
it.
• Static variables are rarely used other than being
declared as constants. Constants are variables that are
declared as public/private, final, and static. Constant
variables never change from their initial value.

CC103- INTERMEDIATE PROGRAMMING


13
LESSON II – Java Programing Review

Class/Static Variables
• Static variables are stored in the static memory. It is
rare to use static variables other than declared final
and used as either public or private constants.
• Static variables are created when the program starts
and destroyed when the program stops.
• Visibility is similar to instance variables. However, most
static variables are declared public since they must be
available for users of the class.

CC103- INTERMEDIATE PROGRAMMING


14
LESSON II – Java Programing Review

Class/Static Variables
• Static variables are stored in the static memory. It is
rare to use static variables other than declared final
and used as either public or private constants.
• Static variables are created when the program starts
and destroyed when the program stops.
• Visibility is similar to instance variables. However, most
static variables are declared public since they must be
available for users of the class.

CC103- INTERMEDIATE PROGRAMMING


15
LESSON II – Java Programing Review

Class/Static Variables
• Default values are same as instance variables. For
numbers, the default value is 0; for Booleans, it is false;
and for object references, it is null. Values can be
assigned during the declaration or within the
constructor. Additionally, values can be assigned in
special static initializer blocks.
• Static variables can be accessed by calling with the
class name
ClassName.VariableName.

CC103- INTERMEDIATE PROGRAMMING


16
LESSON II – Java Programing Review

Class/Static Variables
• Static variables can be accessed by calling with the
class name
ClassName.VariableName.

• When declaring class variables as public static final,


then variable names (constants) are all in upper case. If
the static variables are not public and final, the naming
syntax is the same as instance and local variables.

CC103- INTERMEDIATE PROGRAMMING


17
LESSON II – Java Programing Review

Basic Operators
• Arithmetic Operators
• Relational Operators
• Bitwise Operators
• Logical Operators
• Assignment Operators
• Misc Operators

CC103- INTERMEDIATE PROGRAMMING


18
LESSON II – Java Programing Review

Basic Operators
• The Arithmetic Operators
Operator Description Example
Adds values on either
+ (Addition) side of the operator. A + B will give 30

Subtracts right-hand
- (Subtraction) operand from left-hand A - B will give -10
operand.
Multiplies values on
* (Multiplication) either side of the A * B will give 200
operator.
CC103- INTERMEDIATE PROGRAMMING
19
LESSON II – Java Programing Review

Basic Operators
• The Arithmetic Operators
Operator Description Example
Divides left-hand
/ (Division) operand by right-hand B / A will give 2
operand.
Divides left-hand
operand by right-hand
% (Modulus) B % A will give 0
operand and returns
remainder.

CC103- INTERMEDIATE PROGRAMMING


20
LESSON II – Java Programing Review

Basic Operators
• The Arithmetic Operators
Operator Description Example
Increases the value of
++ (Increment) operand by 1. B++ gives 21

Decreases the value


-- (Decrement) of operand by 1. B-- gives 19

CC103- INTERMEDIATE PROGRAMMING


21
LESSON II – Java Programing Review

Basic Operators
• The Relational Operators
• Operator Description Example
Checks if the values of
two operands are equal
== (equal to) (A == B) is not true.
or not, if yes then
condition becomes true.
Checks if the values of
two operands are equal
!= (not equal to) or not, if values are not (A != B) is true.
equal then condition
becomes true.
CC103- INTERMEDIATE PROGRAMMING
22
LESSON II – Java Programing Review

Basic Operators
• The Relational Operators
• Operator Description Example
Checks if the value of left
operand is greater than the
> (greater than) (A > B) is not true.
value of right operand, if yes
then condition becomes true.
Checks if the value of left
operand is less than the value
< (less than) (A < B) is true.
of right operand, if yes then
condition becomes true.

CC103- INTERMEDIATE PROGRAMMING


23
LESSON II – Java Programing Review

Basic Operators
• The Relational Operators
• Operator Description Example
Checks if the value of left
operand is greater than or equal
>= (greater than (A >= B) is not
to the value of right operand, if
or equal to) true.
yes then condition becomes
true.
Checks if the value of left
<= (less than or operand is less than or equal to
(A <= B) is true.
equal to) the value of right operand, if yes
then condition becomes true.
CC103- INTERMEDIATE PROGRAMMING
24
LESSON II – Java Programing Review

Basic Operators
• The Bitwise Operators
Operator Description Example
Binary AND Operator (A & B) will
& (bitwise
copies a bit to the result if give 12 which
and)
it exists in both operands. is 0000 1100
Binary OR Operator copies (A | B) will
| (bitwise or) a bit if it exists in either give 61 which
operand. is 0011 1101

CC103- INTERMEDIATE PROGRAMMING


25
LESSON II – Java Programing Review

Basic Operators
• The Bitwise Operators
Operator Description Example

Binary XOR Operator copies the bit if it (A ^ B) will give 49


^ (bitwise XOR)
is set in one operand but not both. which is 0011 0001

(~A ) will give -61


which is 1100 0011
Binary Ones Complement Operator is
~ (bitwise in 2's complement
unary and has the effect of 'flipping'
compliment) form due to a
bits.
signed binary
number.

CC103- INTERMEDIATE PROGRAMMING


26
LESSON II – Java Programing Review

Basic Operators
• The Bitwise Operators
Operator Description Example
Binary Left Shift Operator. The
A << 2 will give
left operands value is moved left
<< (left shift) 240 which is
by the number of bits specified
1111 0000
by the right operand.
Binary Right Shift Operator. The
left operands value is moved A >> 2 will give
>> (right shift)
right by the number of bits 15 which is 1111
specified by the right operand.

CC103- INTERMEDIATE PROGRAMMING


27
LESSON II – Java Programing Review

Basic Operators
• The Bitwise Operators
Operator Description Example
Shift right zero fill operator. The
left operands value is moved
A >>>2 will give
>>> (zero fill right by the number of bits
15 which is 0000
right shift) specified by the right operand
1111
and shifted values are filled up
with zeros.

CC103- INTERMEDIATE PROGRAMMING


28
LESSON II – Java Programing Review

Basic Operators
• The Bitwise Operators
Operator Description Example
Binary Left Shift Operator. The
A << 2 will give
left operands value is moved left
<< (left shift) 240 which is
by the number of bits specified
1111 0000
by the right operand.
Binary Right Shift Operator. The
left operands value is moved A >> 2 will give
>> (right shift)
right by the number of bits 15 which is 1111
specified by the right operand.

CC103- INTERMEDIATE PROGRAMMING


29
LESSON II – Java Programing Review

Basic Operators
• The Bitwise Operators
Operator Description Example
Called Logical AND operator. If
both the operands are non-zero,
&& (logical and) (A && B) is false
then the condition becomes
true.
Called Logical OR Operator. If
any of the two operands are
|| (logical or) (A || B) is true
non-zero, then the condition
becomes true.

CC103- INTERMEDIATE PROGRAMMING


30
LESSON II – Java Programing Review

Basic Operators
• The Bitwise Operators
Operator Description Example
Called Logical OR Operator. If
any of the two operands are
|| (logical or) (A || B) is true
non-zero, then the condition
becomes true.
Called Logical NOT Operator.
Use to reverses the logical state
! (logical not) of its operand. If a condition is !
true then Logical NOT operator
will make false.
CC103- INTERMEDIATE PROGRAMMING
31
LESSON II – Java Programing Review

Basic Operators
• The Assignment Operators
Operator Description Example
Simple assignment operator.
C = A + B will assign
= Assigns values from right side
value of A + B into C
operands to left side operand.
Add AND assignment operator. It
adds right operand to the left C += A is equivalent
+=
operand and assign the result to to C = C + A
left operand.

CC103- INTERMEDIATE PROGRAMMING


32
LESSON II – Java Programing Review

Basic Operators
• The Assignment Operators
Operator Description Example
Subtract AND assignment operator.
C -= A is
It subtracts right operand from the
-= equivalent to C = C
left operand and assign the result
–A
to left operand.
Multiply AND assignment operator.
C *= A is
It multiplies right operand with the
*= equivalent to C = C
left operand and assign the result
*A
to left operand.

CC103- INTERMEDIATE PROGRAMMING


33
LESSON II – Java Programing Review

Basic Operators
• The Assignment Operators
Operator Description Example
/= Divide AND assignment operator. It
C /= A is
divides left operand with the right
equivalent to C = C
operand and assign the result to
/A
left operand.
Modulus AND assignment operator.
C %= A is
It takes modulus using two
%= equivalent to C = C
operands and assign the result to
%A
left operand.

CC103- INTERMEDIATE PROGRAMMING


34
LESSON II – Java Programing Review

Basic Operators
• The Assignment Operators
Operator Description Example

Left shift AND assignment C <<= 2 is same


<<=
operator. as C = C << 2

Right shift AND assignment C >>= 2 is same


>>=
operator. as C = C >> 2

CC103- INTERMEDIATE PROGRAMMING


35
LESSON II – Java Programing Review

Basic Operators
• The Assignment Operators
Operator Description Example

C &= 2 is
Bitwise AND assignment
&= same as C = C
operator.
&2
bitwise exclusive OR and C ^= 2 is same
^=
assignment operator. as C = C ^ 2

CC103- INTERMEDIATE PROGRAMMING


36
LESSON II – Java Programing Review

Basic Operators
• The Assignment Operators
Operator Description Example

bitwise exclusive OR and C ^= 2 is same


^=
assignment operator. as C = C ^ 2
bitwise inclusive OR and C |= 2 is same
|=
assignment operator. as C = C | 2

CC103- INTERMEDIATE PROGRAMMING


37
LESSON II – Java Programing Review

Basic Operators
• The Assignment Operators
Operator Description Example

bitwise exclusive OR and C ^= 2 is same


^=
assignment operator. as C = C ^ 2
bitwise inclusive OR and C |= 2 is same
|=
assignment operator. as C = C | 2

CC103- INTERMEDIATE PROGRAMMING


38
LESSON II – Java Programing Review

Java Operator Precedence Table


Precedence Operator Type Associativity
Parentheses
()
Array subscript
15 [] Left to Right
Member selection
·
Unary post-
++ increment
14 Right to left
-- Unary post-
decrement

CC103- INTERMEDIATE PROGRAMMING


39
LESSON II – Java Programing Review

Java Operator Precedence Table


Precedence Operator Type Associativity
Unary pre-increment
++ Unary pre-
decrement
--
Unary plus
+
Unary minus
13 - Right to left
Unary logical
!
negation
~ Unary bitwise
( type ) complement
Unary type cast
CC103- INTERMEDIATE PROGRAMMING
40
LESSON II – Java Programing Review

Java Operator Precedence Table


Precedence Operator Type Associativity
* Multiplication
12 / Division Left to right
% Modulus
+ Addition
11 Left to right
- Subtraction

CC103- INTERMEDIATE PROGRAMMING


41
LESSON II – Java Programing Review

Java Operator Precedence Table


Precedence Operator Type Associativity
Bitwise left shift
<< Bitwise right shift with
10 >> sign extension Left to right
>>> Bitwise right shift with
zero extension
Relational less than
Relational less than or
<
equal
<=
Relational greater than
9 > Left to right
Relational greater than or
>=
equal
instanceof
Type comparison (objects
only)
CC103- INTERMEDIATE PROGRAMMING
42
LESSON II – Java Programing Review

Java Operator Precedence Table


Precedence Operator Type Associativity
Relational is equal
== to
8 Left to right
!= Relational is not
equal to
7 & Bitwise AND Left to right
6 ^ Bitwise exclusive OR Left to right
5 | Bitwise inclusive OR Left to right
4 && Logical AND Left to right

CC103- INTERMEDIATE PROGRAMMING


43
LESSON II – Java Programing Review

Java Operator Precedence Table


Precedence Operator Type Associativity
3 || Logical OR Left to right
2 ?: Ternary conditional Right to left
Assignment
= Addition assignment
+= Subtraction
-= assignment
1 Right to left
*= Multiplication
/= assignment
%= Division assignment
Modulus assignment

CC103- INTERMEDIATE PROGRAMMING


44
LESSON II – Java Programing Review

Selection Control Statements


• Selection implies the capacity to choose among
several alternative course of action
• Sometimes called Branching or decision control
structure
• A selection statement allows the conditional
execution of a block of statements. If a
condition is true, a block of statements will
be executed once, else it will be skipped.

CC103- INTERMEDIATE PROGRAMMING


45
LESSON II – Java Programing Review

Selection Control Types


Two types:
• The if Statements
• If Construct
• If – else Construct
• Nested if-else Construct
• The switch Statement

CC103- INTERMEDIATE PROGRAMMING


46
LESSON II – Java Programing Review

IF construct syntax

• One way selection statement tests whether a


relation or logical expression is true before it allows
the execution of a sub sequent program statement
or compound statement.

CC103- INTERMEDIATE PROGRAMMING


47
LESSON II – Java Programing Review

The IF-
IF-ELSE Construct syntax

• Two-way Selection Statement controls the execution of


two alternative control flow blocks. Like one way Selection
Statement, the result of a relational or logical expression is
also tested.
• If the condition is TRUE or valid/accepted then the
execution is directed to the if block of statement.
• If the condition is result is FALSE or unacceptable from the
value range, the ELSE block will be executed. 48
CC103- INTERMEDIATE PROGRAMMING
LESSON II – Java Programing Review

The NESTED IF-


IF-ELSE Construct syntax

• You can handle multiple blocks of code, and only one of


those blocks will be executed at most.
• a multi-way selection statement offers more than just two
alternative control flow blocks.

CC103- INTERMEDIATE PROGRAMMING


49
LESSON II – Java Programing Review

The NESTED IF-


IF-ELSE Construct syntax

• You can handle multiple blocks of code, and only one of


those blocks will be executed at most.
• a multi-way selection statement offers more than just two
alternative control flow blocks.

CC103- INTERMEDIATE PROGRAMMING


50
LESSON II – Java Programing Review

The NESTED IF-


IF-ELSE Construct syntax

• Having an ELSE at the end or after the


last else if block is optional for catching
other values handled by the if blocks
above it.
CC103- INTERMEDIATE PROGRAMMING
51
LESSON II – Java Programing Review

The switch Statement


• A switch statement allows a variable to
be tested for equality against a list of
values. Each value is called a case, and
the variable being switched on is checked
for each case.
• Unlike if and if-else statements, the
switch statement can have a number of
possible execution paths.
CC103- INTERMEDIATE PROGRAMMING
52
LESSON II – Java Programing Review

The switch Statement


• A switch statement allows a variable to
be tested for equality against a list of
values. Each value is called a case, and
the variable being switched on is checked
for each case.
• Unlike if and if-else statements, the
switch statement can have a number of
possible execution paths.
CC103- INTERMEDIATE PROGRAMMING
53
LESSON II – Java Programing Review

The switch Statement SYNTAX


switch( switch_expression ){
case case_selector1:
statement1;//
statement2;//block 1
break;
case case_selector2:
statement1;//
statement2;//block 2
break;
:
default:
statement1;//
statement2;//block n

CC103- INTERMEDIATE PROGRAMMING


54
LESSON II – Java Programing Review

Iteration Statements
• a block of statements executed over and over again as
long as a certain condition is true

• four iteration constructs:


• While
• do-while
• For
• for-each

CC103- INTERMEDIATE PROGRAMMING


55
LESSON II – Java Programing Review

The while Loop


• The code block in the while loop may not be
executed at all
• Pre test (test condition first before execute block)

• SYNTAX:

CC103- INTERMEDIATE PROGRAMMING


56
LESSON II – Java Programing Review

The do-
do-while Loop
• A block is executed once even before checking the
condition
• Post test ( execute first before checking condition)
• SYTNAX:

CC103- INTERMEDIATE PROGRAMMING


57
LESSON II – Java Programing Review

The for Loop


• A for loop is useful when you know how many times a task
is to be repeated

• SYNTAX:

CC103- INTERMEDIATE PROGRAMMING


58
LESSON II – Java Programing Review

The for-
for-each Loop
• Iterations are performed automatically for all the
elements of the collection.

• It sets the <variable> to the first element of


the collection (or array) during the first iteration, to
the second element during the second iteration, and so
on.
CC103- INTERMEDIATE PROGRAMMING
59
LESSON II – Java Programing Review

The for-
for-each Loop
• Iterations are performed automatically for all the
elements of the collection.

• It sets the <variable> to the first element of


the collection (or array) during the first iteration, to
the second element during the second iteration, and so
on.
CC103- INTERMEDIATE PROGRAMMING
60
LESSON II – Java Programing Review

Declaring Arrays
• To declare an array, write the data type, followed by a set
of square brackets[ ], followed by the identifier name.

For example,
int []ages;
or
int ages[];

CC103- INTERMEDIATE PROGRAMMING


61
LESSON II – Java Programing Review

Array Instantiation
• After declaring, we must create the array and specify its
length with a constructor statement.

Definitions:
– Instantiation - this means creation
– Constructor
● In order to instantiate an object, we need to use a
constructor for this. A constructor is a method that is called
to create a certain object.
● We will cover more about instantiating objects and
constructors later.
CC103- INTERMEDIATE PROGRAMMING
62
LESSON II – Java Programing Review

Array Instantiation
• You can also instantiate an array by directly initializing it
with data.
● For example,
int arr[] = {1, 2, 3, 4, 5};
This statement declares and instantiates an array of
integers with five elements (initialized to the values 1, 2, 3,
4, and 5).

CC103- INTERMEDIATE PROGRAMMING


63
LESSON II – Java Programing Review

Accessing an Array Element


• To access an array element, or a part of the array, you use
a number called an index or a subscript
• index number or subscript
• assigned to each member of the array, to allow the program to
access an individual member of the array.
• begins with zero and progress sequentially by whole numbers to
the end of the array.
• NOTE: Elements inside your array are from 0 to (sizeOfArray-1)

CC103- INTERMEDIATE PROGRAMMING


64
LESSON II – Java Programing Review

Accessing an Array Element


• index number or subscript
• assigned to each member of the array, to allow the program to
access an individual member of the array.
• begins with zero and progress sequentially by whole numbers to
the end of the array.
• NOTE: Elements inside your array are from 0 to (sizeOfArray-1)
• For example, given the array we declared a while ago, we
have:
//assigns 10 to the first element in the array
ages[0] = 10;
//prints the last element in the array
System.out.print(ages[99]);
CC103- INTERMEDIATE PROGRAMMING
65
LESSON II – Java Programing Review

Accessing an Array Element


• The following is a sample code on how to print all the
elements in the array. This uses a for loop, so our code is
shorter.
public class ArraySample{
public static void main( String[] args ){
int[] ages = new int[100];
for( int i=0; i<100; i++ ){
System.out.print( ages[i] );
}
}
}
CC103- INTERMEDIATE PROGRAMMING
66
LESSON II – Java Programing Review

Multidimensional Arrays sample


// integer array with 3 rows and 3 columms
int[][] twoD = new int[3][3];
// character array 8 x 16 x 24
char[][][] threeD = new char[8][16][24];
// String array 4 rows x 2 columns
String[][] nbaTeams={{"boston","dallas"},
{"detroit","denver"},
{"miami","houston"},
{"toronto","utah"}
}; 67
CC103- INTERMEDIATE PROGRAMMING
THE END

CC102- Programming Fundamentals

Anda mungkin juga menyukai