java basics
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.
// Example of initialization
int a = 10, b = 10;
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
Class/Static Variables
• Static variables can be accessed by calling with the
class name
ClassName.VariableName.
Basic Operators
• Arithmetic Operators
• Relational Operators
• Bitwise Operators
• Logical Operators
• Assignment Operators
• Misc Operators
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.
Basic Operators
• The Arithmetic Operators
Operator Description Example
Increases the value of
++ (Increment) operand by 1. B++ gives 21
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.
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
Basic Operators
• The Bitwise Operators
Operator Description Example
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.
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.
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.
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.
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.
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.
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.
Basic Operators
• The Assignment Operators
Operator Description Example
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
Basic Operators
• The Assignment Operators
Operator Description Example
Basic Operators
• The Assignment Operators
Operator Description Example
IF construct syntax
The IF-
IF-ELSE Construct syntax
Iteration Statements
• a block of statements executed over and over again as
long as a certain condition is true
• SYNTAX:
The do-
do-while Loop
• A block is executed once even before checking the
condition
• Post test ( execute first before checking condition)
• SYTNAX:
• SYNTAX:
The for-
for-each Loop
• Iterations are performed automatically for all the
elements of the collection.
The for-
for-each Loop
• Iterations are performed automatically for all the
elements of the collection.
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[];
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).