JAVA
JAVA
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:
• 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
Java is a class-based, object-oriented programming language that is designed
to have as few implementation dependencies as possible. It is intended to let
application developers Write Once and Run Anywhere (WORA), meaning that
compiled Java code can run on all platforms that support Java without the need
for recompilation.
Java was developed by James Gosling at Sun Microsystems Inc. in May
1995 and later acquired by Oracle Corporation and is widely used for
developing applications for desktop, web, and mobile devices.
Java is known for its simplicity, robustness, and security features, making it a
popular choice for enterprise-level applications. Java applications are compiled
to byte code that can run on any Java Virtual Machine. The syntax of Java is
similar to C/C++.
Java makes writing, compiling, and debugging programming easy. It helps to
create reusable code and modular programs.
Characteristics of Java
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.
2. Object-Oriented Programming (OOP): Java is designed around the
principles of object-oriented programming, which allows developers to model
real-world entities as objects and organize code into reusable and modular
components. Concepts like classes, objects, inheritance, and polymorphism are
integral to Java's OOP paradigm.
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.
4. Robustness and Memory Management: Java's robustness is achieved
through features like exception handling, automatic memory management
(garbage collection), and strong type checking. Exceptions help in dealing with
errors and exceptions that may occur during program execution, while garbage
collection relieves developers from manually managing memory allocation and
deallocation.
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.
6. Security: Java emphasizes security and provides built-in mechanisms
for secure programming. It includes features like bytecode verification,
sandboxing, and security managers, which help in creating secure and trusted
applications.
7. Scalability and Performance: Java's scalability and performance have
improved over the years. The Just-In-Time (JIT) compiler optimizes bytecode at
runtime, translating it into machine code for efficient execution. Additionally,
Java supports multi-threading, enabling developers to write concurrent and
efficient programs.
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.
JVM, JRE, JDK
1. JVM: The JVM (Java Virtual Machine) is a crucial component of the
Java platform. It provides a runtime environment for executing Java bytecode.
The JVM interprets or compiles Java bytecode into machine code and handles
memory management, security, and other runtime functionalities. It ensures
that Java programs are platform-independent, as bytecode can be executed on
any system with a compatible JVM.
It It It runs the
includes includes java byte code
development libraries to run and make java
tools like Java application to
(compiler) + application + work on any
Includes JRE JVM platform.
Running Convert
Writing
a Java bytecode into
and compiling
application on native machine
Java code
Use Case a system code
The compilation and execution process in Java involves several steps. Let's break it
down:
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:
3. Java Virtual Machine (JVM):
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).
accordingly.
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:
The JVM can also be considered an emulator in the sense that it emulates a
virtual computing environment in which the Java program runs. It creates a
virtual representation of the underlying hardware and provides a runtime
environment for executing Java programs
Instruction Set
An instruction set in Java refers to the low-level operations that the Java Virtual
Machine (JVM) can execute.
A Class Loader in Java is like a delivery person that brings Java classes into
memory when needed. It loads .class files so the Java program can use
them.
3. Bytecode Verification
o Before loading a class, Java checks if it follows security rules.
1. Trusted Code (Your computer) → Full access to files, network, and system.
WRAPPER CLASSES:
In Java, Wrapper Classes are used to convert primitive data
types into object.
Java has 8 primitive data types (int, char, float, etc.). But
sometimes, we need objects instead of primitives.
What is an Array?
An array is like a box that holds multiple values of the same
type. Instead of creating separate variables, we store them in
one single variable.
EXAMPLE:
int num1 = 10;
int num2 = 20;
int num3 = 30;
int[] numbers = {10, 20, 30};
Strings in Java
A String is a sequence of characters (text). It is an object in Java.
EXAMPLE:
String name = "Alice";
OPERATORS:
Operators in Java are symbols that perform operations on values or
variables. Think of them as tools that help in calculations and logic.
1. Types of Operators in Java:
ARITHMETIC OPERATORS:
ASSIGNMENT OPERATORS:
COMPARISON OPERATORS:
LOGICAL OPERATORS:
BITWISE OPERATORS:
2. LOOPS :
3.JUMP STATEMENTS:
Break statement: Terminates the execution of a loop or switch
statement and transfers control to the next statement after the
loop or switch.
Continue Statement: (Skips one loop iteration).
Return : (Exits the method).
Blue Car).
EXAMPLE:
class Car {
String color; // Property
int speed; // Property
Inheritance in Java
Inheritance means one class (child) can use the properties & methods
of another class (parent).
Example: Think of Family Inheritance
Father (Parent Class) has a house.
// Main class
public class Main {
public static void main(String[] args) {
Dog myDog = new Dog(); // Creating an object of Dog
myDog.eat(); // Inherited from Animal
myDog.bark(); // Defined in Dog class
}
}
Working :
Animal is the parent class (also called superclass).
Dog is the child class (also called subclass) that inherits eat() from Animal.
Dog can use both eat() (from parent) and bark() (its own method)
TYPES OF INHERITANCE
// Parent class
class Animal {
void eat() {
System.out.println("Eating...");
}
// Child class inheriting from
Animal
void bark() {
System.out.println("Barking...");
public class
SingleInheritanceExample {
d.eat(); // Inherited
method from Animal class
}
Multilevel Inheritance : class C extends B {}
// Grandparent class
class Animal {
void eat() {
System.out.println("Eating...");
void bark() {
System.out.println("Barking...");
void weep() {
System.out.println("Weeping...");
}
// Main class to test multilevel
inheritance
public class
MultilevelInheritanceExample {
p.weep(); // Method of
Puppy class
}
: class B extends A,
Hierarchical Inheritance
class C extends A
// Parent class
class Animal {
void eat() {
System.out.println("Eating...");
}
// Child class 1 inheriting from Animal
void bark() {
System.out.println("Barking...");
void meow() {
System.out.println("Meowing...");
public class
HierarchicalInheritanceExample {
NOTE.
Example:
interface A {
System.out.println("Interface A");
interface B {
System.out.println("Interface B");
class C implements A, B {
obj.show();
HYBRID INHERITANCE:
Hybrid inheritance is a combination of more than one type of inheritance
(e.g., multilevel + multiple).
EXAMPLE:
class Test {
} else {
}
public static void main(String[] args) {
EXAMPLE:
class Test {
super(message);
// Main Class
} else {
System.out.println("Eligible to vote.");
}
try {
checkAge(age);
} catch (InvalidAgeException e) {
StringBuffer in Java
Creating a StringBuffer:
Java Applets
An applet is a small Java program that runs inside a web browser or an
applet viewer.
Applets are used to add interactive features to web pages.
JAVA LIFECYCLE:
<html>
<body>
<applet code="HelloWorldApplet.class" width="300"
height="150">
</applet>
</body>
</html>
THE END OF UNIT-2