INTRODUCTION TO JAVA
PROGRAMMING
UNIT-I
OOP Concepts:-Data abstraction, encapsulation, inheritance, Benefits of Inheritance,
Polymorphism, classes and objects, Procedural and object oriented programming paradigms.
Java Programming- History of Java, comments, Data types, Variables, Constants, Scope and
Lifetime of variables, Operators, Operator Hierarchy, Expressions, Type conversion and casting,
Enumerated types, Control flow- block scope, conditional statements, loops, break and continue
statements, simple java stand alone programs, arrays, console input and output, formatting output,
constructors, methods, parameter passing, static fields and methods, access control, this reference,
overloading methods and constructors, recursion, garbage collection, building strings, exploring
string class.
UNIT-II
Inheritance – Inheritance hierarchies super and subclasses, Member access rules, super
keyword, preventing inheritance: final classes and methods, the Object class and its
methods.
Polymorphism – dynamic binding, method overriding, abstract classes and methods.
Interfaces- Interfaces Vs Abstract classes, defining an interface, implement interfaces,
accessing implementations through interface references, extending interface.
Inner classes- Uses of inner classes, local inner classes, anonymous inner classes, static
inner classes, examples.
Packages- Defining, creating and accessing a package, Understanding CLASSPATH,
importing packages.
UNIT-III
Exception handling- Dealing with errors, benefits of exception handling, the
classification of exceptions- exception hierarchy, checked exceptions and
unchecked exceptions, usage of try, catch, throw, throws and finally, rethrowing
exceptions, exception specification, built in exceptions, creating own exception
subclasses.
Multithreading – Differences between multiple processes and multiple threads,
thread states, creating threads, interrupting threads, thread priorities,
synchronizing threads, inter-thread communication, producer consumer
pattern,Exploring [Link] and [Link].
UNIT-IV
Applets – Concepts of Applets, differences between applets and applications, life
cycle of an applet, types of applets, creating applets, passing parameters to
applets.
Event Handling: Events, Handling mouse and keyboard events, Adapter classes.
Files- Streams- Byte streams, Character streams, Text input/output.
Files- Streams- Byte streams, Character streams, Text input/output, Binary
input/output, random access file operations, File management using File class..
UNIT-V
GUI Programming with Java – AWT class hierarchy, component, container,
panel, window, frame, graphics.
AWT controls: Labels, button, text field, check box, and graphics. Layout
Manager –
Layout manager types: border, grid and flow.
Swing – Introduction, limitations of AWT, Swing vs AWT.
● What is Program ?
An ordered set of instructions to be executed by a computer to carry out a specific
task is called a program.
● What is Programming Language ?
The language used to specify this set of instructions to the computer is called a
programming language.
As we know that computers understand the language of 0s and 1s which is called machine language
or low level language.
● However, it is difficult for humans to write or comprehend instructions using 0s
and 1s. This led to the advent of high-level programming languages like
Python, C++, Visual Basic, PHP, Java that are easier to manage by humans
but are not directly understood by the computer.
● A program written in a high-level language is called source code.
● The language translators like compilers and interpreters are needed to
translate the source code into machine language.
● Java uses both compilers and interpreters to convert its instructions into
machine language, so that it can be understood by the computer.
● Unit-1
OOPs: Object Oriented Programming is a paradigm that provides many concepts
such as inheritance, data binding, polymorphism etc.
Simula is considered as the first object-oriented programming language.
The programming paradigm where everything is represented as an object is
known as truly object-oriented programming language.
Smalltalk is considered as the first truly object-oriented programming language.
OOPs (Object Oriented Programming System)
Object means a real word entity such as pen, chair, table etc.
Object-Oriented Programming is a methodology or paradigm to design a program
using classes and objects.
It simplifies the software development and maintenance by providing some
concepts:
OOPs (Object Oriented Programming System) (Cont.)
1. Object
2. Class
3. Encapsulation
4. Inheritance
5. Polymorphism
6. Abstraction
Class
A class is a blueprint for creating objects.
It defines the attributes (data) and methods (behaviours) of the objects.
Think of a class as a template.
Create a Class
To create a class, use the keyword class:
Class (Cont.)
● Modifiers: A class can be public or have default access.
● Class name: The class name should begin with the initial letter capitalized by convention.
● Body: The class body is surrounded by braces, { }.
Example
class Car {
String brand;
int speed;
void drive() {
[Link](brand + " is driving at " + speed + " km/h.");
Here, Car is a class with attributes brand and speed, and a behavior drive().
Object
An object is an instance of a class.
It represents a specific entity with its own values for the defined attributes.
Car myCar = new Car();
[Link] = "Toyota";
[Link] = 120;
[Link]();
Attributes and Methods
Attributes (fields) store data, while methods define actions an object
can perform.
The brand and speed in the example are attributes, and drive() is a
method.
Examples
● A class is a template to create objects having similar properties and behavior, or in other words, we
can say that a class is a blueprint for objects.
● An object is an instance of a class. For example, the animal type Dog is a class, while a particular dog
named Tommy is an object of the Dog class.
Example
OOPs (Object Oriented Programming System) (Cont.)
1. Object
2. Class
3. Encapsulation
4. Inheritance
5. Polymorphism
6. Abstraction
OOPs (Object Oriented Programming System) (Cont.)
Create a Class
Create a class named "Main" with a variable x:
public class Main {
int x = 5;
}
OOPs (Object Oriented Programming System) (Cont.)
● Create an Object
● In Java, an object is created from a class.
● We have already created the class named Main, so now we can use this to create objects.
● To create an object of Main, specify the class name, followed by the object name, and use the keyword new:
● Create an object called "myObj" and print the value of x:
● public class Main {
● int x = 5;
●
● public static void main(String[] args) {
● Main myObj = new Main();
● [Link](myObj.x);
● }
● }
Inheritance
Multiple Objects
Create two objects of Main:
public class Main {
int x = 5;
public static void main(String[] args) {
Main myObj1 = new Main(); // Object 1
Main myObj2 = new Main(); // Object 2
[Link](myObj1.x);
[Link](myObj2.x);
}
}
Day 2
Java Install
Add path to Environment Variables path
C:\Program Files\Java\jdk-23\bin
Print Text
[Link]("Hello World!");
// This is a simple Java program to print Hello World!
public class HelloWorld {
public static void main(String[] args) {
[Link]("Hello World!");
} Output
Hello World!
}
How does this work:
● // starts a single-line comment. The comments does not executed by Java.
● public class HelloWorld defines a class named HelloWorld. In Java, every program must be inside
a class.
● public static void main(String[] args) is the entry point of any Java application. It tells the JVM
where to start executing the program.
● [Link]("Hello, World!"); prints the message to the console.
Print Number
[Link](3);
[Link](358);
[Link](50000);
[Link](3 + 3);
[Link](2 * 5);
Java Multi-line Comments
● Multi-line comments start with /* and ends with */.
● Any text between /* and */ will be ignored by Java.
/* The code below will print the words Hello World
to the screen, and it is amazing */
[Link]("Hello World");
Java program execution
● Write code in a file like [Link].
● The Java Compiler "javac" compiles it into bytecode
"[Link]".
● The JVM (Java Virtual Machine) reads the .class file and
interprets the bytecode.
● JVM converts bytecode to machine readable code i.e. "binary"
(001001010) and then execute the program.
Java program execution
Differences Between JDK, JRE and JVM
● JDK: JDK stands for Java Development Kit. It is a set of development tools and libraries used to
create Java programs. It works together with the JVM and JRE to run and build Java applications..
● JRE: JRE stands for Java Runtime Environment, and it provides an environment to run Java
programs on the system. The environment includes Standard Libraries and JVM.
● JVM: JVM stands for Java Virtual Machine. It's responsible for executing the Java program.
JDK (Java Development Kit)
The JDK is a software development kit that provides tools to develop and run Java applications. It includes
two main components:
● Development Tools (to provide an environment to develop your java programs)
● JRE (to execute your java program)
Working of JDK
The JDK enables the development and execution of Java programs. Consider the following process:
● Java Source File (e.g., [Link]): You write the Java program in a source file.
● Compilation: The source file is compiled by the Java Compiler (part of JDK) into bytecode, which is
stored in a .class file (e.g., [Link])
JDK (Java Development Kit)
● Execution: The bytecode is executed by the JVM (Java Virtual Machine), which interprets the
bytecode and runs the Java program.
JRE ((Java Runtime Environment)
The JRE is an installation package that provides an environment to only run(not develop) the Java program (or
application) onto your machine. JRE is only used by those who only want to run Java programs that are end-users of
your system.
Working of JRE
When you run a Java program, the following steps occur:
● Class Loader: The JRE’s class loader loads the .class file containing the bytecode into memory.
● Bytecode Verifier: JRE includes a bytecode verifier to ensure security before execution
● Interpreter: JVM uses an interpreter + JIT compiler to execute bytecode for optimal performance
● Execution: The program executes, making calls to the underlying hardware and system resources as
needed.
JVM (Java Virtual Machine)
The JVM is a very important part of both JDK and JRE because it is contained or inbuilt in both. Whatever
Java program you run using JRE or JDK goes into JVM and JVM is responsible for executing the java program
line by line, hence it is also known as an interpreter.
Working of JVM
It is mainly responsible for three activities.
● Loading
● Linking
● Initialization
Java Variables
Variables are containers for storing data values.
In Java, there are different types of variables, for example:
● String - stores text, such as "Hello". String values are surrounded by double quotes
● int - stores integers (whole numbers), without decimals, such as 123 or -123
● float - stores floating point numbers, with decimals, such as 19.99 or -19.99
● char - stores single characters, such as 'a' or 'B'. Char values are surrounded by single quotes
● boolean - stores values with two states: true or false
Declaring (Creating) Variables
To create a variable in Java, you need to:
● Choose a type (like int or String)
● Give the variable a name (like x, age, or name)
● Optionally assign it a value using =
● Syntax
● type variableName = value;
Example
● String name = "John";
[Link](name);
● int myNum = 15;
[Link](myNum);
● int myNum;
myNum = 15;
[Link](myNum);
● int myNum = 15;
myNum = 20; // myNum is now 20
[Link](myNum);
Final Variables
final int myNum = 15;
myNum = 20;
int myNum = 5;
float myFloatNum = 5.99f;
char myLetter = 'D';
boolean myBool = true;
String myText = "Hello";
Display Variables
The println() method is often used to display variables.
To combine both text and a variable, use the + character:
String name = "John";
[Link]("Hello " + name);
You can also use the + character to add a variable to another variable:
String firstName = "John ";
String lastName = "Doe";
String fullName = firstName + lastName;
[Link](fullName)
Display Variables
For numeric values, the + character works as a mathematical operator (notice that we use int (integer) variables here):
int x = 5;
int y = 6;
[Link](x + y); // Print the value of x + y
Declare Many Variables
int x = 5;
int y = 6;
int z = 50;
[Link](x + y + z);
We can also write that
int x = 5, y = 6, z = 50;
[Link](x + y + z);
One Value to Multiple Variables
int x, y, z;
x = y = z = 50;
[Link](x + y + z);
Java Identifiers
All Java variables must be identified with unique names.
These unique names are called identifiers.
Identifiers can be short names (like x and y) or more descriptive names (age, sum, totalVolume).
Note: It is recommended to use descriptive names in order to create understandable and maintainable code:
// Good
int minutesPerHour = 60;
// OK, but not so easy to
understand what m actually is
int m = 60;
The general rules for naming variables are:
● Names can contain letters, digits, underscores, and dollar signs
● Names must begin with a letter
● Names should start with a lowercase letter, and cannot contain whitespace
● Names can also begin with $ and _
● Names are case-sensitive ("myVar" and "myvar" are different variables)
● Reserved words (like Java keywords, such as int or boolean) cannot be used
as names
Constants (final keyword)
When to Use final?
You should declare variables as final when their values should never change. For example, the number of minutes in an
hour will always be 60, and your birth year will never change:
final int MINUTES_PER_HOUR =
60;
final int BIRTHYEAR = 1980;
Real-Life Examples
// Student data
String studentName = "John Doe";
int studentID = 15;
int studentAge = 23;
float studentFee = 75.25f;
char studentGrade = 'B';
// Print variables
[Link]("Student name: " + studentName);
[Link]("Student id: " + studentID);
[Link]("Student age: " + studentAge);
[Link]("Student fee: " + studentFee);
[Link]("Student grade: " + studentGrade);