OOP with java unit 1
OOP with java unit 1
Chapter-1
Introduc on to JAVA
Introduction
Desktop
Application
Java
Database
Connections
With widespread recognition and a strong presence in the tech job market, Java
remains one of the most in-demand programming languages worldwide. It is lauded for its
Object Oriented Programming With JAVA
ease of learning and user-friendly nature. As an open-source language, Java is accessible and
free, offering a secure, fast, and powerful development environment. Supported by a vibrant
community of millions of developers, Java is employed in creating a vast array of applications
and software solutions.
History of Java
In 1991, James Gosling, along with his colleagues at Sun Microsystems Lab, invented
Java. Originally conceived for interactive television, Java was ahead of its time, offering
complexity that the market wasn't ready to embrace. Initially called "Oak," named after an oak
tree outside Gosling's office, the project was eventually renamed "Java" inspired by Java coffee.
The innovation's journey began with the Green Team, a group of pioneering engineers
developing this sophisticated language for digital devices like televisions and set-top boxes.
Although the original idea was innovative for its time, Java found its true potential in
online programming. This led to its integration by Netscape, marking the beginning of Java's
widespread adoption. Today, Java is a cornerstone in diverse applications such as e-commerce,
gaming, mobile technology, and web development. Key milestones in Java's evolution include:
1. The Birth of Java: In June 1991, James Gosling, Mike Sheridan, and Patrick Naughton
initiated the Java language project. They were part of the Green Team, a small group focused
on solar engineering solutions.
Object Oriented Programming With JAVA
2. Early Design Focus: Java was initially designed for embedded systems in electronic gadgets
like set-top boxes.
3. Initial Naming: The language was first called "Greentalk," represented by the file
extension.gt.
4. Evolution and Growth: After its early development, Java was rebranded from "Oak" to
"Java". Since its introduction in the mid-1990s, Java has undergone significant evolution. Each
version brings new features, improved security measures, and enhanced performance.
As of now, multiple Java versions have been released, each contributing to the language's
robust functionality and staying power in the tech industry.
Features of Java
Java offers a range of features that make it a popular choice for developers. Below, we
detail the key features of Java, emphasizing its benefits and strengths.
Simple
Object
Interpreted
oriented
Dynamic Distributed
Plateform
Portable
independent
High
Rubust
performance
Secured
write reusable, scalable, and maintainable code by organizing data and behaviours into
objects.
6.Portable-Java's portability is unparalleled. Once written, Java code can run on any
device with a JVM, as it is compiled into hardware-independent bytecode (.class files). This
cross-platform capability is a significant advantage over other programming languages.
Features of JVM
Java's JVM architecture comprises essential components such as a class loader, memory
space, and an execution engine, among others. The key operations performed by the JVM
include:
The image illustrates the Java Development Kit (JDK) workflow, emphasizing the Java Class
Loading and Execution Process within the Java Virtual Machine (JVM). In the given figure
detailed analysis of the components depicted in the image:
1. Class Loader-A Class Loader in Java is a critical component of the Java Runtime Environment
(JRE) that dynamically loads Java classes into memory during runtime. Following the
Delegation Hierarchy Model, it plays a vital role in the dynamic linking of classes. The Class
Loader efficiently performs three main functions: loading, linking, and initialization. This
process ensures that Java applications run smoothly by managing class loading seamlessly. By
understanding the role of a Class Loader, Java developers can optimize application
performance and enhance runtime efficiency.
The Class Loader is a crucial component in the Java execution process. Here's how it functions:
- It loads .class files (bytecode) into memory whenever a Java program is executed.
- Java offers several types of class loaders:
a) Bootstrap Class Loader: This loader handles the core Java classes, loading them
from the rt.jar or module path.
b) Extension Class Loader: Responsible for loading classes from the lib/ext directory.
The Class Loader ensures that all necessary classes are readily available for execution, playing
a vital role in running Java applications smoothly.
Object Oriented Programming With JAVA
Class loader
loading linking Ini a on
JNI Java Na ve
Na ve Method
Interface Library
2. Bytecode Verifier
When executing a Java program, the Java Virtual Machine (JVM) handles memory
dynamically by dividing it into various runtime memory areas. These areas are essential for
storing code, variables, objects, and methods during program execution.
One key component, the JVM Method Area, is responsible for housing class structures,
including metadata, the constant runtime pool, and method code.
a) Bytecode Loading and Verification: After the Java program loads, its bytecode enters
the Bytecode Verifier.
b) Security and Integrity Checks: The Bytecode Verifier ensures the security and integrity
of the program by:
a. Preventing illegal memory access.
b. Ensuring no stack underflows or overflows occur.
c. Verifying compliance with Java’s safety rules.
If the bytecode verification fails, a VerifyError is thrown, effectively preventing the execution of
potentially harmful code.
Object Oriented Programming With JAVA
3. Execution Engine
The Just-In-Time (JIT) Compiler significantly enhances performance and is a vital part of
the Java Virtual Machine (JVM), which executes bytecode. It processes .class files by
interpreting and transforming them into machine code that the CPU can execute efficiently.
After verification, the Execution Engine handles the bytecode conversion. Key Components of
the Java Virtual Machine:
a) Interpreter: Executes Java bytecode line by line, resulting in slower execution speeds.
b) JIT Compiler: Transforms bytecode into native machine code for faster execution,
boosting overall performance.
c) Garbage Collector: Automatically eliminates unused objects to free up memory. In Java,
objects are dynamically created in the Heap memory using the new keyword. However,
unused objects consume memory and may lead to an OutOfMemoryError. The Garbage
Collector efficiently identifies and clears these objects. The heap memory stores all objects
and their associated instance variables and arrays, shared by multiple threads.
For optimal JVM performance, understanding and leveraging these components, including the
JIT Compiler and Garbage Collector, is essential. By doing so, developers can ensure efficient
memory management and execution speed in Java applications.
Java Virtual Machine (JVM): The Java Virtual Machine (JVM) is a powerful engine
that creates a runtime environment for running Java code and applications. Its primary function
is to transform Java bytecode into machine language, allowing it to execute on any device with
a compatible JVM installation. Although termed as a "machine," the JVM exists solely within
the computer's memory. The process it follows includes these key steps:
1. Compiling Source Code: The javac compiler converts the high-level Java source code into
bytecode, which is a machine-readable format.
2. Executing Bytecode: A Java interpreter then takes this bytecode and translates it into
human-understandable instructions, allowing the program to run seamlessly.
Object Oriented Programming With JAVA
Classloader
ByteCode verifier
Interpreter
Run-Time
Hardware
Discover the comprehensive software package essential for running Java applications:
a) Bytecode Verifier-The Bytecode Verifier plays a crucial role in ensuring the integrity of
Java applications. It meticulously checks the format and accuracy of Java code before it
reaches the interpreter. If any code compromises system integrity or breaches access
rights, the class is deemed corrupt and will not be loaded, ensuring a secure runtime
environment.
b) Interpreter-Once the bytecode is successfully loaded, the Java Interpreter takes charge. It
constructs an object of the Java Virtual Machine (JVM), enabling the Java program to run
seamlessly on the host machine. This process ensures that your Java applications execute
natively and efficiently.
c) ClassLoader-The Java ClassLoader is a dynamic component responsible for loading all
necessary classes required for running a Java program. Enhancing efficiency, classes are
only loaded into memory when needed. This automated process is managed by the
ClassLoader, optimizing resources. During JVM initialization, three primary ClassLoaders
are activated:
1) Bootstrap ClassLoader: The foundational loader that loads core Java libraries.
2) Extensions ClassLoader: Handles the loading of standard Java extensions.
3) System ClassLoader: Manages classes specified by the user's classpath.
The Java Development Kit (JDK) is an essential software development kit for developers
looking to create, compile, and run Java applications. As a cornerstone in Java development,
the JDK encompasses the Java Runtime Environment (JRE) along with additional tools
necessary for efficient Java programming, including the compiler (javac), debugger, and a suite
of utilities.
Components
4. Development Tools – It includes tools like javadoc, javap, jdb (Java Debugger).
5. Java Standard Libraries (API) – It includes pre-built classes (e.g., java.lang, java.util,
java.io).
JDK
JRE
JVM
Developers can choose between multiple versions provided by Oracle, such as the Oracle JDK,
which requires a commercial license for production but remains free for development
purposes, and the OpenJDK, which is open-source and free. The JDK is indispensable for Java
developers crafting various applications, whether for desktop, web, mobile, or enterprise
environments.
Integral to Java development, the JDK includes a comprehensive collection of tools designed
specifically for developing and executing Java programs. Notable tools within the JDK include:
Appletviewer The appletviewer is a web browser that is used to run java applets.
It is java interpreter that can analyse and executes your program line by
Java
line.
Javac It is a java compiler that can analyse and executes your whole program.
It is java disassembler that is used to translate machine code into a human
Javap
readable format.
It is used to generate C header (.h) files from Java classes and other
javah
programming files.
It generates HTML documentation for web-based applications from Java
Javadoc
source code.
jdb It is java debugger it is used in debugging the existing errors.
These three are the core components of the Java platform, but they serve different purposes
in the Java lifecycle. Let’s dive deeper into the differences and roles of JVM, JRE, and JDK with
a detailed explanation.
Object Oriented Programming With JAVA
JRE
JVM JDK
Feature (Java Runtime
(Java Virtual Machine) (Java Development Kit)
Environment)
Runs compiled Java Provides environment to For developing, compiling,
Installation
programs(bytecode) on execute Java applications. and running Java
Purpose
any platform. programs.
Java Virtual Machine JVM + Java libraries. JRE + Development tools
Contains
(JVM) Only (compiler, debugger, etc.)
The Java Environment consists of various components that work together to write, compile,
and execute Java programs. The key elements of the Java environment are JDK (Java
Development Kit), JRE (Java Runtime Environment), and JVM (Java Virtual Machine). To set up
Java Development Environment you will need:
To Verify your Java Installation Run the following commands in the terminal or command
prompt If Java is installed correctly, it will display the installed version:
Applications of Java
In mobile app development, Java plays a pivotal role as the foundation for Android
applications. Officially supported by Android Studio, Java's extensive libraries and active
community make it a popular choice for developing a wide array of Android apps, including
Object Oriented Programming With JAVA
those in banking and social media. Whether for creating a seamless e-commerce experience
or powering the latest social media platform, Java continues to be a top choice for developers
worldwide.
Java Applications
Java plays a pivotal role in web development by powering dynamic websites and
1 applications. Popular Java frameworks such as Spring, Hibernate, Struts, and
JavaServer Faces (JSF) are often employed to streamline the development process.
For mobile app development, particularly Android applications, Java remains a
dominant choice. With tools like Android SDK and Android Studio, developers can
2
create feature-rich and performant Android apps, capitalizing on Java’s reliability and
extensive community support.
In the realm of enterprise applications, Java brings stability and security, making it a
top choice for industries such as banking, healthcare, insurance, and logistics.
3 Frameworks like Spring Boot and Java EE (Enterprise Edition) are commonly used to
build scalable and secure enterprise solutions that can handle complex business
processes.
Java’s cross-platform nature is perfect for creating desktop applications, utilizing GUI
frameworks such as JavaFX, Swing, and AWT. Developers frequently use Integrated
4
Development Environments (IDEs) like IntelliJ IDEA, Eclipse, and NetBeans to enhance
their programming efficiency and productivity.
Java's scalability makes it ideal for developing cloud-based applications. Leading cloud
platforms such as Amazon Web Services (AWS), Google Cloud, and Microsoft Azure
5
host Java applications, which are often deployed as Software as a Service (SaaS) and
Platform as a Service (PaaS) solution.
Java is integral to big data and real-time analytics, processing massive datasets
efficiently with frameworks like Apache Hadoop and Apache Spark. These tools enable
6
businesses to derive valuable insights from data, bolstering informed decision-making
processes.
In the Internet of Things (IoT) landscape, Java is utilized for developing applications for
7 embedded systems and smart devices, including smart homes, wearables, and
industrial automation.
Java's versatility extends to game development across desktop, online, and mobile
8 platforms. Notable games like Minecraft illustrate Java’s capability to support rich
graphics and interactive experiences.
Java plays a crucial role in artificial intelligence (AI) and machine learning applications,
including chatbots, natural language processing (NLP), and image recognition.
9
Popular Java libraries, like DeepLearning4J and Weka, support these functions, making
Java an essential language for AI development.
Java is widely used in network security and data protection. It is instrumental in
implementing secure authentication and encryption methods to safeguard sensitive
10
information. With its versatility and robust security features, Java remains a top choice
for developers in both AI and cybersecurity fields.