OOP Lecture 02
OOP Lecture 02
Programming-Java
Basics
1
Application Programming Interface(API) or
Java Class Libraries(JCL).
Java programs consist of pieces called classes.
of related classes
Classes include pieces called methods that perform
tasks
Java contain a collection of ready made clasess called
vendors
You can either use ready made classes or create your
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
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");
}
}
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
}
}
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.
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
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)
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
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
25
Relational and Conditional
Operators
Compares two values and determines the
relationship between them
! ! 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
29
Shift and Logical Operators
Bitwise functions of number operands
~ ~ 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
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
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 :-
37