Chapter-1 (Introduction To Java)
Chapter-1 (Introduction To Java)
Introduction
Object-Oriented Programming (OOP) is a programming paradigm based on the
concept of "objects," which are instances of classes. These objects encapsulate data
(attributes) and behaviors (methods) into a single entity. OOP emphasizes principles
like encapsulation, inheritance, polymorphism, and abstraction to create modular,
reusable, and scalable software.
This approach models real-world entities and their interactions, making it intuitive and
efficient for designing complex systems.
1. Class:
A class is a user-defined data type. It consists of data members and
member functions, which can be accessed and used by creating an instance of
that class. It represents the set of properties or methods that are common to all objects
of one type. A class is like a blueprint for an object.
For Example: Consider the Class of Cars. There may be many cars with different
names and brands but all of them will share some common properties like all of them
will have 4 wheels, Speed Limit, Mileage range, etc. So here, Car is the class, and
wheels, speed limits, mileage are their properties.
2. Object:
It is a basic unit of Object-Oriented Programming and represents the real-life
entities. An Object is an instance of a Class. When a class is defined, no memory is
allocated but when it is instantiated (i.e. an object is created) memory is allocated. An
object has an identity, state, and behaviour. Each object contains data and code to
manipulate the data. Objects can interact without having to know details of each
other’s data or code, it is sufficient to know the type of message accepted and type of
response returned by the objects.
For example “Dog” is a real-life Object, which has some characteristics like color,
Breed, Bark, Sleep, and Eats.
Object
3. Data Abstraction:
Data abstraction is one of the most essential and important features of object-
oriented programming. Data abstraction refers to providing only essential
information about the data to the outside world, hiding the background details
or implementation. Consider a real-life example of a man driving a car. The man
only knows that pressing the accelerators will increase the speed of the car or applying
brakes will stop the car, but he does not know about how on pressing the accelerator
the speed is increasing, he does not know about the inner mechanism of the car or the
implementation of the accelerator, brakes, etc in the car. This is what abstraction is.
4. Encapsulation:
Encapsulation is defined as the
wrapping up of data under a single unit. It
is the mechanism that binds together code
and the data it manipulates. In
Encapsulation, the variables or data of a class
are hidden from any other class and can be
accessed only through any member function
of their class in which they are declared. As in
encapsulation, the data in a class is hidden
from other classes, so it is also known as data-
hiding.
6. Polymorphism:
The word polymorphism means having many forms. In simple words, we can
define polymorphism as the ability of a message to be displayed in more than one
form. For example, A person at the same time can have different characteristics. Like
a man at the same time is a father, a husband, an employee. So the same person
possesses different behaviour in different situations. This is called polymorphism.
7. Dynamic Binding:
In dynamic binding, the code to be executed in response to the function call is
decided at runtime. Dynamic binding means that the code associated with a given
procedure call is not known until the time of the call at run time. Dynamic Method
Binding One of the main advantages of inheritance is that some derived class D has
all the members of its base class B. Once D is not hiding any of the public members
of B, then an object of D can represent B in any context where a B could be used. This
feature is known as subtype polymorphism.
UNIT-1
CHAPTER- 1
INTRODUCTION TO JAVA
JAVA
Java is a high-level, object-oriented, platform-independent programming language
developed by James Gosling at Sun Microsystems (now owned by Oracle
Corporation) in 1995. It is designed to be simple, robust, secure, and portable,
enabling developers to create software applications that can run on any device or
operating system equipped with a Java Virtual Machine (JVM).
Features of JAVA
1. Simple
Java is designed to be easy to learn and use. Its syntax is clear and simple,
influenced by C and C++ but eliminating many of the complex features of those
languages, like pointers, operator overloading, and multiple inheritance in
classes.
No Pointers: Java does not allow direct memory manipulation, which simplifies
memory management and reduces errors such as memory leaks.
Automatic Garbage Collection: Java has an automatic garbage collection
system that reclaims memory by removing unused objects, making memory
management easier and less error-prone.
2. Object-Oriented
Everything in Java is treated as an object, which makes it easier to manage
and scale complex software systems.
Main Principles of OOP:
o Encapsulation: Bundling the data (variables) and methods (functions)
that operate on the data into a single unit, a class.
o Inheritance: A mechanism where one class acquires the properties and
behaviors of another class.
o Polymorphism: The ability for one method or class to take many forms
(e.g., method overloading, method overriding).
o Abstraction: Hiding the complex implementation details and showing
only the essential features of an object.
OOP makes Java code more modular, reusable, and easier to maintain.
3. Platform-Independent
One of Java's most significant features is its ability to run on any platform. Java
follows the "Write Once, Run Anywhere" (WORA) philosophy.
Compilation into Bytecode: Java code is compiled into an intermediate form
called bytecode (.class files), not machine code.
The bytecode can be executed on any system that has a Java Virtual Machine
(JVM), which interprets the bytecode into platform-specific machine code.
This allows Java programs to run on any device or operating system (Windows,
Linux, macOS, etc.) without modification.
4. Secure
Java has this feature since it provides several built-in mechanisms that protects
against vulnerabilities like memory corruption, unauthorized access and
malicious code execution. Java is said to be secure for the following reasons:
o No Direct Memory Access: Unlike C or C++, Java does not allow direct
memory manipulation, which prevents many types of security
vulnerabilities (e.g., buffer overflows).
o Bytecode Verification: Java verifies bytecode before it is executed,
ensuring that malicious or harmful code does not run.
o Class Loaders: These are part of JRE. These control which classes can
be loaded into the Java environment, providing further control over
security.
o Security Manager: The security manager checks the permissions of the
application and can restrict access to system resources, like file I/O and
network connections, preventing unauthorized access.
5. Robust
Java is designed to be reliable and resilient (the ability of a system to continue to
operate and recover from failures, disruptions, or stress). It has built-in features
that help prevent errors and crashes:
o Exception Handling: Java provides robust error-handling capabilities
through the use of exceptions. Errors are managed using try-catch blocks,
ensuring the program can continue to run even after encountering issues.
o Automatic Garbage Collection: Java automatically handles memory
management and cleans up unused objects, reducing the likelihood of
memory leaks.
o Type Checking: Java is statically typed, meaning the type of every
variable is checked at compile-time. This prevents many common runtime
errors.
6. Multithreaded
Java has built-in support for multithreading (Thread: Threads are lightweight
subprocesses, representing the smallest unit of execution with separate paths.
Multithreading: is a Java feature that allows concurrent execution of two or
more parts of a program for maximum utilization of CPU), which means that
multiple threads can run concurrently within a program.
This is particularly useful in applications that require multiple tasks to be
performed simultaneously, such as:
o Web servers managing multiple client requests
o Games with multiple players
o Real-time applications like chat applications
Java provides a Thread class and the Runnable interface for creating and
managing threads, making it easier to write highly efficient, responsive
applications.
7. Portable
Java is highly portable, which is one of its main advantages.
Java programs are compiled into platform-independent bytecode, which can be
run on any system with a JVM.
Since Java avoids platform-specific code, a program written on one platform
(e.g., Windows) can run on other platforms (e.g., Linux or macOS) without
modification, as long as a JVM is available.
8. High Performance
While Java is an interpreted language, it uses a Just-In-Time (JIT) compiler to
convert bytecode into machine code at runtime, improving the performance of
Java applications.
The JVM optimizes the execution of bytecode to improve the program's
performance as it runs, making Java faster than other interpreted languages like
Python.
9. Distributed
Java is built with support for distributed computing, meaning applications can
communicate and work across networks.
Java's Distributed Features:
o Remote Method Invocation (RMI): Allows objects to communicate
across a network as if they were local.
o Enterprise JavaBeans (EJB): A server-side component architecture for
distributed, transactional, and secure applications.
o Sockets and Networking: Java provides built-in classes for creating and
managing network connections and interacting with other systems over
the internet.
10. Dynamic
Java is a dynamic language, meaning that it can adapt to changing conditions
and environments during runtime.
Dynamic Loading of Classes: Java allows classes to be loaded into the program
at runtime (using reflection and class loaders), which makes the system flexible
and extensible.
Java also supports dynamically adding or removing libraries, which allows
developers to modify or extend applications without recompiling the entire
system.
11. Architecture-Neutral
Java bytecode is designed to be architecture-neutral, meaning the compiled code
does not depend on the machine's hardware architecture.
The JVM handles the translation of bytecode into native machine code specific
to the hardware and operating system of the machine it's running on.
12. Interpreted
Java programs are interpreted by the JVM, which provides platform
independence.
The JVM translates bytecode into machine code during execution. This
interpretation allows for the portability of Java programs across different
systems.
C generally breaks down into functions. Java breaks down into Objects.
C does not contain the property called Java contains the property of
Inheritance because it does not support Inheritance which is very useful in
OOPS, which is very useful for code code reusability.
C Java
Union and structure datatypes are supported Java does not supports union and
by C. structures.
C Java
Components of JRE:
1. Java Virtual Machine (JVM):
o The JVM is the core component of the JRE responsible for executing Java
bytecode.
o It translates the platform-independent Java bytecode into machine-
specific code and handles tasks like:
Memory management.
Garbage collection.
Exception handling.
o Ensures that Java follows the Write Once, Run Anywhere (WORA)
philosophy.
2. Core Java Libraries:
o The JRE includes a rich set of pre-built class libraries (Java API) that
provide common functionalities, such as:
Collections (e.g., ArrayList, HashMap).
Input/Output (I/O) operations (e.g., file handling, networking).
Database connectivity (e.g., JDBC).
Utilities for string manipulation, date/time handling, etc.
o These libraries are essential for running Java applications, as they provide
pre-written code for common tasks.
3. Java Class Loader:
o The class loader is responsible for loading Java classes into the JVM at
runtime.
o It ensures that the required classes and resources are loaded dynamically
during program execution.
The Java Runtime Environment (JRE) is a runtime environment that includes the
JVM, core libraries, and tools required to execute Java applications. It ensures the
efficient running of Java programs across different platforms.
Lifecycle of JVM:
1. Loading: The Class Loader loads the Java bytecode into memory.
2. Linking: Classes are verified, prepared, and resolved to ensure correctness and
compatibility.
3. Initialization: The JVM initializes classes and their static variables.
4. Execution: The Execution Engine interprets or compiles bytecode and runs the
program.
Advantages of JVM:
Platform Independence: Ensures that Java programs can run on any system
with a JVM.
Memory Management: Handles memory allocation and garbage collection
automatically.
Security: Provides a secure environment with features like class verification and
sandboxing.
Optimized Performance: With the JIT Compiler, the JVM optimizes
performance during runtime.
The Java Virtual Machine (JVM) is the heart of Java's runtime environment,
providing a platform-independent, secure, and efficient execution of Java programs. It
interprets and compiles Java bytecode, manages memory, and ensures the "Write Once,
Run Anywhere".
Advantages of Java:
1. Platform Independence:
o Java's "Write Once, Run Anywhere" (WORA) capability allows
programs to run on any platform with a compatible JVM, making it
highly portable.
2. Object-Oriented:
o Java follows the Object-Oriented Programming (OOP) model, making
code modular, reusable, and easier to understand.
3. Rich Standard Libraries:
o Java provides extensive built-in libraries (e.g., Collections, I/O,
Networking, etc.) that simplify development.
4. Security:
o Java has built-in security features such as bytecode verification, a secure
class loader, and a Security Manager, ensuring robust application
security.
5. Automatic Memory Management:
o Java has a Garbage Collector that automatically handles memory
allocation and deallocation, reducing the risk of memory leaks.
6. Multi-threading Support:
o Java supports multi-threading, allowing developers to build applications
with concurrent processes for better performance.
7. Robust and Reliable:
o Java's strong memory management, exception handling, and type-
checking mechanisms make it a robust language for building stable
applications.
8. Community Support:
o Java has a massive community of developers, making it easy to find
help, resources, and tools.
9. Scalability:
o Java is suitable for developing small-scale applications to large,
distributed enterprise systems.
10. Wide Usage:
o Java is used across various domains, including web development,
mobile applications (via Android), enterprise systems, and cloud
computing.
Disadvantages of Java:
1. Performance Issues:
o Java programs are generally slower than programs written in languages
like C or C++ because of the JVM and abstraction layers.
2. Memory Consumption:
o Java applications use more memory due to features like garbage
collection and runtime checks, which may be inefficient for resource-
constrained systems.
3. Verbose Syntax:
o Java code tends to be more verbose, which can lead to longer and more
complex programs compared to other modern languages.
4. Lack of Low-Level Programming:
o Java abstracts hardware-level operations, which makes it less suitable
for tasks requiring direct system-level access (e.g., device drivers).
5. Startup Time:
o Java applications often have a longer startup time because the JVM
needs to initialize before execution.
6. Dependency on JVM:
o Java applications cannot run without a properly installed and configured
JVM, creating a dependency on the environment.
7. GUI Development:
o Although Java supports GUI development (Swing, JavaFX), it is less
favored compared to modern tools and frameworks like React or Flutter
for building user interfaces.
Summary Table:
Advantages Disadvantages
*********************************************************************