Professional Documents
Culture Documents
Unit 1
Unit 1
Unit 1
JAVA
Ranajit Senko
Assistant Professor
Department of CSE
GITAM Institute of Technology (GIT)
Visakhapatnam – 530045
1
Topics to be Covered
2
ABOUT JAVA LANGUAGE:
Java is a High Level, Object Oriented Programming language developed by “SUN MICRO SYSTEMS” in 1995.
Java is designed for the development of Software for the Consumer Electronic Devices. The main features of
JAVA Language is that it is a Platform Independent Language. Java is called “The Internet Version of C”. The
In 1994, a team of Java developers wrote Web Runner, a clone of the internet browser Mosaic. It was based
on the Java programming language. The name ‘Web Runner’ was a tribute to the Blade Runner movie. It was
renamed Hot Java and was officially announced in May the same year at the Sun World conference.
3
Advantages of Java Programming Language:
4
Advantages of Java Programming Language:
Architecture-neutral − Java compiler generates an architecture-neutral object file format, which makes the
compiled code executable on many processors, with the presence of Java runtime system.
Portable − Being architecture-neutral and having no implementation dependent aspects of the specification
makes Java portable. Compiler in Java is written in ANSI C with a clean portability boundary, which is a POSIX
subset.
Robust − Java makes an effort to eliminate error prone situations by emphasizing mainly on compile time error
checking and runtime checking.
Multithreaded − With Java's multithreaded feature it is possible to write programs that can perform many
tasks simultaneously. This design feature allows the developers to construct interactive applications that can
run smoothly.
5
Advantages of Java Programming Language:
High Performance − With the use of Just-In-Time compilers, Java enables high performance.
Dynamic − Java is considered to be more dynamic than C or C++ since it is designed to adapt to an
evolving environment. Java programs can carry extensive amount of run-time information that can be
used to verify and resolve accesses to objects on run-time.
Interpreted − Java byte code is translated on the fly to native machine instructions and is not stored
anywhere. The development process is more rapid and analytical since the linking is an incremental
and light-weight process.
6
Java Development Kit – JDK
The Java Development Kit (JDK) is a software development environment used for developing Java applications
and applets. It includes the Java Runtime Environment (JRE), an interpreter/loader (java), a compiler (javac), an
archiver (jar), a documentation generator (javadoc) and other tools needed in Java development.
The Java Development Kit (JDK) is one of three core technology packages used in Java programming, along with
the JVM (Java Virtual Machine)
7
Java Virtual Machine
By All Language compilers translate source code into machine code for a
specific computer.
Java compiler also does the same. The Java compiler produces an intermediate
code also known as “Byte Code” for a machine. The machine is called “Java
Virtual Machine (JVM). This exists only inside the computer memory. Byte code
is a form of instruction set designed for efficient execution by a software
interpreter.
8
Unlike human-readable source code, byte codes (normally numeric addresses)
encode the result of compiler parsing and performing semantic analysis of
things like type, scope, and nesting depths of program objects.
Whenever you write java command on the command prompt to run the java
class, (runtime Instance) an instance of JVM is created.
The JVM performs the following main tasks: Loads code, Verifies code, Executes
code, Provides runtime environment.
JVM ARCHITECTURE
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.
Bootstrap ClassLoader: This is the first classloader which is the super class of Extension classloader.
It loads the rt.jar file which contains all class files of Java Standard Edition like.
• java.lang package classes, java.net package classes, java.util package classes, java.io package
classes, java.sql package classes etc.
Extension ClassLoader: This is the child classloader of Bootstrap and parent classloader of System
classloader. It loads the jar files located inside.
• $JAVA_HOME/jre/lib/ext directory.
9
JVM ARCHITECTURE
System/Application ClassLoader: This is the child classloader of Extension classloader. It loads
the classfiles from classpath. By default, classpath is set to current directory. You can change
the classpath using "-cp" or "-classpath" switch. It is also known as Application class loader.
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.
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.
10
JVM ARCHITECTURE
5) Program Counter Register - PC (program counter) register contains the address of the Java virtual machine instruction currently
being executed.
6) Native Method Stack - It contains all the native methods used in the application.
7) Execution Engine -
A virtual processor
Interpreter: Read bytecode stream then execute the instructions.
Just-In-Time(JIT) compiler: It is used to improve the performance. JIT compiles parts of the byte code that have
Similar functionality at the same time, and hence reduces the amount of time needed for compilation. Here,
the term "compiler" refers to a translator from the instruction set of a Java virtual machine (JVM) to the
instruction set of a specific CPU.
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.
JVM, JRE, and JDK are platform dependent because the configuration of each OS is different from each other.
12
Java Byte Code
13
Differences Between Byte code and Machine Code
2 Byte code is considered as the intermediate-level code Machine Code is considered as the low-
level code.
3 Byte code is a non-runnable code generated after compilation of source code and Machine code is a set of instructions in
it relies on an interpreter to get executed machine language or in binary format
and it is directly executed by CPU.
4 Byte code is executed by the virtual machine than the Central Processing Unit. Machine code is not executed by a
virtual machine it is directly executed by
CPU.
5 Byte code is less specific towards machine than the machine code Machine code is more specific towards
machine than the byte code.
6 It is platform-independent as it is dependent on the virtual machine and the system It is not platform independent because
having a virtual machine can be executed irrespective of the platform. the object code of one platform can not
be run on the same Operating System.
7 All the source code need not be converted into byte code for execution by CPU. All the source code must be converted
Some source code written by any specific high-level language is converted into into machine code before it is executed
byte code then byte code to object code for execution by CPU. by the CPU.
14
Simple Java Program
A Java library is just a collection of classes that have been written by somebody else already. You download
those classes and tell your computer about them, and then you can use those classes in your code.
If one installs Java Development Kit (JDK), in the directory above where you find the libs you will probably find
a src.jar file. This can be unpacked either with the jar.exe utility, or with a standard zip-style tool, and contains
the Java sources to the standard class.
Libraries are places of information, offering people free access to the information that are found elsewhere.
Example:
java. util is a package and part of Java class library: A package is a namespace that organizes a set of related
classes and interfaces. The Java platform provides an enormous class library (a set of packages) suitable for use
in your own applications.
16
LITERALS, IDENTIFIERS, WHITESPACES, COMMENTS, OPERATORS, SEPARATORS, KEYWORDS:
Java programs are a collection of White spaces, identifiers, Comments, Literals, Operators, Separators and Keywords.
Java is a free form language i.e., you do not need to follow any special indentation rules.
Identifiers are used for class names, Method names and variable names. An Identifier may be any descriptive
sequence of Upper and lower case letters, numbers, Under score ( _ ) and a dollar Sign ( $ ) characters.
Example: Avgtemp Count A4 $test7 this_is_ok
19
COMMENT STATEMENTS IN JAVA
In Java there are three types of Comment Statements.
1. Single line Comment.
2. Multi line Comment.
3. Documentation Comment.
For longer comments, we can create long Multi line comments by starting with a /* and ending with
*/.
The documentation comment is used to produce an HTML file that documents your program. The
documentation comment with /** a and ends with a **/.
20
OOPS Concept in Java
Encapsulation:
• The wrapping up of data and methods into a single unit (class ) is known as “Encapsulation”.
• Data Encapsulation is the most striking feature of a class.
• The data is not accessible to the outside world, and only those functions which are wrapped in the class can access it.
• There functions provide the Interface between the objects data and the program.
Inheritance:
• Inheritance is one of the concepts in Oops.
• Inheritance is the process by which objects of one class acquire the properties of objects of another class.
• It supports the concept of hierarchical classification.
• The concepts of Inheritance provides the idea of reusability. i.e., we can add additional feature to an existing class without modifying it.
Polymorphism:
• Polymorphism is one of the important concepts in OOPS.
• Polymorphism means “the ability to take more than one form”.
• An operation can exhibit different behaviors in different instances.
• Polymorphism plays an important role in allowing objects having different internal structures to share the same external interface.
• Polymorphism is extensively used in implementing Inheritance.
21
Encapsulation
Types of Inheritance
22
Data Type in a Programming Language
23
Declaration and Initialization of a Variable
In Java, Variable is the basic unit of storage. It is defined as the combination of an Identifier. In Java, all
variables must be declared before they can be used. A variable can be used to store a value of any data type.
The general form of declaration of a variable is:
Suppose if the two types are incompatible, you must use a “cast”,
which performs an explicit conversion between incompatible types.
25
Type-Casting and Example of Type-Casting
The process of converting one data type to another is called
“Casting”.
26
Types of Operators:
Java supports a rich set of operators. An operator is a symbol that tells the
computer to perform certain Mathematical (or) Logical Manipulations.
27
Arithmetic (Mathematical) Operators
Operator Name Description Example
+ Addition Adds together two x+y Java Comparison Operators
values
Operato Name Exampl
- Subtraction Subtracts one value x-y r e
from another
== Equal to x == y
* Multiplication Multiplies two values x*y
!= Not equal x != y
/ Division Divides one value by
another x/y > Greater than x>y
BITWISE OPERATOR
| (bitwise or) Binary OR Operator copies a bit if it exists in either operand. (A | B) will give 61 which is 0011 1101 Java defines several
bitwise operators, which
^ (bitwise XOR)
Binary XOR Operator copies the bit if it is set in one operand but
(A ^ B) will give 49 which is 0011 0001 can be applied to the
not both.
integer types, long, int,
short, char, and byte.
Binary Ones Complement Operator is unary and has the effect of (~A ) will give -61 which is 1100 0011 in 2's
~ (bitwise compliment) 'flipping' bits. complement form due to a signed binary
number. Bitwise operators are
used for testing the bits
<< (left shift) Binary Left Shift Operator. The left operands value is moved left A << 2 will give 240 which is 1111 0000 (or) shifting them to the
by the number of bits specified by the right operand.
left (or) right.
Binary Right Shift Operator. The left operands value is moved These Operators may not
>> (right shift) A >> 2 will give 15 which is 1111
right by the number of bits specified by the right operand. be applicable to float (or)
double.
Java defines several
Shift right zero fill operator. The left operands value is moved
>>> (zero fill right shift) right by the number of bits specified by the right operand and bitwise operators, which
A >>>2 will give 15 which is 0000 1111
shifted values are filled up with zeros. can be applied to the
integer types, long, int,
short, char, and byte.29
Examples of Bitwise Operator
31
If Statement:
If…Else Statement:
The syntax (or) general form of a simple if This statement is an extension of the simple if statement. The syntax
statement is. is:
If(Condition) If(condition)
• { {
• True-Block statement;
statement-block;
}
• } else
• Statement x; {
Here, the statement – Block may be a single False – Block statement;
statement (or) a group of statements. }
If the condition is true, the statement – block will Statement – x;
be executed.
If the condition is true, then the true – statement Block is executed.
Otherwise, the statement – Block will be skipped Other wise, the False - statement Block is executed.
and the Execution will jump to statement – X. In both these cases, the control is transferred sub-sequently to the
statement –x.
In either case , either True –Block (or) False – Block will be executed
but not the both.
32
Nested If Statement Else if Ladder:
When a series of decisions are involved, we use more than one if…else statement
is Nested form. The way of putting if’s together when multipath decisions are involved. A
multiple decision is a chain of if’s in which the statement is associated.
If(Condition 1)
{ The syntax of else if ladder is:
if(Condition 2) If(Condition 1)
{ statement 1;
Statement 1; else if(Condition 2)
} statement 2;
else else if(Condition N)
{ statement-n;
Statement 2; else
} default statement;
} Statement-x;
else
{ This construction is known as the else if ladder. The condition are evaluated
Statement 3; from top to down ward. As soon as the true condition is found. The statement
} associated with it is executed and the control is transferred to the statement –
Statement -X ; x;
When all conditions become false, then the final else containing the default
Hence, if the condition 1 false, the statement 3 is executed. Other wise, continue statement will be executed.
to perform the condition 2.
If condition 2 is False, the statement 1 will be executed. Other wise the
statement 2 will be executed and then the control will be transferred to the
statement-x.
33
Switch Statement: Conditional Operator
The switch statement tests the value of a given variable (or) condition
against a list of case values and when a match is found, a block of This operator is a combination of ? And : And takes three
statement associated with that case is executed..
operators. This operator is known as Conditional Operator.
The syntax of switch statement is:
The general form is:
• Switch(Expression) Conditional Expression1?Expression 2:Expression 3;
• {
• case value-1;
• Block-1;
– break;
• case value-2;
• Block-2;
• break;
• case value-N;
• Block-N;
• break;
• default:
• default-block;
• break;
• }
34
Iteration Statements in Java:
While Loop: The Do-While Loop:
Java provides three loop (or) Iterative statements. The Do-while loop always executes its body at
least once because its condition expression is
They are: at the bottom of the loop.
1. While Loop: While loop is Java’s most fundamental looping
statement. It repeats a statement (or) Block while its controlling Initialization;
expression is true. Do
{
Initialization; body of the loop;
• While(Condition) }while(Condition);
• {
• Body of loop; Each iteration of the Do-while loop executes
• } the “body of the loop” and then evaluates the
conditional expression.
The conditions can be any boolean expression. The “Body of Loop” will If the condition is true, the loop will repeats.
be executed as long as the conditional expression is “True”. When the Otherwise the loop terminates.
condition becomes false, control passes to the next line of the code
immediately following the loop. The while is an “Entry Control Loop”
This loop is also called as the ExitControlLoop.
statement.
35
For Loop:
The For loop is another “Entry Control loop”.
Break Statement :
In Java, the break statement has 3 uses.
The general form of the for loop is. 1.It terminates a statement sequence in a switch statement.
For(Initialization; Condition; Increment/Decrement) 2.It can be used to exit a loop.
{ 3.It can be used as a civilized form of a go to statement.
Body of the loop;
The syntax is
} break;
Continue Statement:
The Java continue statement stops one iteration in a loop and
JUMP STATEMENTS: continues to the next iteration.
Jump statements in Java, transfer the control to another part of your
program.
This statement lets you skip particular iterations without stopping a
Java supports 3 Jump statements. They are:
loop entirely. ...
• 1.Break
•
That's where the continue statement comes in.
2.Continue
• 3. Return
The Java continue statement is used to skip the current iteration of a
loop in Java
36
Example on Continue Statement:
ContinueExample.java
//
Java Program to demonstrate the use of continue statement
//inside the for loop.
public class ContinueExample {
public static void main(String[] args) {
//for loop
for(int i=1;i<=10;i++){
if(i==5){
//using continue statement
continue;//it will skip the rest statement
}
System.out.println(i);
}
}
}
37
Return Statement:
Output:
[Java, C++, Python]
38
Scope and Lifetime of a Variable
39
THANK YOU
40