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

Java Basics

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Java Basics

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Java is an Object-Oriented programming language developed by James Gosling in the early 1990s.

The team
initiated this project to develop a language for digital devices such as set-top boxes, television, etc.
Originally C++ was considered to be used in the project but the idea was rejected for several reasons(For instance
C++ required more memory). Gosling endeavoured to alter and expand C++ however before long surrendered
that for making another stage called Green. James Gosling and his team called their project “Greentalk” and its
file extension was .gt and later became to known as “OAK”.
Why “Oak”?
The name Oak was used by Gosling after an oak tree that remained outside his office. Also, Oak is an image of
solidarity and picked as a national tree of numerous nations like the U.S.A., France, Germany, Romania, etc. But
they had to later rename it as “JAVA” as it was already a trademark by Oak Technologies. “JAVA” Gosling and
his team did a brainstorm session and after the session, they came up with several names such as JAVA, DNA,
SILK, RUBY, etc. Java name was decided after much discussion since it was so unique. The name Java
originates from a sort of espresso bean, Java. Gosling came up with this name while having a coffee near his
office. Java was created on the principles like Robust, Portable, Platform Independent, High Performance,
Multithread, etc. and was called one of the Ten Best Products of 1995 by the TIME MAGAZINE. Currently,
Java is used in internet programming, mobile devices, games, e-business solutions, etc.
The Java language has experienced a few changes since JDK 1.0 just as various augmentations of classes and
packages to the standard library. In Addition to the language changes, considerably more sensational changes
have been made to the Java Class Library throughout the years, which has developed from a couple of hundred
classes in JDK 1.0 to more than three thousand in J2SE 5.
History of various Java versions:
Version Release Date Major changes

JDK Beta 1995

The Very first version was released


on January 23, 1996. The principal
JDK 1.0 January 1996
stable variant, JDK 1.0.2, is called
Java 1.

Was released on February 19, 1997.


There were many additions in JDK
JDK 1.1 February 1997
1.1 as compared to version 1.0 such
as
Version Release Date Major changes

• A broad retooling of the AWT


occasion show
• Inner classes added to the
language
• JavaBeans
• JDBC
• RMI

“Play area” was the codename


which was given to this form and
was released on 8th December 1998.
Its real expansion included: strictfp
keyword
• the Swing graphical API was
coordinated into the centre
J2SE 1.2 December 1998 classes
• Sun’s JVM was outfitted with a
JIT compiler out of the blue
• Java module
• Java IDL, an IDL usage for
CORBA interoperability
• Collections system

Codename- “KESTREL” Release


Date- 8th May 2000 Additions:
• HotSpot JVM included
• Java Naming and Directory
J2SE 1.3 May 2000
Interface
• JPDA
• JavaSound
• Synthetic proxy classes

Codename- “Merlin” Release Date-


6th February 2002 Additions:
Library improvements
• Regular expressions modelled
after Perl regular expressions
• The image I/O API for reading
and writing images in formats
J2SE 1.4 February 2002 like JPEG and PNG
• Integrated XML parser and
XSLT processor (JAXP)
(specified in JSR 5 and JSR 63)
• Preferences API (java.util.prefs)
Public Support and security updates
for this version ended in October
2008.
Version Release Date Major changes

Codename- “Tiger” Release Date-


“30th September 2004” Originally
numbered as 1.5 which is still used
as its internal version. Added
several new language features such
J2SE 5.0 September 2004 as:
• for-each loop
• Generics
• Autoboxing
• Var-args

Codename- “Mustang” Released


Date- 11th December 2006
Packaged with a database supervisor
and encourages the utilization of
scripting languages with the JVM.
Replaced the name J2SE with java
SE and dropped the .0 from the
JAVA SE 6 December 2006 version number. Additions:
• Upgrade of JAXB to version
2.0: Including integration of a
StAX parser.
• Support for pluggable
annotations (JSR 269).
• JDBC 4.0 support (JSR 221)

Codename- “Dolphin” Release


Date- 7th July 2011 Added small
language changes including strings
in the switch. The JVM was
extended with support for dynamic
JAVA SE 7 July 2011 languages. Additions:
• Compressed 64-bit pointers.
• Binary Integer Literals.
• Upstream updates to XML and
Unicode.

Released Date- 18th March 2014


Language level support for lambda
JAVA SE 8 March 2014 expressions and default methods and
a new date and time API inspired by
Joda Time.

Release Date: 21st September 2017


Project Jigsaw: designing and
JAVA SE 9 September 2017
implementing a standard, a module
system for the Java SE platform, and
Version Release Date Major changes

to apply that system to the platform


itself and the JDK.

Released Date- 20th March


Addition:
• Additional Unicode language-
tag extensions
• Root certificates
• Thread-local handshakes
JAVA SE 10 March 2018
• Heap allocation on alternative
memory devices
• Remove the native-header
generation tool – javah.
• Consolidate the JDK forest into
a single repository.

Released Date- 25th September,


2018 Additions-
• Dynamic class-file constants
• Epsilon: a no-op garbage
collector
• The local-variable syntax for
JAVA SE 11 September 2018
lambda parameters
• Low-overhead heap profiling
• HTTP client (standard)
• Transport Layer Security (TLS)
1.3
• Flight recorder

Released Date- 19th March 2019


Additions-
• Shenandoah: A Low-Pause-
Time Garbage Collector
(Experimental)
JAVA SE 12 March 2019 • Microbenchmark Suite
• Switch Expressions (Preview)
• JVM Constants API
• One AArch64 Port, Not Two
• Default CDS Archives

Released Date – 17th September


2019
Additions-
JAVA SE 13 September 2019 • Text Blocks (Multiline strings).
• Switch Expressions.
• Enhanced Thread-local
handshakes.
Version Release Date Major changes

Released Date – 17th March 2020


Additions-
• Records (new class type for
JAVA SE 14 March 2020
data modeling).
• Pattern Matching for instanceof.
• Helpful NullPointerExceptions.

Released Date – 15th September


2020
Additions-
JAVA SE 15 September 2020 • Sealed Classes.
• Hidden Classes.
• Foreign Function and Memory
API (Incubator).

Released Date – 16th March 2021


Additions-
• Records (preview feature).
JAVA SE 16 March 2021 • Pattern Matching for switch
(preview feature).
• Unix Domain Socket Channel
(Incubator).

Released Date – 14th September


2021
Additions-
• Sealed Classes (finalized).
JAVA SE 17 September 2021 • Pattern Matching for instanceof
(finalized).
• Strong encapsulation of JDK
internals by default.
• New macOS rendering pipeline.

Java JDK, JRE and JVM

What is JVM?

JVM (Java Virtual Machine) is an abstract machine that enables your computer to run a Java program.

When you run the Java program, Java compiler first compiles your Java code to bytecode. Then, the JVM
translates bytecode into native machine code (set of instructions that a computer's CPU executes
directly).
Java is a platform-independent language. It's because when you write Java code, it's ultimately written
for JVM but not your physical machine (computer). Since JVM executes the Java bytecode which is
platform-independent, Java is platform-independent.

Working of Java Program


What is JRE?
JRE (Java Runtime Environment) is a software package that provides Java class libraries, Java Virtual
Machine (JVM), and other components that are required to run Java applications.JRE is the superset of
JVM.

Java Runtime Environment


If you need to run Java programs, but not develop them, JRE is what you need.
What is JDK?
JDK (Java Development Kit) is a software development kit required to develop applications in Java.
When you download JDK, JRE is also downloaded with it.
In addition to JRE, JDK also contains a number of development tools (compilers, JavaDoc, Java Debugger,
etc).

Java Development Kit


If you want to develop Java applications, download JDK. Relationship between JVM, JRE, and JDK.

Relationship between JVM, JRE, and JDK Every Java developer knows that bytecode will be executed by
the JRE (Java Runtime Environment). But many don't know the fact that JRE is the implementation of Java
Virtual Machine (JVM), which analyzes the bytecode, interprets the code, and executes it. It is very important, as a
developer, that we know the architecture of the JVM, as it enables us to write code more efficiently. In this article,
we will learn more deeply about the JVM architecture in Java and different components of the JVM.

Differences between JDK, JRE and JVM

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.
Now we need an environment to make a run of our program. Henceforth, JRE stands
for “Java Runtime Environment” and may also be written as “Java RTE.” The
Java Runtime Environment provides the minimum requirements for executing a Java
application; it consists of the Java Virtual Machine (JVM), core classes,
and supporting files.
Now let us discuss JVM, which stands out for java virtual machines. It is as follows:
• A specification where the working of Java Virtual Machine is specified. But
implementation provider is independent to choose the algorithm. Its
implementation has been provided by Sun and other companies.
• An implementation is a computer program that meets the requirements of the
JVM specification.
• Runtime Instance Whenever you write a java command on the command prompt
to run the java class, an instance of JVM is created.
Before proceeding to the differences between JDK, JRE, and JVM, let us discuss them
in brief first and interrelate them with the image below proposed.

Don’t get confused as we are going to discuss all of them one by one.
1. JDK (Java Development Kit) is a Kit that provides the environment to develop and
execute(run) the Java program. JDK is a kit(or package) that includes two things
• Development Tools(to provide an environment to develop your java programs)
• JRE (to execute your java program).
2. JRE (Java Runtime Environment) is an installation package that provides an
environment to only run(not develop) the java program(or application)onto your
machine. JRE is only used by those who only want to run Java programs that are end-
users of your system.
3. JVM (Java Virtual Machine) is a very important part of both JDK and JRE
because it is contained or inbuilt in both. Whatever Java program you run using JRE
or JDK goes into JVM and JVM is responsible for executing the java program line by
line, hence it is also known as an interpreter.
Now let us discuss the components of JRE in order to understand its importance of it
and perceive how it actually works. For this let us discuss components.
The components of JRE are as follows:
1. Deployment technologies, including deployment, Java Web Start, and Java Plug-
in.
2. User interface toolkits, including Abstract Window Toolkit (AWT), Swing, Java
2D, Accessibility, Image I/O, Print Service, Sound, drag, and drop (DnD),
and input methods.
3. Integration libraries, including Interface Definition Language (IDL), Java
Database Connectivity (JDBC), Java Naming and Directory Interface (JNDI),
Remote Method Invocation (RMI), Remote Method Invocation Over Internet Inter-
Orb Protocol (RMI-IIOP), and scripting.
4. Other base libraries, including international support, input/output (I/O),
extension mechanism, Beans, Java Management Extensions (JMX), Java Native
Interface (JNI), Math, Networking, Override Mechanism, Security, Serialization,
and Java for XML Processing (XML JAXP).
5. Lang and util base libraries, including lang and util, management, versioning,
zip, instrument, reflection, Collections, Concurrency Utilities, Java Archive (JAR),
Logging, Preferences API, Ref Objects, and Regular Expressions.
6. Java Virtual Machine (JVM), including Java HotSpot Client and Server Virtual
Machines.
After having an adequate understanding of the components, now let us discuss the
working of JDK. In order to understand how JDK works, let us consider an illustration
below as follows:
Illustration:
Consider a java source file saved as ‘Example.java’. The file is compiled into a set of
Byte Code that is stored in a “.class” file. Here it will be “Example.class“.
Note: From above, media operation computing during the compile time can be
interpreted.
The following actions occur at runtime as listed below:
• Class Loader
• Byte Code Verifier
• Interpreter
• Execute the Byte Code
• Make appropriate calls to the underlying hardware

Now let us discuss in brief how JVM works out. It is as follows:


JVM becomes an instance of JRE at the runtime of a Java program. It is widely
known as a runtime interpreter.JVM largely helps in the abstraction of inner
implementation from the programmers who make use of libraries for their programs
from JDK.
It is mainly responsible for three activities.
• Loading
• Linking
• Initialization
Similarly, now let us discuss the working of JRE which is as follows:
• JVM(Java Virtual Machine) acts as a run-time engine to run Java applications. JVM
is the one that actually calls the main method present in a java code. JVM is a part
of JRE(Java Runtime Environment).
• Java applications are called WORA (Write Once Run Anywhere). This means a
programmer can develop Java code on one system and can expect it to run on any
other Java-enabled system without any adjustments. This is all possible because of
JVM.
• When we compile a .java file, .class files(contains byte-code) with the same class
names present in .java file are generated by the Java compiler. This .class file goes
into various steps when we run it. These steps together describe the whole JVM.

What Is the JVM?


A Virtual Machine is a software implementation of a physical machine. Java was developed with the concept
of WORA (Write Once Run Anywhere), which runs on a VM. The compiler compiles the Java file into a
Java .class file, then that .class file is input into the JVM, which loads and executes the class file. Below is a diagram
of the Architecture of the JVM.

JVM Architecture Diagram

How Does the JVM Work?


As shown in the above architecture diagram, the JVM is divided into three main subsystems:

1. ClassLoader Subsystem
2. Runtime Data Area
3. Execution Engine

1. ClassLoader Subsystem
Java's dynamic class loading functionality is handled by the ClassLoader subsystem. It loads, links. and initializes
the class file when it refers to a class for the first time at runtime, not compile time.

1.1 Loading
Classes will be loaded by this component. BootStrap ClassLoader, Extension ClassLoader, and Application
ClassLoader are the three ClassLoaders that will help in achieving it.

1. BootStrap ClassLoader – Responsible for loading classes from the bootstrap classpath, nothing
but rt.jar. Highest priority will be given to this loader.
2. Extension ClassLoader – Responsible for loading classes which are inside the ext folder (jre\lib).
3. Application ClassLoader –Responsible for loading Application Level Classpath, path mentioned
Environment Variable, etc.

The above ClassLoaders will follow Delegation Hierarchy Algorithm while loading the class files.
1.2 Linking

1. Verify – Bytecode verifier will verify whether the generated bytecode is proper or not if verification fails
we will get the verification error.
2. Prepare – For all static variables memory will be allocated and assigned with default values.
3. Resolve – All symbolic memory references are replaced with the original references from Method Area.

1.3 Initialization
This is the final phase of ClassLoading; here, all static variables will be assigned with the original values, and
the static block will be executed.

2. Runtime Data Area


The Runtime Data Area is divided into five major components:

1. Method Area – All the class-level data will be stored here, including static variables. There is only one
method area per JVM, and it is a shared resource.
2. Heap Area – All the Objects and their corresponding instance variables and arrays will be stored here.
There is also one Heap Area per JVM. Since the Method and Heap areas share memory for multiple
threads, the data stored is not thread-safe.
3. Stack Area – For every thread, a separate runtime stack will be created. For every method call, one entry
will be made in the stack memory which is called Stack Frame. All local variables will be created in the
stack memory. The stack area is thread-safe since it is not a shared resource. The Stack Frame is divided
into three subentities:
1. Local Variable Array – Related to the method how many local variables are involved and the
corresponding values will be stored here.
2. Operand stack – If any intermediate operation is required to perform, operand stack acts as
runtime workspace to perform the operation.
3. 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.
4. PC Registers – Each thread will have separate PC Registers, to hold the address of current executing
instruction once the instruction is executed the PC register will be updated with the next instruction.
5. Native Method stacks – Native Method Stack holds native method information. For every thread, a
separate native method stack will be created.

3. Execution Engine
The bytecode, which is assigned to the Runtime Data Area, will be executed by the Execution Engine. The
Execution Engine reads the bytecode and executes it piece by piece.

1. Interpreter – The interpreter interprets the bytecode faster but executes slowly. The disadvantage of the
interpreter is that when one method is called multiple times, every time a new interpretation is required.
2. JIT Compiler – The JIT Compiler neutralizes the disadvantage of the interpreter. The Execution Engine
will be using the help of the interpreter in converting byte code, but when it finds repeated code it uses the
JIT compiler, which compiles the entire bytecode and changes it to native code. This native code will be
used directly for repeated method calls, which improve the performance of the system.
1. Intermediate Code Generator – Produces intermediate code
2. Code Optimizer – Responsible for optimizing the intermediate code generated above
3. Target Code Generator – Responsible for Generating Machine Code or Native Code
4. Profiler – A special component, responsible for finding hotspots, i.e. whether the method is called
multiple times or not.
3. Garbage Collector: Collects and removes unreferenced objects. Garbage Collection can be triggered by
calling System.gc(), but the execution is not guaranteed. Garbage collection of the JVM collects the objects
that are created.
Java Native Interface (JNI): JNI will be interacting with the Native Method Libraries and provides the Native
Libraries required for the Execution Engine.

Java "Hello, World!" Program


// Your First Program

class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Output
Hello, World!

How Java "Hello, World!" Program Works?


// Your First Program

In Java, any line starting with // is a comment. Comments are intended for users
reading the code to understand the intent and functionality of the program. It is
completely ignored by the Java compiler (an application that translates Java program
to Java bytecode that computer can execute). To learn more, visit Java comments.
class HelloWorld { ... }

In Java, every application begins with a class definition. In the program, HelloWorld is
the name of the class, and the class definition is:
class HelloWorld {
... .. ...
}

For now, just remember that every Java application has a class definition, and the
name of the class should match the filename in Java.
public static void main(String[] args) { ... }

This is the main method. Every application in Java must contain the main method.
The Java compiler starts executing the code from the main method.

How does it work? Good question. However, we will not discuss it in this article.
After all, it's a basic program to introduce Java programming language to a newbie.
We will learn the meaning of public , static , void , and how methods work? in later
chapters.

For now, just remember that the main function is the entry point of your Java
application, and it's mandatory in a Java program. The signature of the main method
in Java is:
public static void main(String[] args) {
... .. ...
}
System.out.println("Hello, World!");
The code above is a print statement. It prints the text Hello, World! to standard
output (your screen). The text inside the quotation marks is called String in Java.
Notice the print statement is inside the main function, which is inside the class
definition.

Things to take away


Every valid Java Application must have a class definition that matches the filename
(class name and file name should be same).
The main method must be inside the class definition.
The compiler executes the codes starting from the main function.
Native Method Libraries: This is a collection of the Native Libraries, which is required for the Execution Engine.

You might also like