Chapter 1 Introduction
Chapter 1 Introduction
Introduction to JAVA
Introduction
Now it is owned by Oracle, and more than 3 billion devices run Java.
It is used for:
Prepared by: Dr. Mahesh Manchanda Department of CSE, Graphic Era Hill University, Dehradun 1
1.2 Java History:
� In 1991, Project Manager James Gosling and his team members Patrick
Naughton, Mike Sheridan, Chris Wrath, and Ed Frank of Sun Micro
Systems Inc.(US) were conceived a project to develop software (which
should be portable and machine independent) for consumer electronic
devices such set-top boxes, televisions etc that could be controlled by a
remote. This small team of sun engineers called Green Team.
� Gosling thought C and C++ would be used to develop the project. But the
problem he faced with them is that they were system dependent languages.
The trouble with C and C++ (and most other languages) is that they are
designed to be compiled for a specific target and could not be used on
various processors, which the electronic devices might use.
� James Gosling and his team members were consuming a lot of coffee while
developing this language. Good quality of coffee was supplied from a place
called “Java Island’. Hence they fixed the name of the language as Java. The
symbol for Java language is cup and saucer.
Prepared by: Dr. Mahesh Manchanda Department of CSE, Graphic Era Hill University, Dehradun 2
1.3 Java Version History
From the first version released in 1996 to the latest Standard Edition (SE)
version 15 released in Sept 2020.
Java SE
Version Version Release Date
Number
Prepared by: Dr. Mahesh Manchanda Department of CSE, Graphic Era Hill University, Dehradun 3
Java SE 10 10 March, 20th 2018
Features of Java
Features of a language are nothing but the set of services or facilities provided
by the language vendors to the industry programmers. Some
important features of java are:
Prepared by: Dr. Mahesh Manchanda Department of CSE, Graphic Era Hill University, Dehradun 4
Figure 1.1 Shows the features of Java
Prepared by: Dr. Mahesh Manchanda Department of CSE, Graphic Era Hill University, Dehradun 5
In the Java programming language, all source code is first written in
plain text files ending with the .java extension. Those source files are
then compiled into .class files by the javac compiler. A .class file does not
contain code that is native to your processor; it instead
contains bytecodes — the machine language of the Java Virtual
Machine (Java VM).
Prepared by: Dr. Mahesh Manchanda Department of CSE, Graphic Era Hill University, Dehradun 6
� Portable: In Java, many types of computers and operating system are in
use throughout the world and are connected to the Internet. For
downloading programs through different platforms connected to the
Internet, some portable, executable code is needed. Java does not have
implementation dependent aspects and it yields or gives same result
on any machine.
� Secured: Java is best known for its security. With Java, we can develop
virus-free systems. Java is secured because:
� No explicit pointer
� Java Programs run inside a virtual machine sandbox
Prepared by: Dr. Mahesh Manchanda Department of CSE, Graphic Era Hill University, Dehradun 7
� Robust (Strong/ Powerful): Most programs in use today fail for one of
the two reasons: memory management or exceptional conditions. Thus,
the ability to create robust programs was given a high priority in the
design of Java. Java forces the user to find mistake in the early stages of
program development. Java checks code at compilation time. However, it
also checks the code at run time also. Java programs will not crash
because of its exception handling and its memory management
features.
Prepared by: Dr. Mahesh Manchanda Department of CSE, Graphic Era Hill University, Dehradun 8
� High Performance:
It has high performance because of following reasons:
� Garbage collector, collect the unused memory space and improve the
performance of the application.
� It support multithreading, because of this time consuming process
can be reduced to executing the program.
� Along with interpreter there will be JIT (Just In Time) compiler which
enhances the speed of execution.
� Multithreaded: Executing different parts of program simultaneously is
called multithreading. This is an essential feature to design server side
programs.
� Dynamic: We can develop programs in Java which dynamically
change on Internet (e.g.: Applets).
There are many differences and similarities between the C++ programming
language and Java. A list of top differences between C++ and Java are given
below:
Comparison
C++ Java
Index
Prepared by: Dr. Mahesh Manchanda Department of CSE, Graphic Era Hill University, Dehradun 9
Java doesn't support multiple
Multiple C++ supports multiple inheritance through class. It can
inheritance inheritance. be achieved by using interfaces in
java.
Call by Value C++ supports both call Java supports call by value only.
and Call by by value and call by There is no call by reference in
reference reference. java.
Prepared by: Dr. Mahesh Manchanda Department of CSE, Graphic Era Hill University, Dehradun 10
Java has no virtual keyword. We
C++ supports virtual
can override all non-static
keyword so that we can
Virtual Keyword methods by default. In other
decide whether or not to
words, non-static methods are
override a function.
virtual by default.
Prepared by: Dr. Mahesh Manchanda Department of CSE, Graphic Era Hill University, Dehradun 11
The Java Development Kit comes with a collection of tools that are used for
developing and running Java programs. They include:
Java (Java Interpreter) : Which runs applets and applications by reading and
interpreting bytecode.
Javah (for C header files) : Produce header file for use with native methods.
The Java Standard Library (or API) includes thousands of classes and methods
grouped into several functional packages. Most commonly used packages are:
Applet package: This includes as set of classes that allows us to create Java
applets. [Link]
Prepared by: Dr. Mahesh Manchanda Department of CSE, Graphic Era Hill University, Dehradun 12
Runtime class libraries: These are the set of core class libraries that are
required for the execution of Java program
User Interface toolkits: AWT and Swings are the examples of toolkit the
support varied input methods for the users to interact with the application
program.
Java Virtual Machine (JVM) is the heart of entire Java program execution
process. First of all, the .java program is converted into a .class file consisting
of byte code instructions by the java compiler at the time of compilation.
Remember, this java compiler is outside the JVM. This .class file is given to the
JVM. JVM mainly performs following operations.
Prepared by: Dr. Mahesh Manchanda Department of CSE, Graphic Era Hill University, Dehradun 13
Figure :The internal architecture of the Java virtual machine.
In JVM, there is a module (or program) called class loader sub system,
which performs the following instructions:
Prepared by: Dr. Mahesh Manchanda Department of CSE, Graphic Era Hill University, Dehradun 14
b. Operand stack – If any intermediate operation is required to
perform, operand stack act as runtime workspace to perform the
operation.
c. Frame data – All symbols corresponding to the method is stored
here. In the case of any exception, the catch block information will
be maintained in the frame data.
5. PC Registers – Each thread will have separate PC Registers, to hold
address of current executing instruction once the instruction is
executed the PC register will be updated with the next instruction. It
holds the address of next executable instruction that means that use the
priority for the method in the execution process?
6. Native Method Stacks: When a Java application invokes a native
method, that application does not only use Java Stacks but also uses
the native method stack for the execution of native methods (for example
C/C++ functions). To execute the native methods, generally native
method libraries (for example C/C++ header files) are required. The
libraries required for the execution of native methods are available to the
Java Virtual Machine through Java Native Interface.
Note: The main aim of JIT compiler is to speed up the execution of java
program.
Generally, any language (like C/C++, pascal, cobol, etc.) will use either an
interpreter or a compiler to translate the source code into a machine code.
But in JVM, we got interpreter and JIT compiler both working at the same
time on byte code to translate it into machine code. Now, the main question is
why both are needed and how both work simultaneously? To understand this,
let us take some sample code. Assume these are byte code instructions:
print a;
print b;
Repeat the following 10 time by changing i value from 1 to 10.
When, the interpreter starts execution from 1 st instruction, it converts print
a; into machine code and gives it to the microprocessor, For this, say the
interpreter has taken 2 nanoseconds time. The processor takes it, executes it,
and the value of a is displayed.
Prepared by: Dr. Mahesh Manchanda Department of CSE, Graphic Era Hill University, Dehradun 15
Now the interpreter comes back into memory and reads the 2 nd instruction
print b;. To convert this into machine code, it takes another 2 nanoseconds.
Then the instruction is given to the processor and it execute it.
Next, the interpreter comes to the 3 rd instruction, which is a looping
statement print a; This should be done 10 times and this is known to the
interpreter. Hence, the first time it converts print a; into machine code, it
takes 2 nanosecond. After giving the instruction to the processor, it comes
back to the memory and reads the print a instruction the 2 nd time converts it
to machine code. This will take another 2 nanoseconds. This will given to the
processor to execute. Like this, the interpreter will convert the print a
instruction for 10 times, consuming a total of 10 x 2 = 20 nanoseconds. This
procedure is not efficient in terms of time. That is the reasons why JVM does
not allocate this code to the interpreter. It allows this code to the JIT compiler.
Let us see how JIT compiler will execute the looping instruction. First of all,
the JIT compiler reads the print a instruction and converts that into machine
code. For this, say, it is taking 2 nanoseconds. Then the JIT compiler allots a
block of memory and pushes this machine code instruction into that memory.
For this, say, it is taking another 2 nanoseconds. This means JIT compiler
has taken a total of 4 nanoseconds. Now, the processor will fetch this
instruction from memory and execute it 10 times. Just observe that the JIT
compiler has taken only 4 nanoseconds for execution; whereas to execute the
same loop, the interpreter needs 20 nanoseconds. Thus, JIT compiler increase
the speed of execution.
Prepared by: Dr. Mahesh Manchanda Department of CSE, Graphic Era Hill University, Dehradun 16
Figure Abstract view of Java program execution.
JVM works on different operating systems. The .class files (bytecode) can run
on various operating systems. JVM converts bytecode into computer-readable
machine code that can be executed.
This command will produce a file ‘[Link]’, which is used for running the
program with the command ‘java’.
Prepared by: Dr. Mahesh Manchanda Department of CSE, Graphic Era Hill University, Dehradun 17
Setting path and classpath in Java is very simple but before do this process
you need to know about path variable and classpath variable. Here I will
show you how to set path and classpath in Java in very simple and easy
way.
Path Variable
Path variable is set for providing path for all Java tools like java, javac,
javap, javah, jar, appletviewer which are used in java programming. All these
tools are available in bin folders so we set path upto bin folders.
Classpath Variable
Classpath variable is set for providing a path for predefined Java classes
which is used in our application. All classes are available in lib/[Link] so we
set classpath upto lib/[Link].
Prepared by: Dr. Mahesh Manchanda Department of CSE, Graphic Era Hill University, Dehradun 18
When you get this type of error, then your operating system cannot find the
Java compiler (javac). To solve this error you need to set the PATH variable.
Javac is a tool which is available in bin folder so you must set the PATH
upto bin folder. In a bin folder all tools are available like javap, javah, jar,
javac, java, appletviewer etc. All these tools are used for different-different
purpose.
Prepared by: Dr. Mahesh Manchanda Department of CSE, Graphic Era Hill University, Dehradun 19
Now click on advance setting
Click on advance
Prepared by: Dr. Mahesh Manchanda Department of CSE, Graphic Era Hill University, Dehradun 20
Click on Advance variables
Prepared by: Dr. Mahesh Manchanda Department of CSE, Graphic Era Hill University, Dehradun 21
Click on new button which is below the first box.
Now one dilog box is appear, now ignore this but do not close.
Now open my computer open c:/ > Programs Files > java > java1.6.0 > bin
copy this path
Prepared by: Dr. Mahesh Manchanda Department of CSE, Graphic Era Hill University, Dehradun 22
Now come back on previous open dilogbox and write variable
name 'path' and for variable value paste all copied path upto the bin folder.
Put .; at the end. It (.) selects all the tools from the bin folder.
Now open my computer open c:/ > Programs Files > java > java1.6.0 > jre >
lib > [Link] copy this path
Note: [Link] is available in lib folder this jar files contains all classes of jdk.
Prepared by: Dr. Mahesh Manchanda Department of CSE, Graphic Era Hill University, Dehradun 23
Now again come back on Environment variable dilogbox and click on new.
Now one box is open and write path variable as 'classpath' and for variable
value paste all copied paths upto [Link]. Put .; at the end. It (.) selects all the
classes from lib folder.
Prepared by: Dr. Mahesh Manchanda Department of CSE, Graphic Era Hill University, Dehradun 24
Note: Finally after set classpath restart your system, or you can re-open
command prompt.
Prepared by: Dr. Mahesh Manchanda Department of CSE, Graphic Era Hill University, Dehradun 25
� File with no public class can have any name there is no rule applied for
the same.
� Import and package statements applied to all the classes in same
source code file.
� The first line of the C/C++ program contains include statement. For
example, <stdio.h> is the header file that contains functions, like
printf (), scanf () etc. So if we want to use any of these functions, we
should include this header file in C/ C++ program.
� Similarly in Java first we need to import the required packages. By
default [Link].* is imported. Java has several such packages in its
library.
� A package is a kind of directory that contains a group of related classes
and interfaces. A class or interface contains methods.
� Since Java is purely an Object Oriented Programming language,
we cannot write a Java program without having at least one class or
object.
� So, it is mandatory to write a class in Java program. We should use
class keyword for this purpose and then write class name.
� In C/C++, program starts executing from main method similarly in
Java, program starts executing from main method. The return type of
main method is void because program starts executing from main
method and it returns nothing.
� Each word of class name and interface name starts with a capital
� Method names start with small letters then each word start with a
capital
Prepared by: Dr. Mahesh Manchanda Department of CSE, Graphic Era Hill University, Dehradun 26
� Constants should be written using all capital letters
Prepared by: Dr. Mahesh Manchanda Department of CSE, Graphic Era Hill University, Dehradun 27
� JVM is a program written by Java Soft people (Java development
team) and main() is the method written by us. Since, main () method
should be available to the JVM, it should be declared as public. If we
don’t declare main () method as public, then it doesn’t make itself
available to JVM and JVM cannot execute it.
� JVM always looks for main() method with String type array as
parameter otherwise JVM cannot recognize the main() method, so we
must provide String type array as parameter to main () method. Main()
method having signature with default variable arguments.
At compile time, java file is compiled by Java Compiler (It does not interact with
OS) and converts the java code into bytecode.
Prepared by: Dr. Mahesh Manchanda Department of CSE, Graphic Era Hill University, Dehradun 28
What happens at runtime?
The java command-line argument is an argument i.e. passed at the time of running the java
program. The arguments passed from the console can be received in the java program and it can
Prepared by: Dr. Mahesh Manchanda Department of CSE, Graphic Era Hill University, Dehradun 29
be used as an input. So, it provides a convenient way to check the behavior of the program for
the different values. You can pass N (1,2,3 and so on) numbers of arguments from the command
prompt.
Example
1. class CommandLineExample{
2. public static void main(String args[]){
3. [Link]("Your first argument is: "+args[0]);
4. }
5. }
Example
In this example, we are printing all the arguments passed from the command-
line. For this purpose, we have traversed the array using for loop.
1. class A{
2. public static void main(String args[]){
3.
4. for(int i=0;i<[Link];i++)
5. [Link] (args[i] + “ ”);
6.
7. }
8. }
Output:
Do 10 Good 20 Have 30 Good
Prepared by: Dr. Mahesh Manchanda Department of CSE, Graphic Era Hill University, Dehradun 30
platform differs from most other platforms in that it's a software-only platform
that runs on top of other hardware-based platforms.
The Java platform has two components:
� The Java Virtual Machine
� The Java Application Programming Interface (API)
Can you save a java source file by other name than the class name?
Yes, if the class is not public. It is explained in the figure given below:
Prepared by: Dr. Mahesh Manchanda Department of CSE, Graphic Era Hill University, Dehradun 31
Difference between JDK, JRE, and JVM
JVM
o Loads code
o Verifies code
o Executes code
o Provides runtime environment
JRE
Prepared by: Dr. Mahesh Manchanda Department of CSE, Graphic Era Hill University, Dehradun 32
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:
The JDK contains a private Java Virtual Machine (JVM) and a few other
resources such as an interpreter/loader (java), a compiler (javac), an archiver
(jar), a documentation generator (Javadoc), etc. to complete the development of
a Java Application.
What is Java?
Prepared by: Dr. Mahesh Manchanda Department of CSE, Graphic Era Hill University, Dehradun 33
If I don't provide any arguments on the command line, then what will the
value stored in the String array passed into the main() method, empty or
NULL?
The program compiles and runs correctly because the order of specifiers
doesn't matter in Java.
Difference between Java and C++
Java SE is also called Core Java. It is the set of libraries that are part of
standard java installation. For example, the Collections framework is part of
Core Java. But, Servlet/JSP is part of Java Enterprise Edition.
Java was first created by James Gosling at Sun Microsystems. The first public
version was released in 1996.
Java 15 is the latest version, released on Sept 15, 2020. Now, Oracle releases a
new java version every 6 months.
The ease of executing the code on any platform makes this language a
Platform Independent Programming Language. Just by installing JDK
software on a system, JVM also gets installed automatically. This JVM (Java
virtual machine) is the one that can execute Java bytecode. Each Java source
file is compiled into a bytecode file, which is later executed by the JVM. Java
was designed with a motto to allow the programs to be run on any platform,
without the need of rewriting or compiling them each time recompile for
different platforms.
Prepared by: Dr. Mahesh Manchanda Department of CSE, Graphic Era Hill University, Dehradun 34
Define the Java’s WORA nature?
WORA means Write Once Run Anywhere. Java is compiled to a bytecode
which does not platform specific. This bytecode is the intermediate language
between the source code and the machine code. As the bytecode is not
platforming specific, so it can be used to any platform. Sometimes, it is also
called WORE – Write Once Run Everywhere.
How can we execute any code even before the main method?
If we want to execute any statements before the even creation of objects at
load time of class, we can use a static block of code in the class. Any
statements inside this static block of code will get executed once at the time
of loading the class even before the creation of objects in the main method.
What is classloader?
Prepared by: Dr. Mahesh Manchanda Department of CSE, Graphic Era Hill University, Dehradun 35
This document was created with the Win2PDF “Print to PDF” printer available at
[Link]
This version of Win2PDF 10 is for evaluation and non-commercial use only.
Visit [Link] for a 30 day trial license.
This page will not be added after purchasing Win2PDF.
[Link]