JAVA PROGRAMMING – DETAILED
NOTES
1. Introduction to Java
Java is a high-level, object-oriented programming language used for developing a wide range
of applications such as web applications, mobile applications, enterprise software, and embedded
systems.
Java was developed by James Gosling and his team at Sun Microsystems in 1995. Later, Sun
Microsystems was acquired by Oracle Corporation, which now maintains and develops Java.
One of the most important features of Java is its platform independence. Java follows the
principle:
“Write Once, Run Anywhere (WORA)”
This means that a Java program written on one system can run on any other system that has Java
installed.
Java programs run on a special environment called the Java Virtual Machine (JVM), which
converts Java bytecode into machine code.
2. Features of Java
Java provides many powerful features that make it popular among developers.
1. Platform Independent
Java programs are compiled into bytecode, which can run on any system that has a JVM
installed.
2. Object-Oriented
Java follows the Object-Oriented Programming (OOP) paradigm. Programs are built using
objects and classes.
3. Secure
Java has built-in security features such as:
No direct memory access
Bytecode verification
Security manager
4. Robust
Java is considered robust because it has:
Strong memory management
Exception handling
Automatic garbage collection
5. Multithreading
Java supports multithreading, which allows multiple tasks to run simultaneously.
6. Portable
Java programs can easily move from one platform to another without modification.
3. Java Program Structure
A basic Java program contains a class and a main method.
Example:
class HelloWorld
{
public static void main(String args[])
{
[Link]("Hello World");
}
}
Explanation
Class
A class is a blueprint used to create objects.
Main Method
The main() method is the starting point of a Java program.
[Link]()
Used to display output on the screen.
4. Java Data Types
Data types specify the type of data that a variable can store.
Java has two main categories of data types.
1. Primitive Data Types
Primitive data types store simple values.
Examples:
int
float
double
char
boolean
byte
short
long
Example:
int age = 20;
double salary = 25000.50;
char grade = 'A';
boolean result = true;
2. Non-Primitive Data Types
Non-primitive data types are created by programmers and can store complex data.
Examples:
String
Arrays
Classes
Interfaces
Example:
String name = "John";
5. Variables in Java
A variable is a container used to store data values.
Syntax:
datatype variableName = value;
Example:
int number = 10;
String city = "Chennai";
Types of Variables
1. Local Variables – Declared inside methods
2. Instance Variables – Declared inside classes but outside methods
3. Static Variables – Declared with the static keyword
6. Operators in Java
Operators are symbols used to perform operations on variables and values.
1. Arithmetic Operators
Used for mathematical operations.
Examples:
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus
Example:
int a = 10;
int b = 5;
int result = a + b;
2. Relational Operators
Used to compare two values.
Examples:
== Equal to
!= Not equal
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
3. Logical Operators
Used to combine conditions.
Examples:
&& Logical AND
|| Logical OR
! Logical NOT
4. Assignment Operators
Used to assign values.
Examples:
= Assign
+= Add and assign
-= Subtract and assign
7. Control Statements
Control statements determine the flow of program execution.
1. If Statement
Used to execute code when a condition is true.
Example:
if(age >= 18)
{
[Link]("Eligible to vote");
}
2. If–Else Statement
Used when there are two possible outcomes.
if(mark >= 50)
{
[Link]("Pass");
}
else
{
[Link]("Fail");
}
3. Switch Statement
Used when multiple choices are available.
Example:
switch(day)
{
case 1:
[Link]("Monday");
break;
case 2:
[Link]("Tuesday");
break;
}
8. Looping Statements
Loops are used to execute a block of code repeatedly.
1. For Loop
for(int i = 1; i <= 5; i++)
{
[Link](i);
}
2. While Loop
int i = 1;
while(i <= 5)
{
[Link](i);
i++;
}
3. Do-While Loop
int i = 1;
do
{
[Link](i);
i++;
}
while(i <= 5);
9. Object-Oriented Programming Concepts
Java follows the Object-Oriented Programming model.
Main OOP principles include:
Class
Object
Inheritance
Polymorphism
Encapsulation
Abstraction
10. Class and Object
Class
A class is a blueprint for creating objects.
Example:
class Student
{
int id;
String name;
}
Object
An object is an instance of a class.
Example:
Student s1 = new Student();
11. Inheritance
Inheritance allows a class to inherit properties and methods from another class.
Example:
class Animal
{
void eat()
{
[Link]("Eating");
}
}
class Dog extends Animal
{
void bark()
{
[Link]("Barking");
}
}
Inheritance promotes code reuse.
12. Polymorphism
Polymorphism means one method with multiple forms.
Types:
1. Method Overloading
2. Method Overriding
Example of Method Overloading:
int add(int a, int b)
{
return a + b;
}
int add(int a, int b, int c)
{
return a + b + c;
}
13. Encapsulation
Encapsulation is the process of wrapping data and methods into a single unit (class).
It also involves data hiding using access modifiers.
Example:
class Student
{
private int id;
public void setId(int id)
{
[Link] = id;
}
public int getId()
{
return id;
}
}
14. Exception Handling
Exception handling is used to handle runtime errors in a program.
Keywords used:
try
catch
finally
throw
throws
Example:
try
{
int a = 10 / 0;
}
catch(Exception e)
{
[Link]("Exception occurred");
}
15. Applications of Java
Java is widely used in many fields.
Examples:
Web applications
Mobile applications
Desktop software
Enterprise systems
Cloud applications
Java is commonly used to develop mobile applications for Android, which is developed by
Google.
16. Advantages of Java
Platform independent
Secure programming language
Large developer community
Object-oriented structure
Strong memory management
17. Conclusion
Java is one of the most widely used programming languages in the world. It is used in web
development, mobile app development, enterprise systems, and many other technologies.
Because of its portability, security, and powerful features, Java continues to be an important
language in modern software development.