0% found this document useful (0 votes)
16 views

OOP Lecture 02

Uploaded by

joashtekeleza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

OOP Lecture 02

Uploaded by

joashtekeleza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 37

Object Oriented

Programming-Java

Basics

1
Application Programming Interface(API) or
Java Class Libraries(JCL).
 Java programs consist of pieces called classes.

 These classes are grouped into packages—collections

of related classes
 Classes include pieces called methods that perform

tasks
 Java contain a collection of ready made clasess called

Java class libraries, which are also known as the Java


APIs (Application Programming Interfaces).
 Class libraries are embedded in compilers by compiler

vendors
 You can either use ready made classes or create your

own classes (user defined classes) 2


Five Phases of Java Program
 Edit
 Creasing a program
 Writing source code using

 Editors e.g. notepad etc are used, some

editors are integrated in IDEs.


 Written program is saved in secondary

storage in a file with .java extension

3
Five Phases of Java Program
 Compile
 uses the command javac to compile a program in
command window for windows, mac, and linux.
 Java Compiler is used
 Java compiler translates Java source code into
bytecodes
 Stores them on disk in a file ending with .class
 Bytecodes are executed by the JVM—a part of the
JDK
 JVM abstracts OS and Hardware so that program
only interacts with JVM
 The JVM is invoked by the java command when you
run a program. 4
Five Phases of Java Program
 Load
 program is placed in primary memory(RAM)
before it can execute
 Loader is used for program loading
 loader takes the .class files containing the
program’s bytecodes and transfers them to
primary memory(RAM).
 The class loader also loads any of the .class
files provided by API or JCL that your program
uses.
 The .class files can be loaded from a disk on

your system or over a network/Internet. 5


Five Phases of Java Program
 Verify
 as the classes are loaded, the bytecodes are
examined to ensure that they are valid and do
not violate Java’s security restrictions.

 bytecode Verifier is used for Java code


verification

 java enforces strong security, to make sure


that Java programs arriving over the network
do not damage your files or your system.
6
Five Phases of Java Program
 Execute
 the JVM executes the program’s bytecodes,
thus performing the actions specified by the
program.
 translates the bytecodes into the underlying

computer’s machine language.

7
General Structure of a Java
program
//there must be at least one class declaration
public class Sample
{
public static void main(String args[])
{
System.out.print("Hall Java");
}
}

( ) parentheses indicate method


{ } braces forming blocks to delimit the body of declaration
“ ” double quotation
‘ ’ quotation
; semicolon to terminate statement 8

Statements-perform method’s task


General Structure of a Java
program
 Class: keyword is used to declare a class
 Public: keyword is an access modifier which represents
visibility.
 Static: is a keyword, class method or static method. There is
no need to create object to invoke the static method. The main
method is executed by the JVM, so it doesn't require to create
object to invoke the main method. So it saves memory.
 Void: is the return type of the method, it means it doesn't return
any value.
 Main: represents startup of the program.
 String[] args: is used for command line argument. We will
learn it later.
 System.out: output object which allow output of characters to 9

command-line.
Outputting to the Console cont…
 print: does not place output cursor at the beginning of a new
line
e.g
public class Sample
{
public static void main(String args[])
{
System.out.print("Hall Programing");
System.out.print("Hall Java");
}
}
10
Outputting to the Console cont…
 println: place output cursor at the beginning of a new line
e.g
public class Sample
{
public static void main(String args[])
{
System.out.println("Hall Programing");
System.out.println("Hall Java");
}
}

11
Outputting to the Console count.
 printf: formats output
 E.g

public class Sample


{
public static void main(String args[])
{
System.out.printf("%s", "Hall World");
System.out.printf( "%s\n%s\n", "Welcome to", "Java Programming!" );

}
}

12
Outputting to the Console count.
 Each format specifier is a placeholder for a value and specifies
the type of data to output.
 Format specifiers also may include optional formatting
information.

 Format specifiers example:


%s: placeholder for string
%d: placeholder for integers
%f: placeholder for doubles
%c: placeholder for character

13
Inputint from the Console.
 Scanner
import java.util.Scanner;
public class Addition{
public static void main(String args[]){
Scanner input = new Scanner(System.in);
int number1;
int number2;
int sum;
System.out.print("Enter first integer");
number1 = input.nextInt();
System.out.print("Enter second integer");
number2 = input.nextInt();
sum = number1 + number2;
System.out.printf("sum is %d\n", sum); }}
Expression: Portions of statements that contain calculation. 14

System.in: standard input object reads of information typed by the user.


Escape Sequence
 \: backslash is called escape character.
 \n: Newline. Position the screen cursor at the
beginning of the next line.
 \t: Horizontal tab. Move the screen cursor to the
next tab stop.
 \r: Carriage return. Position the screen cursor at the
beginning of the current line—do not advance to the
next line. Any characters output after the carriage
return overwrite the characters previously output on
that line.
 \\: Backslash. Used to print a backslash character.
 \“: Double quote. Used to print a double-quote
character. For example displays 15
Comments
 Comments make the code Readable
 Give Comments wherever necessary

 Eg.
 // This is a single line of comments
 /* This is a block
of comments */

16
Data Types

17
Primitive Data Types
Keyword Description Size(Range)

int Integer 32 bits()

byte Byte-Length Integer 8 bits()

short Short Integer 16 bits()

long Long Integer 64 bits()

float Single-Precision Floating Point 32 bits()

double Double-Precision Floating Point 64 bits()

char A Single Character 16 bits()

18
boolean A Boolean Value (true or false) 1 bit()
Type Conversion & Casting
A variable can be assigned a value that is different
to its data type
 In cases where precision is not lost, conversion is
automatically handled
 Eg. int x = 5;
long y = x;
 In cases where precision is lost Type Casting has
to be done
 Eg. int x = 5;
short y = (short) x;
19
Variables (Fields)
 A variable is a location in the computer’s memory where a
value can be stored for use later in a program
 Have a name(any valid identifier): Locate variable.
 Have a type: specifies the kind of data stored at that
memory’s location.

 Defining a variable.
AccessSpecifier DataType VariableName = Value;
private int x = 5;
char c = ‘x’;
boolean b; 20

final int i = 8;
Variables (Fields)
 Types of Variables
 Local Variable: A variable that is declared
inside the method is called local variable.
 Instance Variable: A variable that is declared

inside the class but outside the method is


called instance variable . It is not declared as
static.
 Static variable: A variable that is declared as

static is called static variable. It cannot be


local.

21
Variables (Fields)
 e.g

public class A
{
int data=50;//instance variable
static int m=100;//static variable

void method()
{
int n=90;//local variable
}
22

}//end of class
Operators
 An Operator performs a function on one, two
or three operands
 Categories of Operators in Java :-
 Arithmetic Operators
 Relational and Conditional Operators
 Shift and Logical Operators
 Assignment Operators
 Special Operators

23
Binary Arithmetic Operators
 Java supports various arithmetic operators for all
integer and floating point operators
Operator Use Description
+ op1 + op2 Adds op1 and op2

- op1 - op2 Subtracts op2 from op1


* op1 * op2 Multiplies op1 by op2
/ op1 / op2 Divides op1 by op2
% op1 % op2 Computes the remainder
of dividing op1 by op2 24
Unary Arithmetic Operators
 Shortcut increment decrement operators

Operator Use Description


++ op++ Increments op by 1; evaluates to the value
of op before it was incremented
++ ++op Increments op by 1; evaluates to the value
of op after it was incremented
-- op-- Decrements op by 1; evaluates to the value
of op before it was decremented
-- --op Decrements op by 1; evaluates to the value
of op after it was decremented

25
Relational and Conditional
Operators
 Compares two values and determines the
relationship between them

Operator Use Returns true if


> op1 > op2 op1 is greater than op2

>= op1 >= op2 op1 is greater than or equal to op2


< op1 < op2 op1 is less than op2

<= op1 <= op2 op1 is less than or equal to op2


== op1 == op2 op1 and op2 are equal
!= op1 != op2 op1 and op2 are not equal 26
Relational and Conditional
Operators
 Used to construct decision making expressions
Operator Use Returns true if
&& op1 && op2 op1 and op2 are both true, conditionally
evaluates op2

|| op1 || op2 either op1 or op2 is true, conditionally


evaluates op2

! ! op op is false

27
Shift and Logical Operators
 Shift Operators perform bit manipulation on data by
shifting the bits of its first operand right or left

Operator Use Description


>> op1 >> op2 shift bits of op1 right by
distance op2
<< op1 << op2 shift bits of op1 left by
distance op2
>>> op1>>>op2 shift bits of op1 right by
distance op2 (unsigned)
28
Shift and Logical Operators
 e.g. :- 13 >> 1
Shifting 1101 (13) to right by one position
results in 110 (6)

29
Shift and Logical Operators
 Bitwise functions of number operands

Operator Use Description


& op1 & op2 bitwise and

| op1 | op2 bitwise or

^ op1 ^ op2 bitwise xor

~ ~ op bitwise complement

30
Shift and Logical Operators
 When its operands are numbers, the & operation
performs the bitwise AND function on each parallel
pair of bits in each operand

op1 op2 Result


0 0 0
0 1 0
1 0 0
1 1 1

31
Shift and Logical Operators
 e.g. :- 13 & 12
1101 //13
& 1100 //12
-------
1100 //12

32
Assignment Operators
 Basic assignment operator (=) is used to assign
one value to another
 Shortcut Assignment Operators
Operator Use Equivalent to
+= op1 += op2 op1 = op1 + op2
-= op1 -= op2 op1 = op1 - op2
*= op1 *= op2 op1 = op1 * op2
/= op1 /= op2 op1 = op1 / op2
%= op1 %= op2 op1 = op1 % op2
&= op1 &= op2 op1 = op1 & op2 33
Special Operators
Operator Description
?: Shortcut if-else statement

[] Used to declare arrays, create arrays, and


access array elements
. Used to form qualified names

( params ) Delimits a comma-separated list of


parameters
( type ) Casts (converts) a value to the specified type

new Creates a new object or a new array

instanceof Determines whether its first operand is an


instance of its second operand
34
Special Operators
 op1 ? op2 : op3
returns op2 if op1 is true or returns op3 if op1
is false
 [ ] operator

e.g. :-
float[] arrayOfFloats = new float[10];
arrayOfFloats[6] accesses the 7th element of
the array
35
Special Operators
 Dot (.) Operator :-
Accesses instance members of an
object or class members of a class
 The ( ) operator :-
Used to list arguments when calling a method
 The new operator :-

Used to create new objects


e.g. :- Integer anInteger = new Integer(10);
36
Special Operators
 The instanceof Operator :-
Tests whether first operand is an instance of
the second
op1 instanceof op2
op1 must be the name of an object and op2
must be the name of a class

37

You might also like