Class IX Chapter 2
Class IX Chapter 2
Introduction to java
What is Java?
• Java is a programming language and a platform.
• Java is a high level, robust, object-oriented and secure programming
language.
• Java was developed by Sun Microsystems (which is now the
subsidiary of Oracle) in the year 1995.
• James Gosling is known as the father of Java.
• Before Java, its name was Oak. Since Oak was already a registered
company, so James Gosling and his team changed the name from
Oak to Java.
• Platform: Any hardware or software environment in which a
program runs, is known as a platform. Since Java has a runtime
environment (JRE) and API, it is called a platform.
History of Java
• Java was originally designed for
interactive television, but it was too
advanced technology for the digital cable
television industry at the time.
• The history of Java starts with the Green
Team. Java team members (also known
as Green Team), initiated this project to
develop a language for digital devices
such as set-top boxes, televisions, etc.
• Currently, Java is used in internet
programming, mobile devices, games, e-
business solutions, etc. Following are
given significant points that describe the
history of Java.
1. James Gosling, Mike Sheridan, and Patrick
Naughton initiated the Java language project
in June 1991. The small team of sun
engineers called Green Team.
2. Initially it was designed for small, embedded
systems in electronic appliances like set-top
boxes.
3. Firstly, it was called "Greentalk" by James
Gosling, and the file extension was .gt.
4. After that, it was called Oak and was
developed as a part of the Green project.
Why Java was named as "Oak"?
• Why Oak? Oak is a symbol of strength and
chosen as a national tree of many countries
like the U.S.A., France, Germany, Romania,
etc.
• In 1995, Oak was renamed as "Java" because
it was already a trademark by Oak
Technologies.
Java Version History
• Many java versions have been released till now. The current stable release
of Java is Java SE 10.
• JDK Alpha and Beta (1995) JDK 1.0 (23rd Jan 1996)
• JDK 1.1 (19th Feb 1997) J2SE 1.2 (8th Dec 1998)
• J2SE 1.3 (8th May 2000) J2SE 1.4 (6th Feb 2002)
• J2SE 5.0 (30th Sep 2004) Java SE 6 (11th Dec 2006)
• Java SE 7 (28th July 2011) Java SE 8 (18th Mar 2014)
• Java SE 9 (21st Sep 2017) Java SE 10 (20th Mar 2018)
• Java SE 11 (September 2018) Java SE 12 (March 2019)
• Java SE 13 (September 2019) Java SE 14 (Mar 2020)
• Java SE 15 (September 2020) Java SE 16 (Mar 2021)
• Java SE 17 (September 2021) Java SE 18 (to be released
by March 2022)
• Since Java SE 8 release, the Oracle corporation follows a pattern in which
every even version is release in March month and an odd version released
in September month.
Features of Java
• The primary objective of Java programming
language creation was to make it portable,
simple and secure programming language.
• Apart from this, there are also some excellent
features which play an important role in the
popularity of this language.
• The features of Java are also known as Java
buzzwords.
Simple
• Java is very easy to learn, and its syntax is simple, clean
and easy to understand. According to Sun Microsystem,
Java language is a simple programming language
because:
• Java syntax is based on C++ (so easier for programmers
to learn it after C++).
• Java has removed many complicated and rarely-used
features, for example, explicit pointers, operator
overloading, etc.
• There is no need to remove unreferenced objects because
there is an Automatic Garbage Collection in Java.
Object-oriented
• Java is an object-oriented programming language.
Everything in Java is an object. Object-oriented means
we organize our software as a combination of different
types of objects that incorporate both data and
behavior.
• Object-oriented programming (OOPs) is a
methodology that simplifies software development and
maintenance by providing some rules.
• Basic concepts of OOPs are: Object, Class, Inheritance,
Polymorphism ,Abstraction and Encapsulation.
Platform Independent
• Java is platform independent
because it is different from other
languages like C, C++, etc. which
are compiled into platform specific
machines while Java is a write
once, run anywhere language.
• A platform is the hardware or
software environment in which a
program runs.
• There are two types of platforms
software-based and hardware-
based. Java provides a software-
based platform.
• The Java platform differs from most other platforms
in the sense that it is a software-based platform that
runs on top of other hardware-based platforms.
• It has two components:
– Runtime Environment
– API(Application Programming Interface)
• Java code can be executed on multiple platforms, for
example, Windows, Linux, Sun Solaris, Mac/OS, etc.
Java code is compiled by the compiler and converted
into bytecode.
• This bytecode is a platform-independent code because
it can be run on multiple platforms, i.e., Write Once
and Run Anywhere (WORA).
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
• Class loader:
– Class loader in Java is a part of the Java Runtime Environment
(JRE) which is used to load Java classes into the Java Virtual
Machine dynamically.
– It adds security by separating the package for the classes of the
local file system from those that are imported from network
sources.
• Bytecode Verifier:
– It checks the code fragments for illegal code that can violate
access rights to objects.
• Security Manager:
– It determines what resources a class can access such as reading
and writing to the local disk.
– Java language provides these securities by default. Some security
can also be provided by an application developer explicitly
through SSL, JAAS, Cryptography, etc.
Robust
• The English mining of Robust is strong. Java is
robust because:
– It uses strong memory management.
– There is a lack of pointers that avoids security problems.
– Java provides automatic garbage collection which runs
on the Java Virtual Machine to get rid of objects which
are not being used by a Java application anymore.
– There are exception handling and the type checking
mechanism in Java. All these points make Java robust.
Architecture-neutral
• Java is architecture neutral because there are
no implementation dependent features, for
example, the size of primitive types is fixed.
• In C programming, int data type occupies 2
bytes of memory for 32-bit architecture and 4
bytes of memory for 64-bit architecture.
However, it occupies 4 bytes of memory for
both 32 and 64-bit architectures in Java.
Portable
• Java is portable because it facilitates you to
carry the Java bytecode to any platform.
• It doesn't require any implementation.
High-performance
• Java is faster than other traditional interpreted
programming languages because Java
bytecode is "close" to native code.
• It is still a little bit slower than a compiled
language (e.g., C++).
• Java is an interpreted language that is why it is
slower than compiled languages, e.g., C, C++,
etc.
Distributed
• Java is distributed because it facilitates users
to create distributed applications in Java.
• RMI and EJB are used for creating distributed
applications.
• This feature of Java makes us able to access
files by calling the methods from any machine
on the internet.
Multi-threaded
• A thread is like a separate program, executing
concurrently.
• We can write Java programs that deal with many
tasks at once by defining multiple threads.
• The main advantage of multi-threading is that it
doesn't occupy memory for each thread.
• It shares a common memory area.
• Threads are important for multi-media, Web
applications, etc.
Dynamic
• Java is a dynamic language. It supports the
dynamic loading of classes. It means classes
are loaded on demand.
• It also supports functions from its native
languages, i.e., C and C++.
• Java supports dynamic compilation and
automatic memory management (garbage
collection).
What is Java Bytecode?
• Java bytecode is the instruction
set for the Java Virtual Machine.
• It acts similar to an assembler
which is an alias representation of
a C++ code.
• As soon as a java program is
compiled, java bytecode is
generated.
• In more apt terms, java bytecode
is the machine code in the form of
a .class file.
• With the help of java bytecode we
achieve platform independence in
java.
How does it works?
• When we write a program in Java, firstly, the compiler compiles
that program and a bytecode is generated for that piece of code.
• When we wish to run this .class file on any other platform, we
can do so.
• After the first compilation, the bytecode generated is now run by
the Java Virtual Machine and not the processor in consideration.
• This essentially means that we only need to have basic java
installation on any platforms that we want to run our code on.
• Resources required to run the bytecode are made available by
theJava Virtual Machine, which calls the processor to allocate
the required resources.
• JVM's are stack-based so they stack implementation to read the
codes.
Ordinary Compilation
Ordinary compilation
the source code is
directly converted into
machine or object code
using an interpreter or
a compiler . In Java
compilation into an the
source code is first
converted into an
intermediate code called
the byte code.
Java Compilation Process
• The source code of a Java code is
compiled into an intermediate binary code
known as the Bytecode during the Java
compilation process.
• The machine cannot directly execute this
Bytecode.
• A virtual machine known as the Java
Virtual Machine, or JVM, understands it.
• JVM includes a Java interpreter that
converts Bytecode to target computer
machine code.
• JVM is platform-specific, which means
that each platform has its own JVM.
• However, once the proper JVM is installed
on the machine, any Java Bytecode code
can be run
JVM(Java Virtual Machine)
• JVM stands for Java Virtual Machine. It is software that provides
a runtime environment to execute Java code or other
programming languages that target the JVM.
• The JVM is responsible for interpreting and executing Java
bytecode, which is a compiled version of the Java source code. It
also provides features like memory management, security, and
platform independence.
• One of the key advantages of using the JVM is that it allows
developers to write code in Java or other JVM-compatible
languages and run it on any platform that has a JVM installed,
without having to worry about the underlying hardware or
operating system.
• Some popular JVM languages include Java, Kotlin, Scala, and
Groovy. The JVM is widely used in enterprise software
development, web development, and mobile app development.
JIT( Just-In-Time ):
• It is to transform bytecodes into native machine code at runtime, the Just-In-Time (JIT)
compiler, a part of the runtime environment, improves the performance of Java
applications.
• Classes, the building blocks of Java applications, include platform-neutral bytecodes that
a JVM can be a variety of computer architectures.
• The JVM loads the class files at runtime analyses the semantics of every individual
bytecode and executes the necessary computation.
• A Java application runs slower than a native application due to the increased CPU and
memory requirements during interpretation.
• By converting bytecodes into native machine code at run time, the JIT compiler
contributes to the performance enhancement of Java applications.
• By default, the JIT compiler is turned on.
• When a method is compiled, the JVM directly invokes the method’s compiled code
rather than interpreting it.
• The speed of the Java code could theoretically approach that of a native application if
compiling every method did not require processing time and memory use.
• JIT compilation does use memory and the processor. The JVM calls thousands of
methods when it first starts up.
• Even if the software eventually achieves extremely good peak performance, compiling
all of these techniques can greatly affect starting time.
First Java Program
class Simple
{
public static void main(String args[])
{
System.out.println("Hello Java");
}
}
• class -keyword is used to declare a class in Java.
• public -keyword is an access modifier that represents visibility. It means it
is visible to all.
• static -is a keyword. If we declare any method as static, it is known as the
static method. The core advantage of the static method is that there is no
need to create an object to invoke the static method. The main() method is
executed by the JVM, so it doesn't require creating an object to invoke the
main() method. So, it saves memory.
• void -is the return type of the method. It means it doesn't return any value.
• main -represents the starting point of the program.
• String[] args or String args[]- is used for command line argument. We
will discuss it in coming section.
• System.out.println() -is used to print statement. Here, System is a class,
out is an object of the PrintStream class, println() is a method of the
PrintStream class. We will discuss the internal working of
System.out.println() statement in the coming section.
Types of Java Applications
1) Standalone Application
– Standalone applications are also known as desktop
applications or window-based applications. These
are traditional software that we need to install on
every machine. Examples of standalone
application are Media player, antivirus, etc. AWT
and Swing are used in Java for creating standalone
applications.
• 2) Web Application
– An application that runs on the server side and
creates a dynamic page is called a web application.
Currently, Servlet, JSP, Spring, Hibernate, JSF, etc.
technologies are used for creating web
applications in Java.
• 3) Enterprise Application
– An application that is distributed in nature, such as
banking applications, etc. is called an enterprise
application. It has advantages like high-level
security, load balancing, and clustering. In Java,
EJB is used for creating enterprise applications.
• 4) Mobile Application
– An application which is created for mobile devices
is called a mobile application. Currently, Android
and Java ME are used for creating mobile
applications.