OOPSJAVA 1 4 Notes OKD
OOPSJAVA 1 4 Notes OKD
JVM
JVM (Java Virtual Machine) is an abstract machine. It is called a virtual machine
because it doesn't physically exist. It is a specification that provides a runtime
environment in which Java bytecode can be executed. It can also run those
programs which are written in other languages and compiled to Java bytecode.
JVMs are available for many hardware and software platforms. JVM, JRE, and JDK
are platform dependent because the configuration of each OS is different from
each other. However, Java is platform independent. There are three notions of the
JVM specification, implementation, and instance.
1. A specification where working of Java Virtual Machine is specified. But
implementation provider is independent to choose the algorithm. Its
implementation has been provided by Oracle and other companies.
2. An implementation Its implementation is known as JRE (Java Runtime
Environment).
3. Runtime Instance Whenever you write java command on the command
prompt to run the java class, an instance of JVM is created
The JVM performs following operation:
• Loads code
• Verifies code
• Executes code
1
JVM provides definitions for the:
• Memory area
• Register set
• Garbage-collected heap
JVM Architecture
Let's understand the internal architecture of JVM. It contains classloader, memory
area, execution engine etc.
2
1) Classloader
Classloader is a subsystem of JVM which is used to load class files. Whenever we
run the java program, it is loaded first by the classloader. There are three built-in
classloaders in Java.
These are the internal classloaders provided by Java. If you want to create your own
classloader, you need to extend the ClassLoader class.
2) Class(Method) Area
Class(Method) Area stores per-class structures such as the runtime constant pool,
field and method data, the code for methods.
3) Heap
It is the runtime data area in which objects are allocated.
4) Stack
Java Stack stores frames. It holds local variables and partial results, and plays a
part in method invocation and return.
Each thread has a private JVM stack, created at the same time as thread.
A new frame is created each time a method is invoked. A frame is destroyed when
its method invocation completes.
3
5) Program Counter Register
PC (program counter) register contains the address of the Java virtual machine
instruction currently being executed.
7) Execution Engine
It contains:
1. A virtual processor
JRE
JRE is an acronym for Java Runtime Environment. It is also written as Java RTE. The
Java Runtime Environment is a set of software tools which are used for developing
Java applications. It is used to provide the runtime environment. It is the
implementation of JVM. It physically exists. It contains a set of libraries + other
files that JVM uses at runtime.
The implementation of JVM is also actively released by other companies besides
Sun Micro Systems.
4
JDK
JDK is an acronym for Java Development Kit. The Java Development Kit (JDK) is a
software development environment which is used to develop Java applications
and applets. It physically exists. It contains JRE + development tools.
JDK is an implementation of any one of the below given Java Platforms released by
Oracle Corporation:
5
Components of JDK
Following is a list of primary components of JDK:
This tool is used to run and debug Java applets without a web
appletviewer:
browser.
6
It specifies the Java compiler, which converts source code
javac:
into Java bytecode.
javafxpackage
It is a tool to package and sign JavaFX applications.
r:
7
jstat: Java Virtual Machine statistics monitoring tool (experimental).
It is the part of the Java API for XML Binding (JAXB) API. It
xjc:
accepts an XML schema and generates Java classes.
8
Java Lecture Notes-2
9
Data Type Default Value Default size
boolean false 1 bit
byte 0 1 byte
short 0 2 byte
int 0 4 byte
long 0L 8 byte
10
Short Data Type
The short data type is a 16-bit signed two's complement integer. Its value-range
lies between -32,768 to 32,767 (inclusive). Its minimum value is -32,768 and
maximum value is 32,767. Its default value is 0.
The short data type can also be used to save memory just like byte data type. A
short data type is 2 times smaller than an integer.
Example:
Example:
long a=100000L, long b=-200000L
11
Float Data Type
The float data type is a single-precision 32-bit IEEE 754 floating point.Its value
range is unlimited. It is recommended to use a float (instead of double) if you
need to save memory in large arrays of floating point numbers. The float data
type should never be used for precise values, such as currency. Its default value is
0.0F.
Example:
float f1 = 234.5f
double d1=12.3
12
Operators in Java
Operator in Java is a symbol that is used to perform operations. For example: +,
-, *, / etc.
There are many types of operators in Java which are given below:
• Unary Operator,
• Arithmetic Operator,
• Shift Operator,
• Relational Operator,
• Bitwise Operator,
• Logical Operator,
• Assignment Operator.
multiplicative */%
Arithmetic
additive +-
bitwise exclusive
^
Bitwise OR
bitwise inclusive
|
OR
logical AND &&
Logical
logical OR ||
Ternary ternary ?:
The Java unary operators require only one operand. Unary operators are used to
perform various operations i.e.:
10
12
12
10
22
21
-11
false
true
15
50
21
2
5
2
5
5
-5
1073741819
false
false
false
10
false
11
The logical || operator doesn't check the second condition if the first condition is
true. It checks the second condition only if the first one is false.
The bitwise | operator always checks both conditions whether first condition is
true or false.
true
true
true
10
true
11
2
Another Example:
14
16
Output:
13
9
18
9
20
Java Keywords
Java keywords are also known as reserved words. Keywords are particular words
that act as a key to a code. These are predefined words by Java so they cannot be
used as a variable or object name or class name.
3.Break: Java break keyword is used to break the loop or switch statement.
It breaks the current flow of the program at specified conditions.
4.Byte: Java byte keyword is used to declare a variable that can hold 8-bit
data values.
5.case:Java case keyword is used with the switch statements to mark blocks
of text.
18.float:Java float keyword is used to declare a variable that can hold a 32-
bit floating-point number.
19.for:Java for keyword is used to start a for loop. It is used to execute a set
of instructions/functions repeatedly when some condition becomes true. If
the number of iteration is fixed, it is recommended to use for loop.
24.int:Java int keyword is used to declare a variable that can hold a 32-bit
signed integer.
26.long:Java long keyword is used to declare a variable that can hold a 64-
bit integer.
35.short:Java short keyword is used to declare a variable that can hold a 16-
bit integer.
45.try:Java try keyword is used to start a block of code that will be tested for
exceptions. The try block must be followed by either catch or finally block.
46.void:Java void keyword is used to specify that a method does not have a
return value.
48.while:Java while keyword is used to start a while loop. This loop iterates a
part of the program several times. If the number of iteration is not fixed, it is
recommended to use the while loop.
Java Lecture Notes-3
Java Control Statements (Control Flow in Java)
Java compiler executes the code from top to bottom. The statements in the code
are executed according to the order in which they appear. However, Java provides
statements that can be used to control the flow of Java code. Such statements are
called control flow statements. It is one of the fundamental features of Java, which
provides a smooth flow of program.
Java provides three types of control flow statements.
◦ if statements
◦ switch statement
• Loop statements
◦ do while loop
◦ while loop
◦ for loop
◦ for-each loop
• Jump statements
◦ break statement
◦ continue statement
Decision-Making statements:
As the name suggests, decision-making statements decide which statement to
execute and when. Decision-making statements evaluate the Boolean expression
and control the program flow depending upon the result of the condition provided.
There are two types of decision-making statements in Java, i.e., If statement and
switch statement.
1) If Statement:
In Java, the "if" statement is used to evaluate a condition. The control of the
program is diverted depending upon the specific condition. The condition of the If
statement gives a Boolean value, either true or false. In Java, there are four types
of if-statements given below.
Simple if statement
1. if-else statement
2. if-else-if ladder
3. Nested if-statement
Let's understand the if-statements one by one.
1. Simple if statement:
It is the most basic statement among all control flow statements in Java. It
evaluates a Boolean expression and enables the program to enter a block of code
if the expression evaluates to true.
Syntax of if statement is given below.
if(condition) {
statement 1; //executes when condition is true
}
Consider the following example in which we have used the if statement in the java
code.
Student.java
public class Student {
public static void main(String[] args) {
int x = 10;
int y = 12;
if(x+y > 20) {
System.out.println("x + y is greater than 20");
}
}
}
Output:
x + y is greater than 20
2. if-else statement
The if-else statement is an extension to the if-statement, which uses another block
of code, i.e., else block. The else block is executed if the condition of the if-block is
evaluated as false.
Syntax:
if(condition) {
statement 1; //executes when condition is true
}
else{
statement 2; //executes when condition is false
}
Consider the following example.
Student.java
x + y is greater than 20
3. if-else-if ladder:
The if-else-if statement contains the if-statement followed by multiple else-if
statements. In other words, we can say that it is the chain of if-else statements
that create a decision tree where the program may enter in the block of code
where the condition is true. We can also define an else statement at the end of
the chain.
Syntax of if-else-if statement is given below.
if(condition 1) {
statement 1; //executes when condition 1 is true
}
else if(condition 2) {
statement 2; //executes when condition 2 is true
}
else {
statement 2; //executes when all the conditions are false
}
Consider the following example.
Student.java
public class Student {
public static void main(String[] args) {
String city = "Delhi";
if(city == "Meerut") {
System.out.println("city is meerut");
}
else if (city == "Noida") {
System.out.println("city is noida");
}
else if(city == "Agra") {
System.out.println("city is agra");
}
else {
System.out.println(city);
}
}
}
Output:
Delhi
4. Nested if-statement
if(condition 1) {
statement 1; //executes when condition 1 is true
if(condition 2) {
statement 2; //executes when condition 2 is true
}
else{
statement 2; //executes when condition 2 is false
}
}
Consider the following example.
Student.java
if(address.endsWith("India")) {
if(address.contains("Meerut")) {
System.out.println("Your city is Meerut");
}
else if(address.contains("Noida")) {
System.out.println("Your city is Noida");
}else {
System.out.println(address.split(",")[0]);
}
}else {
System.out.println("You are not living in India");
}
}
}
Output:
Delhi
Switch Statement:
In Java, Switch statements are similar to if-else-if statements. The switch
statement contains multiple blocks of code called cases and a single case is
executed based on the variable which is being switched. The switch statement is
easier to use instead of if-else-if statements. It also enhances the readability of
the program.
Points to be noted about switch statement:
• The case variables can be int, short, byte, char, or enumeration. String type
is also supported since version 7 of Java
• Cases cannot be duplicate
• Default statement is executed when any of the case doesn't match the
value of expression. It is optional.
• Break statement terminates the switch block when the condition is satisfied.
It is optional, if not used, next case is executed.
• While using switch statements, we must notice that the case expression will
be of the same type as the variable. However, it will also be a constant
value.
The syntax to use the switch statement is given below.
switch (expression){
case value1:
statement1;
break;
.
.
.
case valueN:
statementN;
break;
default:
default statement;
}
Consider the following example to understand the flow of the switch statement.
Student.java
public class Student implements Cloneable {
public static void main(String[] args) {
int num = 2;
switch (num){
case 0:
System.out.println("number is 0");
break;
case 1:
System.out.println("number is 1");
break;
default:
System.out.println(num);
}
}
}
Output:
2
While using switch statements, we must notice that the case expression will be of
the same type as the variable. However, it will also be a constant value. The
switch permits only int, string, and Enum type variables to be used.
Loop Statements
In programming, sometimes we need to execute the block of code repeatedly
while some condition evaluates to true. However, loop statements are used to
execute the set of instructions in a repeated order. The execution of the set of
instructions depends upon a particular condition.
In Java, we have three types of loops that execute similarly. However, there are
differences in their syntax and condition checking time.
1.for loop
2.while loop
3.do-while loop
Let's understand the loop statements one by one.
Java for loop
In Java, for loop is similar to C and C++. It enables us to initialize the loop
variable, check the condition, and increment/decrement in a single line of code.
We use the for loop only when we exactly know the number of times, we want to
execute the block of code.
Consider the following example to understand the proper functioning of the for
loop in java.
Calculation.java
Consider the following example to understand the functioning of the for-each loop
in Java.
Calculation.java
while(condition){
//looping statements
}
The flow chart for the while loop is given in the following image.
0
2
4
6
8
10
do
{
//statements
} while (condition);
The flow chart of the do-while loop is given in the following image.
Consider the following example to understand the functioning of the do-while loop
in Java.
Calculation.java
Jump Statements
Jump statements are used to transfer the control of the program to the specific
statements. In other words, jump statements transfer the execution control to the
other part of the program. There are two types of jump statements in Java, i.e.,
break and continue.
0
1
2
3
4
5
6
break statement example with labeled for loop
Calculation.java
0
1
2
3
4
5
Java continue statement
Unlike break statement, the continue statement doesn't break the loop, whereas,
it skips the specific part of the loop and jumps to the next iteration of the loop
immediately.
Consider the following example to understand the functioning of the continue
statement in Java.
0
1
2
3
5
1
2
3
5
2
3
5