INTRODUCTION TO OOP USING
JAVA
Tanjina Helaly
WHAT IS PROGRAMMING
Instruction to computer/device to perform task.
Computer understands only 0 and 1. Nothing
else.
So, we need to send the instruction in the form of
0, 1
Do you write program with just 0 and 1?
CLASSIFICATION/EVOLUTION OF
PROGRAMMING
Machine level programming
Send instruction in binary format
Assembly Programming
send code instead of binary code.
Need assembler to convert to binary
High level programming
Code is close to English Language
Need Compiler to convert to binary
3 types
Non structured
Structured/Procedural
Object Oriented Programming
CLASSIFICATION/EVOLUTION OF
PROGRAMMING
Non structured
Generate spaghetti code
Sequential and has GoTo
COBOL, BASIC, FORTRAN
Structured/Procedural
Use Subroutine/Function
improving the clarity, quality, and development time
C, PASCAL
Object Oriented Programming
Object-oriented programming (OOP) is a programming
language model organized around objects rather than
"actions" and data rather than logic.
Historically, a program has been viewed as a logical
procedure that takes input data, processes it, and produces
output data.
Java, C++, C#
OUR GOAL
LEARN OBJECT ORIENTED PROGRAMMING
USING JAVA
PROGRAMMING LANGUAGE
A programming language is a formal constructed
language designed to communicate instructions
to a machine, particularly a computer.
JAVA’S LINEAGE
Java is related to C++, which is a direct
descendent of C.
Much of the character of Java is inherited from these
two languages.
From C, Java derives its syntax.
Many of Java’s object-oriented features were
influenced by C++.
JAVA - CHARACTERISTICS
Uses C/C++ basic syntax and basic data types -int, char,
float, double, long, short, byte etc.
Uses standard C/C++ control structures
“Pure” OO language
No stand alone functions -All code is part of a class
No explicit pointers - uses references
Uses garbage collection
Java is strongly typed
Java is normally compiled to a bytecode.
Java bytecode is a machine language for an abstract
machine
Makes Java secure and Portable
Each platform (or browser) that runs Java has a Java
Virtual Machine (JVM) . The JVM executes Java bytecodes
JAVA – THE PLATFORM
Java has a large API (application programming
interface) covering a wide range of areas The
following list of Java APIs and applications from
Sun show the range of applications of Java .
For reference http://java.sun.com/products/index.html
Java Foundation Classes (JFC) – GUI
JDBC Database Access
Java Web Server
EmbeddedJava - Java on embedded devices
WHY JAVA
Platform Independent - Code once run anywhere
Byte code
Easy to learn
Secure
Byte code & VM
Free
JAVA IDE
Using JDK you can compile and run java
program from command line.
c:> javac HelloWorld. Java
compiling here and
it will produce HelloWorld.class i.e. bytecode.
c:>java HelloWorld
It runs java byte code on native machine
JAVA IDE
Creating, Compiling, Debugging and Execution
for these four steps JDK is not user friendly. IDE
is provided for that. A list of IDEs are:
Eclipse
Netbeans.
IntelliJ IDEA
AN EXAMPLE HELLOWORLD
public class HelloWorldExample
{
public static void main( String args[] )
{
System.out.println("Hello World");
}
}
JAVA SOURCE CODE NAMING
CONVENTIONS
All java source file should end with .java
Each .java file can contain only one public
class
The name of the file should be the name of
the public class plus ".java"
Do not use abbreviations in the name of the class
If the class name contains multiple words then
capitalize the first letter of each word ex.
HelloWorld.java
NAMING CONVENTION
Class Naming
Uses Capitalized word(s) i.e. Title case
Examples:- HelloWorld, MyList, StudentMark
Variable and method names
starts with a lowercase letter and after that use Title
case
Examples:- variableAndMethodNames, aFloat,
studentName
Names of constants
All are capital letters and separated by underscore.
Example: NAMES_OF_CONSTANTS
JAVA IDENTIFIERS RULES
Identifier is a name given to a variable, class, or
method.
Java identifier
Can contain letter, number, underscore (_), or dollar
sign ($).
Cannot start with number.
Identifiers are case sensitive
have no maximum length.
cannot be a keyword, but it can contain a keyword as
part of its name.
JAVA BASICS
Tanjina Helaly
TOOLS/SET-UP
STEP1: INSTALL JAVA AND PATH SET-UP
Need to install Java(JDK and
JRE). Get the latest version from
Java Standard Edition(SE) from
http://www.oracle.com/technetwork
/java/javase/downloads/index.html
After installing Java you need
to set-up the “Path”
environment variable which is
available from My Computer
under Advanced Properties
tab.
Note: Do not delete anything in
“Path” variable. Just add your
path “C:\Program
Files\Java\jdk1.8.0_31\bin;”
(Depending on your version the
path will change) at the
beginning of the existing value.
STEP 2: INSTALL IDE
Need an IDE: Eclipse or NetBeans or IntelliJ IDEA.
Or
A Text Editor e.g. TextPad
You can install
eclipse from :
http://www.eclipse.org/downloads/packages/eclipse-ide-
java-developers/mars1
NetBeans:
http://www.oracle.com/technetwork/java/javase/downloads/i
ndex.html
IntelliJ IDEA:
https://www.jetbrains.com/idea/download/#section=window
s
COMPILE & RUN JAVA APPLICATION
WITHOUT IDE
Using JDK you can compile and run java
program from command line.
c:> javac HelloWorld. Java
compiling here and
it will produce HelloWorld.class i.e. bytecode.
c:>java HelloWorld
It runs java byte code on native machine
WITH JAVA IDE
Creating, Compiling, Debugging and Execution
for these four steps JDK is not user friendly. IDE
is provided for that. A list of IDEs are:
Eclipse
Netbeans.
IntelliJ IDEA
DATA TYPES
Divided into two broad categories:
primitive types
class/reference types.
Primitive data : eight types
Logical: boolean (true or false)
doesn’t hold integer (unlike C)
Textual: char (16 bits)
use the Unicode(International: 0-255) not ASCII(1 byte: 0-127)
Integral: byte (8 bits), short (16 bits), int (32 bits), and long (64
bits)
Floating point: float (32 bits) and double (64 bits)
Class or reference data: two types
Textual: String
All classes that declare by yourself
CASTING
Converting from one data type to another.
e.g. assigning an int value to a long variable
Example
public class TestCast {
public static void main(String[] args) {
byte b= 5;
int a = b; // OK. Auto Casting
byte c = a; // Compiler error. Need Casting
c = (byte)a; // Casting
float f = 1.2f;
a= f; // Compiler error. Need Cast
a = (int)f; // Explicit Cast
f = a;
}
}
OPERATOR
Assignment =
Arithmetic + - * / %
Equality == !=
Relational < <= > >=
Logical &&, ||
increment/decrement ++ --
Shift << >>
ARRAYS
An array is a collection of data items, all of the
same type, accessed using a common name.
The data type can be either a primitive data
type or a reference type.
Major differences with C/C++ arrays:
Java arrays are references
Java arrays know their size (length property)
Java multidimensional arrays need not be
rectangular
Java array elements are initialized
ARRAY DECLARATION & INITIALIZATION
Declaration
int[] sampleArray;
sampleArray = new int[10];
Or
int[] sampleArray = new int[10];
Initialization
During declaration
int[] sampleArray = {1,2,3,4,5};
After declaration
int[] sampleArray;
sampleArray = new int[]{1,2,3,4,5};
sampleArray = {1,2,3,4,5}; // compiler error
ARRAY SIZE & ACCESSING A SPECIFIC
INDEX
Getting size of array
int[] sampleArray = new int[10];
int size = sampleArray.length; //this will return the size of the
array, here 10
Accessing a specific item
Assigning a value
sampleArray[0] = 5;
sampleArray[1] = 2;
sampleArray[2] = 3;
Getting/Reading a value
int value = sampleArray[2];
ARRAYS – EXAMPLE CODE
public class ArrayExample
{
public static void main( String args[] )
{
// space to store Reference is allocated, no array space allocated
double[] sampleArray;
//allocate array locations on heap
sampleArray = new double[ 10 ];
// Indexing starts at 0 like C/C++
sampleArray[ 0 ] = 5.5;
// Reference refers to new array.
// Old array available for garbage collection
sampleArray = new double[ 2 ];
}
}
MULTI-DIMENSIONAL ARRAY
multidimensional arrays are actually arrays of
arrays.
int twoD[][] = new int[4][5];
Do not need to be rectangular
During creation it’s required to specify the size
for the first/leftmost dimension. You can allocate
the remaining dimensions separately.
int twoD[][] = new int[4][];
MULTI-DIMENSIONAL ARRAY
Rectangular Irregular Array
Declarion & int twoD[][] = new int[4][5]; int twoD[][] = new int[4][];
Array or twoD[0] = new int[1];
Creation int twoD[][] = new int[4][]; twoD[1] = new int[2];
twoD[0] = new int[5]; twoD[2] = new int[3];
twoD[1] = new int[5]; twoD[3] = new int[4];
twoD[2] = new int[5];
twoD[3] = new int[5];
Example of 01234 0
Array 56789 12
11134 345
56789 6789
CONTROL STATEMENT
if –else
switch
Loop
for
while
do-while
CONTROL STATEMENT
“Enhance for” or “for-each”
automatically cycles through an array in sequence
from the lowest index to the highest.
Syntax : for(type itr-var : collection) statement-block
Example:
int nums[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int sum = 0;
for(int x: nums)
sum += x;
Advantage: Avoid boundary error
JUMP STATEMENT
break
Exits out of a loop or switch statement
Unlabeled break exits out of the innermost loop or
switch
Use labeled break to exit out of nested loops or switch
or block.
JUMP STATEMENT
public class BreakExample { Output:
public static void main( String args[] ) { Outer loop: 0
for ( int row = 0; row < 5; row++ ) { 0 Break
System.out.println("Outer loop: " + Outer loop: 1
row); 0 1 Break
for ( int column = 0; column < 4 ; Outer loop: 2
column++ ) {
0 Break
System.out.print(column +" " );
Outer loop: 3
if ( ((row + column) % 2 ) == 0 ) {
0 1 Break
System.out.println("Break " );
Outer loop: 4
break;
} 0 Break
}
}
}
}
JUMP STATEMENT – LABELED JUMP
public class BreakExample {
public static void main( String args[] ) {
Outer:
for ( int row = 0; row < 5; row++ ) {
System.out.println("Outer loop: " + row); Output:
for ( int column = 0; column < 4; Outer loop: 0
column++ ) { 0 Break
System.out.println(column + "\t");
if ( ((row + column) % 2 ) == 0 )
{
System.out.println("Break " );
break Outer;
}
}
}
}
}
JUMP STATEMENT
continue
A continue statement skips to the end of the current
loop's body.
The loop's boolean expression is then evaluated.
Code Output
public class TestContinue { 01
public static void main(String args[]) { 23
for(int i=0; i<10; i++) { 45
System.out.print(i + " "); 67
if (i%2 == 0) continue; 89
System.out.println("");
}
}
}
REFERENCE
Java:Complete Reference Chapter 1-5