Unit 12350001
Unit 12350001
UNIT 1
Overview of Java:
Characteristics of Java:
JVM, JRE, JDK
Parameters used in First Java Program
Basic Java Program Understanding
Java program Compilation and Execution Process
JVM as an interpreter and emulator
Instruction Set
class File Format
Security Promises of the JVM
Class loaders and security aspects
Sandbox model
UNIT 2
Java Fundamentals: Data Types, Literals, and Variables
Operators
Control of Flow
Classes and Instances
Inheritance
Throw and Throws clauses
User defined Exceptions
Applets
UNIT - 3
Threads
Runnable Interface
Thread Communication
AWT Components
Component Class and Container Class:
Layout Manager Interface and Default Layouts:
Insets and Dimensions:
Border Layout:
Flow Layout:
Card Layout:
Grid Bag Layout:
AWT Events:
Event Models:
JAVA 1
Listeners:
Class Listener:
Adapters:
Action Event Methods:
Focus Event Methods:
Key Event Methods:
Mouse Events:
Window Events:
UNIT - 4
Input/Output Streams:
Stream Filters:
Data Input and Output Stream:
Print Stream:
Random Access File:
JDBC (Database connectivity with MS-Access, Oracle, MS-SQL Server)
Object Serialization
Sockets
Development of client Server applications
Design of multithreaded server
Remote Method invocation
Java Native interfaces and Development of a JNI based application
Java Collection API Interfaces
UNIT 1
Overview of Java:
Characteristics of Java:
JAVA 2
1. Platform Independence: One of the key features of Java is its ability to run
on any platform that supports Java Virtual Machine (JVM). This platform
independence is achieved by compiling Java source code into bytecode,
which can be executed on any system with a compatible JVM.
3. Simple and Readable Syntax: Java has a straightforward syntax that is easy
to learn and read. It borrows many syntax conventions from languages like
C and C++, making it familiar to programmers from those backgrounds. The
language promotes clean and organized code through its syntax rules.
5. Rich Standard Library: Java comes with a vast standard library that provides
a wide range of pre-built classes and functions for common programming
tasks. This library includes utilities for input/output operations, data
structures, networking, multithreading, and more. Utilizing the standard
library saves development time and effort.
JAVA 3
8. Community and Ecosystem: Java has a large and active developer
community, which contributes to its rich ecosystem. There are numerous
frameworks, libraries, and tools available for Java development, making it
easier to build complex applications quickly.
Overall, Java is a powerful and versatile programming language known for its
platform independence, object-oriented approach, and robustness. It is widely
used in various domains and provides a solid foundation for building reliable and
scalable applications.
3. JDK: The JDK (Java Development Kit) is a software development kit that
includes tools, libraries, and the JRE. It is used by developers to create,
compile, and debug Java applications. The JDK contains the necessary
components for both development and execution of Java programs.
JAVA 4
public keyword is an access modifier that represents visibility. It means it is
visible to all.
void is the return type of the method. It means it doesn't return any value.
String[] args or String args[] is used for command line argument. We will
discuss it in coming section.
JAVA 5
Let's break down the different components of the program:
1. Import Statements:
The program begins with import statements that specify the classes from
the Java standard library that will be used in the program.
In this example, we import the Scanner class from the java.util package
to facilitate user input.
2. Public Class:
The class name should match the filename (excluding the .java
3. Main Method:
The main method is declared as public , static , and void , with a single
parameter of type String array ( String[] args ).
public indicates that the method can be accessed from outside the
class.
static means the method belongs to the class itself, rather than an
instance of the class.
void specifies that the method does not return any value.
4. Method Body:
Inside the main method, we create a Scanner object to read input from
the user.
5. User Input:
JAVA 6
Then we use the Scanner object's nextLine() method to read the user's
input and store it in the name variable of type String .
6. Output:
7. Scanner Closing:
To begin, you need to write your Java program's source code. This is
done in a plain text file with a .java extension. You can use any text
editor or integrated development environment (IDE) for this purpose.
The next step is to compile the Java source code into bytecode.
Bytecode is a platform-independent representation of your program that
can be executed by the Java Virtual Machine (JVM).
To compile the source code, you use the Java compiler (javac) that
comes with the Java Development Kit (JDK). In the command prompt or
terminal, navigate to the directory where your source code file is located
and execute the following command:
JAVA 7
javac YourProgram.java
If there are no errors in your code, the compiler will generate a bytecode
file with a .class extension. This file contains the compiled version of
your program.
When you run a Java program, you need to have a JVM installed on
your machine. The JVM takes care of loading and executing the
bytecode.
To run the compiled bytecode, you use the java command followed by
the name of the class containing the main method (the entry point of
your program).
java YourProgram
The JVM will start executing the bytecode, and your program will run
accordingly.
The Java Virtual Machine (JVM) is the cornerstone of the Java platform. It
provides a runtime environment for executing Java bytecode and plays a crucial
role in the platform's platform independence and security. Let's explore the
organization of the JVM:
1. Class Loader:
JAVA 8
as locating and loading the necessary class files from the file system,
network, or other sources.
The class loader is divided into three main components: the bootstrap
class loader, extension class loader, and application class loader. These
loaders work together to load classes based on the class's visibility and
dependency requirements.
The JVM defines several runtime data areas that are used during
program execution:
Heap: The heap is a runtime data area where objects are allocated.
It is divided into different regions and is the memory area managed
by the garbage collector for dynamic memory allocation and
deallocation.
Java Stack: Each thread in the JVM has its own Java stack, which is
used for method execution and storing local variables, method
parameters, and partial results. It also keeps track of method
invocations and returns.
Native Method Stack: The native method stack is used for executing
native code (code written in languages other than Java) and
handling interactions with the underlying operating system.
3. Execution Engine:
JAVA 9
performance. It optimizes the execution of the bytecode by
analyzing and translating it into efficient machine instructions.
The garbage collector identifies objects that are no longer reachable and
frees the memory occupied by them, ensuring efficient memory usage
and preventing memory leaks.
The Native Method Interface (JNI) allows Java code to interact with code
written in other programming languages, such as C or C++. It provides a
way to call native methods and exchange data between Java and native
code.
JAVA 10
1. JVM as an Interpreter:
The JVM interprets Java bytecode instructions one by one and executes
them. It reads the bytecode, understands the instruction, and performs
the corresponding operation. This interpretation happens at runtime.
2. JVM as an Emulator:
By providing this emulation layer, the JVM ensures that Java programs
can run consistently across different platforms without being directly
dependent on the underlying hardware and operating system.
It's important to note that the JVM combines interpretation and emulation with
the Just-In-Time (JIT) compilation technique to improve performance. The JIT
compiler dynamically compiles frequently executed bytecode into native machine
code, which can be directly executed by the CPU. This compilation process
optimizes the execution speed of the Java program by eliminating the
interpretation overhead.
JAVA 11
Instruction Set
The instruction set in the context of Java refers to the set of bytecode
instructions that the Java Virtual Machine (JVM) can execute. These instructions
are specified by the Java Virtual Machine Specification and are used to perform
various operations and control flow within a Java program.
Here are some common bytecode instructions found in the Java instruction set:
iand , ior , ixor : Perform bitwise AND, OR, XOR on two integers.
4. Object-oriented Instructions:
JAVA 12
invokevirtual , invokestatic , invokeinterface : Invoke methods on objects
or classes.
5. Array Instructions:
iaload , faload , aaload : Load an element from an array onto the stack.
These are just a few examples of the instructions available in the Java bytecode
instruction set. The JVM executes these instructions in a stack-based manner,
where operands are pushed onto a stack and operations are performed on the
top elements of the stack.
It's worth noting that Java bytecode instructions are designed to be platform-
independent and are executed by the JVM, which is responsible for translating
them into machine code specific to the underlying hardware architecture.
The class file starts with a fixed 4-byte magic number ( 0xCAFEBABE ) that
identifies it as a valid class file.
The version numbers (minor and major) indicate the version of the class
file format being used.
2. Constant Pool:
The constant pool section stores various constant values used by the
class, such as strings, class names, field names, method names, and
more.
JAVA 13
The class file format follows a specific structure and is designed to be platform-
independent. It allows the JVM to interpret and execute the bytecode on any
system that has a compatible JVM implementation.
It's important to note that the class file format is defined in the Java Virtual
Machine Specification and is subject to updates and revisions as new versions
of the JVM are released.
JAVA 14
1. Bytecode Structure: The verification process ensures that the bytecode
conforms to the structural rules defined by the JVM specification. It checks
for valid instructions, correct operand types, proper control flow, and
exception handling.
4. Stack Integrity: The verification process ensures that the bytecode's stack
operations, such as pushing and popping values, are well-formed and don't
violate the stack's integrity.
1. Class Structures: The Class Area stores information about classes and
interfaces loaded by the JVM. This includes the fully qualified names of the
classes, superclass and interface references, field information, method
bytecode, constant pool, and static variables.
3. Method Bytecode: The Class Area holds the bytecode instructions for
methods defined in the loaded classes. These instructions are interpreted or
compiled to machine code for execution.
JAVA 15
4. Static Variables: The Class Area also includes memory space for static
variables, which are shared among all instances of a class. Static variables
are initialized when the class is loaded and can be accessed without
creating an instance of the class.
The Class Area provides a centralized location for class-level information and
allows efficient sharing of data among multiple instances of the same class. It is
managed by the JVM's garbage collector, which handles the allocation and
deallocation of memory within the Class Area.
Java Stack:
The Java Stack is a region of memory used for method execution and storing
local variables and method call information. Here are some key points about the
Java Stack:
1. Stack Frames: Each method invocation creates a new stack frame, also
known as an activation record, on the Java Stack. The stack frame contains
information such as local variables, method parameters, return address, and
intermediate results.
4. Thread-Specific: Each thread in a Java program has its own stack. This
allows multiple threads to execute methods concurrently without interfering
with each other's stack frames.
The Java Stack is efficient for managing method calls and local variables
because of its simple and predictable nature. However, it has limited memory
compared to the Java Heap.
Java Heap:
The Java Heap is a region of memory used for dynamic memory allocation,
JAVA 16
specifically for objects and arrays. Here are some key points about the Java
Heap:
1. Object Storage: The Java Heap is where objects are allocated and
deallocated during the program's execution. Objects are created using the
"new" keyword and reside in the heap until they are no longer referenced.
4. Shared Among Threads: Unlike the Java Stack, the Java Heap is shared
among all threads in a Java program. Multiple threads can access and
modify objects stored in the heap simultaneously.
Garbage Collection:
Garbage Collection is the process of automatically reclaiming memory occupied
by objects that are no longer needed in a Java program. Here are some key
points about Garbage Collection:
2. Mark and Sweep: The most common garbage collection algorithm is the
Mark and Sweep algorithm. It works by traversing objects starting from the
root references (e.g., static variables, method call stacks) and marking
reachable objects. Unmarked objects are then swept and their memory is
reclaimed.
JAVA 17
based on object lifetimes. This allows for more efficient garbage collection by
focusing on short-lived objects in the young generation.
Every object is an instance of exactly one class, which does not change
through the life of the object.
If a field or method is marked private, then the only code that ever accesses
it is found within the class itself.
Fields and methods marked protected are used only by code that
participates in the implementation of the class.
Security Architecture:
The security architecture of the JVM encompasses the design principles,
mechanisms, and features that ensure the security of Java applications. It
JAVA 18
provides a layered approach to protect the integrity, confidentiality, and
availability of resources within the JVM. Here are key aspects of the JVM's
security architecture:
2. Security Providers: The JVM allows the integration of security providers that
offer cryptographic algorithms, secure protocols, and other security-related
services. These providers implement the Java Cryptography Architecture
(JCA) and Java Cryptography Extension (JCE) to provide cryptographic
functionalities such as encryption, digital signatures, and secure random
number generation.
4. Class Loading and Sandboxing: The JVM's class loading mechanism and
sandboxing model contribute to its security architecture. Class loading
ensures that classes are loaded from trusted sources and verified before
execution. The sandboxing model restricts untrusted code by isolating its
execution environment, preventing unauthorized access to resources and
system operations.
Security Policy:
The security policy in the JVM defines the permissions and access controls for
Java applications. It specifies the rules and restrictions that determine what
operations an application can perform and what resources it can access. The
JAVA 19
security policy is typically defined in a security policy file, which is loaded by the
JVM during application startup.
When a class needs to be loaded, the class loader first delegates the
task to its parent. If the parent class loader cannot find the class, the
current class loader attempts to load it.
3. Security Constraints:
The Java Security Manager, along with the security policy, defines the
permissions and restrictions for different code sources. Class loaders
are responsible for implementing these security policies by loading
classes in accordance with the defined constraints.
JAVA 20
The Security Manager, as part of the JVM's security architecture,
interacts with class loaders to enforce security policies and permissions.
Class loaders assist in this process by ensuring that classes and code
sources adhere to the security policies defined by the Security Manager.
Java allows the creation of custom class loaders that can be used to
extend or modify the default class loading behavior.
Sandbox model
The sandbox model is a security mechanism implemented in the Java Virtual
Machine (JVM) to restrict untrusted code from accessing sensitive resources
and performing potentially harmful operations. It creates a controlled execution
environment for untrusted code, ensuring that it operates within predefined
boundaries. Here are the key aspects of the sandbox model:
JAVA 21
2. Access Control: The sandbox model enforces access control policies to
regulate the interactions between the untrusted code and the system
resources. It specifies what resources the code can access, such as the file
system, network, or system properties. By controlling these access rights,
the sandbox model limits the potential damage that untrusted code can
cause.
4. Security Policies: The security policies in the sandbox model define the
permissions and restrictions for untrusted code. These policies are typically
specified in a security policy file, which is loaded by the JVM. The policies
can specify the allowable actions, such as reading or writing files, creating
network connections, or accessing certain system properties. The Security
Manager uses these policies to determine whether the requested actions are
permitted.
JAVA 22
accessing protected resources, or executing native code. By confining the
code within the sandbox, the model provides a layer of protection against
malicious actions.
The sandbox model is particularly useful when executing untrusted code, such
as applets in web browsers or code from unknown sources. It allows for the
execution of potentially risky code while mitigating the risks and maintaining the
overall security of the system.
It's worth noting that the effectiveness of the sandbox model relies on proper
configuration, adherence to security best practices, and regular updates to
address potential vulnerabilities in the JVM.
JAVA 23