Introduction of OOP in java
Introduction of OOP in java
Java
• Java is a class-based object oriented programming language and a platform.
• Java is used to develop Mobile apps, Web apps, Desktop apps, Games, Robotics, Embedded
Applications, Enterprise Applications and much more.
• Differernt Java Platforms / Editions are available : Java SE, Java EE, Java ME, and JavaFx
• Companies who are using Java include; Google, Microsoft, Facebook, IBM, Amazon, Netflix, Pinterest,
Uber, JetBrains, Many more...
• 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.
The principles for creating Java
The principles for creating Java programming were
Simple,
Robust,
Portable,
Platform-independent,
Secured,
High Performance,
Multithreaded,
Architecture Neutral,
Object-Oriented,
Interpreted, and
Dynamic".
Java is −
• Object Oriented − In Java, everything is an Object. Java can be easily extended since it is based on the Object model.
• Platform Independent − Unlike many other programming languages including C and C++, when Java is compiled, it is
not compiled into platform specific machine, rather into platform independent byte code. This byte code is
distributed over the web and interpreted by the Virtual Machine (JVM) on whichever platform it is being run on.
• Simple − Java is designed to be easy to learn. If you understand the basic concept of OOP Java, it would be easy to
master.
• Secure − With Java's secure feature it enables to develop virus-free, tamper-free systems. Authentication techniques
are based on public-key encryption.
• 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.
• 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.
• High Performance − With the use of Just-In-Time compilers, Java enables high performance.
• Distributed − Java is designed for the distributed environment of the internet.
• Dynamic − Java is considered to be more dynamic than C or C++ since it is designed to adapt to an evolving
History of Java
1) Initially it was designed for small, embedded systems in electronic appliances like set-top boxes.
2) Firstly, it was called "Greentalk" by James Gosling
3) After that, it was called Oak and was developed as a part of the Green project.
4) Why the name Oak?
4) Oak is a symbol of strength and chosen as a national tree of many countries like the U.S.A., France,
Germany, Romania, etc.
5) In 1995, Oak was renamed as "Java" because it was already a trademark by Oak Technologies.
Why Java Programming named "Java"?
6) The team gathered to choose a new name. The suggested words were "dynamic", "revolutionary", "Silk",
"jolt", "DNA", etc. They wanted something that reflected the essence of the technology: revolutionary,
dynamic, lively, cool, unique, and easy to spell, and fun to say.
7) According to James Gosling, "Java was one of the top choices along with Silk". Since Java was so unique,
most of the team members preferred Java than other names.
8) Java is an island in Indonesia where the first coffee was produced (called Java coffee). It is a kind of espresso
bean. Java name was chosen by James Gosling while having a cup of coffee nearby his office.
9) Notice that Java is just a name, not an acronym.
10) Initially developed by James Gosling at Sun Microsystems (which is now a subsidiary of Oracle
Java Version History
• JDK Alpha and Beta (1995) • Java SE 9 (21st Sep 2017)
• JDK 1.0 (23rd Jan 1996) • Java SE 10 (20th Mar 2018)
• JDK 1.1 (19th Feb 1997) • Java SE 11 (September 2018)
• J2SE 1.2 (8th Dec 1998) • Java SE 12 (March 2019)
• J2SE 1.3 (8th May 2000) • Java SE 13 (September 2019)
• J2SE 1.4 (6th Feb 2002) • Java SE 14 (Mar 2020)
• J2SE 5.0 (30th Sep 2004) • Java SE 15 (September 2020)
• Java SE 6 (11th Dec 2006) • Java SE 16 (Mar 2021)
• Java SE 7 (28th July 2011) • Java SE 17 (September 2021)
• Java SE 8 (18th Mar 2014) • Java SE 18 (to be released by March 2022)
First Java Program
public class MyFirstJavaProgram {
/*
This is my first java program.
This will print 'Hello World' as the output
*/
2)The subscript notation in the Java array can be used after type, before the variable
or after the variable.
public static void main(String[] args)
public static void main(String []args)
public static void main(String args[])
3)You can provide var-args support to the main() method by passing 3 ellipses (dots)
public static void main(String... args)
Having a semicolon at the end of class is optional in Java
class A{
static public void main(String... args){
System.out.println("hello java4");
}
};
What happens at compile time?
At compile time, the Java file is compiled by Java Compiler (It does not
interact with OS) and converts the Java code into bytecode.
What happens at
runtime?
At runtime, these steps are performed:
• Classloader: It is the subsystem of JVM that is used to
load class files.
We will be looking into more details about modifiers in the next section
Java Variables
Following are the types of variables in Java −
• Local Variables
• 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.
• JVM performs the following main tasks: • JVM provides definitions for the:
• Loads code • Memory area
• Verifies code • Class file format
• Executes code • Register set
• Provides runtime environment • Garbage-collected heap
• Fatal error reporting etc.
Difference between JDK, JRE,
and JVM
JRE (Java Runtime Environment )
• 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.
• An implementation Its implementation is known as JRE (Java Runtime
Environment).
• The Java Runtime Environment is a set of software tools which are
used for developing Java applications.
• It contains a set of libraries + other files that JVM uses at runtime.
JVM Architecture
Let's understand the internal architecture of JVM. It contains classloader, memory area, execution engine etc.
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.
• 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 loades the jar files located
inside $JAVA_HOME/jre/lib/ext directory.
• 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 classloader.
Memory Areas
Class(Method) Area: Class(Method) Area stores per-class structures such as
the runtime constant pool, field and method data, the code for methods.
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.
Memory Areas
• Program Counter Register: PC (program counter) register contains the address of
the Java virtual machine instruction currently being executed.
• Native Method Stack: It contains all the native methods used in the application.
• Execution Engine: It contains:
• 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.
• Java Native Interface: Java Native Interface (JNI) is a framework which provides
an interface to communicate with another application written in another
language like C, C++, Assembly etc. Java uses JNI framework to send output to
the Console or interact with OS libraries.