Java Fundamentals
Java Fundamentals
Sohail Shaghasi
sohail.shaghasi@gmail.com
Course Overview
What is computer program?
What is high level and low level language?
Introduction to Java.
What is an IDE?
How Java program Gets executed (Java life cycle)?
What is Variable?
What is Variable Scope?
What is Constant?
sohail.shaghasi@gmail.com
Course Overview Cont.
What is Operator?
What is primitive type?
What is String?
What is array?
What are control statements (if / else, Switch , For, while, Break/Continue)?
What is function/Method?
What is Class ?
What is Constructor?
What is Object ?
What is Access Modifiers?
What is OOP?
Pillars of OOP
sohail.shaghasi@gmail.com
What is Computer Program?
sohail.shaghasi@gmail.com
What is High level and Low Level
language?
Characteristics of High level languages:
Easier to understand and user friendly.
Extremely portable.
Can be debugged in an easier manner.
sohail.shaghasi@gmail.com
What is an IDE?
IDE means
Integrated Development Environment.
An IDE is software application.
Eclipse, Netbeans and Microsoft Visual studio are IDEs.
You can do many things using IDE
Implement databases.
Design User interfaces.
Program an application.
Compile/build an application
Test an application.
Debug an application.
Diagraming.
sohail.shaghasi@gmail.com
Java life Cycle
How Java program gets executed?
sohail.shaghasi@gmail.com
What is Variable?
Just like a variable in a math equation
1+3=4
1+x=?
Without variables programs would be non-interactive
Variables allow us to store data in our program
Opposite of a constant
Syntax:
The number 3 is a constant
X is a variable
sohail.shaghasi@gmail.com
What is Variable Scope?
Global Variables:
Declared outside any function.
Local Variable:
Declare inside a function.
To simplify things, just think of the scope as anything
between the curly braces {}. The outer curly braces are
called the outer blocks , and the inner curly braces are
called inner blocks.
sohail.shaghasi@gmail.com
What is constant?
sohail.shaghasi@gmail.com
What is Operator?
Assignment
=
+=
-=
*=
/=
%=
sohail.shaghasi@gmail.com
Operators Cont
Relational
>
<
>=
<=
==
!=
sohail.shaghasi@gmail.com
Operators Cont.
Logical
&&
||
!
sohail.shaghasi@gmail.com
What are primitive types?
What is Array?
An array is a group of contiguous or related data items that share a common name.
Used when programs have to handle large amount of data
Each value is stored at a specific position
Position is called a index or superscript. Base index = 0 0 69
1 61
index
2 70
3 89 values
4 23
5 10
sohail.shaghasi@gmail.com
6 9
Array Cont.
Like any other variables, arrays must declared and created before
they can be used. Creation of arrays involve three steps:
Declare the array
Create storage area in primary memory.
Put values into the array (i.e., Memory location)
sohail.shaghasi@gmail.com
What are Control statements?
if else
switch
while
do while
For
For Each
break
continue
sohail.shaghasi@gmail.com
If - else
if(conditional_statement)
{
statement to be executed if conditional_statement becomes true
}
Else
{
statements to be executed if the above conditional_statement
becomes false
}
sohail.shaghasi@gmail.com
Switch
switch (n)
{
case expression1:
// code to be executed if n is equal to expression1;
break;
case expression :
// code to be executed if n is equal to expression1;
break;
default:
// code to be executed if n doesn't match any expression1
}
sohail.shaghasi@gmail.com
Switch statement Flowchart
sohail.shaghasi@gmail.com
While loop
while(condition_statementtrue)
{
Statements to be executed when the condition becomes true and execute
them repeatedly until condition_statement becomes false.
}
E.g.
int x = 2;
while(x>5){
system.out.println(value of x:+x);
x++;
}
sohail.shaghasi@gmail.com
Do while loop
do
{
statements to be executed at least once without looking at the condition.
The statements will be executed until the condition becomes true.
}
while(condition_statement);
sohail.shaghasi@gmail.com
Comparing while and do-while loops
sohail.shaghasi@gmail.com
For loop
sohail.shaghasi@gmail.com
Break
Break is used in the loops and when executed, the control of the execution will come out of the
loop.
E.g:
for(int i=1;i<50;i++)
{
if(i%11==0)
{
System.out.println(Before breaking the loop);
break;
}
System.out.println(Value of i:+i );
}
sohail.shaghasi@gmail.com
Continue
Continue makes the loop to skip the current execution and continues with the next iteration.
E.g:
for(int i=1;i<50;i++)
{
if(i%11==0)
{
System.out.println(Before breaking the loop);
continue;
}
System.out.println(Value of i:+i );
}
sohail.shaghasi@gmail.com
What is function/Method?
sohail.shaghasi@gmail.com
Function Cont.
parameters
Function
Access Return
name
Modifier type
Method body
return
sohail.shaghasi@gmail.com
What is Class?
sohail.shaghasi@gmail.com
What is Constructor?
Constructor is a method where you place all the initialization, it has the same
name as the class.
When you create an object, you actually invoke the class constructor.
new Operator
Allocated a memory for that object and returns a reference of that memory
location to you.
Dot operator
It allows you to access public properties and public methods of a respective
class.
sohail.shaghasi@gmail.com
Constructor cont.
sohail.shaghasi@gmail.com
What is Object?
Or also equivalent to
String str = Hello World;
sohail.shaghasi@gmail.com
Object cont.
sohail.shaghasi@gmail.com Object
What are Access Modifiers?
Access within class within package outside outside
Modifier package by package
subclass only
Private Yes No No No
sohail.shaghasi@gmail.com
What is OOP?
object-oriented programming:
A programming paradigm where a software system is represented as a
collection of objects that interact with each other to solve the overall task.
sohail.shaghasi@gmail.com
Four Pillars of OOP
APIE
sohail.shaghasi@gmail.com
Abstraction
In java, we can have an abstract class without any abstract method. This
allow us to create classes that cannot be instantiated, but can only be
inherited.
sohail.shaghasi@gmail.com
Polymorphism
sohail.shaghasi@gmail.com
Inheritance
sohail.shaghasi@gmail.com
Encapsulation
sohail.shaghasi@gmail.com