Java Programming Notes
Java Programming Notes
UNIT -I
An overview of Internet
What is Java?
Feature of java:
Java has many features,
Simplicity: Java's syntax is clean and straightforward, making it easy to learn and
understand.
Object-oriented: Java treats everything as an object, which helps organize and design
software. The four pillars of Java's object-oriented programming (OOP) are encapsulation,
inheritance, polymorphism, and abstraction.
Platform independent: Java code can be written once and run on different platforms, such
as Windows, Linux, and Mac, without modification.
Security: Java is known for its security features, such as avoiding explicit pointers and
running programs in a secure environment.
Robust: Java is strong and reliable, with features like automatic garbage collection and
efficient memory management.
Architecture neutral: Java is not dependent on a specific architecture, and the size of
primitive data types is fixed.
An overview of Internet:
● The ARPANET (Advanced Research Projects Agency Network, later renamed the
internet) established a successful link between the University of California October 29,
1969.
● TCP/IP (Transmission Control Protocol and Internet Protocol) was established in the
1970s, allowing internet technology to mature.
● In the year 1991, a user-friendly internet interface was developed.
● Delhi was the first national commercial online service to offer internet connectivity in
July 1992.
● Wi-Fi was first introduced in 1997.
● Smartphone use was widespread in 2007.
● The 4G network was launched in 2009.
● The internet is used by 3 billion people nowadays.
● By 2030, there are expected to be 7.5 billion internet users and 500 billion devices linked
to the internet.
Benefits of Internet:
The internet has many benefits, including:
● Privacy: Hackers can steal sensitive information from devices and post it on social
media.
● Cybercrime: The internet can be used for cyberbullying, identity theft, fraud, and other
cybercrimes.
● Addiction: People can become addicted to the internet, spending more time online than
on real-life activities.
● Health: Spending too much time on the internet can negatively impact your health,
including eye strain, obesity, and improper posture.
● Distraction: Using social media during work or study hours can reduce productivity.
● Isolation: Online education can lead to feelings of isolation, as students may not have
the same opportunities to interact with other students.
● Academic performance: Excessive internet use can negatively impact academic
performance.
● Misinformation: The internet can be exposed to misinformation.
● Self-esteem: Excessive internet use can lead to comparison and self-esteem issues
● Internet Protocol (IP) is a set of protocols that enables data to be sent between computers
on the internet.
● The Internet Protocol (IP) is a set of rules that allows computers and other devices to
communicate over the Internet.
● It ensures that information sent from one device reaches the correct destination by using
a unique set of numbers known as IP addresses.
● IP has the task of delivering packets from the source host to the destination host solely
based on the IP addresses.
What is an IP Packet?
1. TCP: Transmission Control Protocol enables the flow of data across IP address
connections.
2. UDP: User Datagram Protocol provides a way to transfer low-latency process
communication that is widely used on the internet for DNSlookup and voice over
Internet Protocol.
3. FTP: File Transfer Protocol is a specification that is purpose-built for accessing,
managing, loading, copying and deleting files across connected IP hosts.
4. HTTP: Hypertext Transfer Protocol is the specification that enables the modern
web. HTTP enables websites and web browsers to view content. It typically runs over
port 80.
5. HTTPS: Hypertext Transfer Protocol Secure is HTTP that runs with encryption via
Secure Sockets Layer or Transport Layer Security. HTTPS typically is served over
port 443.
What is IP Address:
● IP provides mechanisms that enable different systems to connect to each other to transfer
data.
Categories of IP Addresses:
IPv4 addresses are divided into two categories:
● Public address
● Private address
Public address:
● The public address is also known as an external address as they are grouped under the
WAN addresses.
● We can also define the public address as a way to communicate outside the network.
● This address is used to access the internet. The public address available on our computer
provides remote access to our computer.
● With the help of a public address, we can set up the home server to access the internet.
This address is generally assigned by the ISP (Internet Service Provider).
Private address:
● A private address is also known as an internal address, as it is grouped under the LAN
addresses.
● It is used to communicate within the network.
● These addresses are not routed on the internet so that no traffic can come from the
internet to this private address.
● The address space for the private address is allocated using to create our own network.
● The private addresses are assigned to mainly those computers, printers, smartphones,
which are kept inside the home or the computers that are kept within the organization.
Internet Access:
● Internet access refers to users or organizations connecting to the internet via laptops,
mobile devices, or personal computers.
Knowing the differences between the connections is essential when choosing the type and
speed that meets your needs. There are numerous ways to connect your e-reader, tablet, gaming
console, desktop, smartphone, or laptop to the internet in this day and age. Below is a deep dive
1. WiFi hotspots:
● These sites offer internet access over WLAN, a Wireless Local Area Network, through a
● Hotspots use WiFi technology that uses radio waves to connect electronic devices to the
● It is among the fastest options currently, and it uses several multiple channels to transfer
3. Dial-up:
● In dial-up connections, users have to link their phone line to a computer to access the
internet.
● This type of connection, also known as analogue, restricts users from making or receiving
phone calls via their phone service when using the internet.
● Although dial-up connections were commonly used before, they are currently outdated.
4. Satellite:
connection is unavailable.
5. Cable:
● Users can access the internet over cable TV lines using a cable modem.
● These modems typically offer fast access to the internet, making this type of connection
● DSL is the acronym for Digital Subscriber Line, and it utilizes existing 2-wire copper
● Unlike dial-up connections, users can make calls while still browsing the internet.
7. ISDN:
● Integrated Service Digital Network, commonly ISDN, enables users to send video, voice
● The user and the internet access provider must install an ISDN adapter at both ends of
this transmission.
Future of internet:
● Decentralization
The internet will be powered by decentralized technologies like blockchain and peer-to-
Users will be able to browse the web using virtual reality (VR) and augmented reality
(AR).
● Smart devices
More devices will be connected to the internet, including smart earbuds, VR glasses, and
holographic projectors.
● Body augmentation
Users will be able to enhance their bodies with technology, such as AR contact lenses or
Artificial intelligence (AI) and machine learning will be used to generate content,
● Internet Application:
An internet application is a program that uses the internet to connect a client to a server.
Internet applications can be used to provide services like online shopping, banking, and
social networking.
Internet applications:
● Online shopping: Users can shop from virtual stores that are available 24/7.
● Email: A widely used application that allows users to send and receive electronic
messages.
● Social networking: Users can connect with friends and family through social media
sites.
● Online banking: Users can access their bank accounts and perform transactions online.
● Education: Users can access educational resources and take online courses.
● Gaming: Users can play games online.
❖ Class
❖ Objects
❖ Encapsulation
❖ Abstraction
❖ Polymorphism
❖ Inheritance
❖ Dynamic Binding
❖ Message Passing
Object:
Class:
● [Ex]: Car is consider as class. That properties wheel, accelarator and break are objects.
Abstraction:
● [Ex]: The man only knows pressing the accelerator and increase speed. Don’t have knows
Encapsulation:
● Encapsulation is binding together properties and the functions that manipulate them.
Inheritance:
● One class properties and characteristics derived to another class is called Inheritance.
● First class is the base class. Second class is the Derived class.
There are 5 types:
2. Multiple Inheritance - One subclass is derived from more than base class.
3. Multilevel Inheritance - One sub class is derived from another sub class (Base).
4. Hierarchical Inheritance - More than subclass is derived from a single base class.
[Ex]: ‘+’ symbol use as Arithmetic operator; Some time used Unary operator.
Dynamic Binding:
● Dynamic binding is the connection between the function declaration and the function
call.
Message Passing:
Benefits of OOPs:
A programming paradigm known as object-oriented programming (OOP). It employs objects
1. Modularity:
2. Reusability:
● Reduces the need to write repetitive code, saving time and effort.
3. Syntax:
● This is the set of rules that define how words and punctuation are organized in a
programming language.
5. Maintenance:
● With OOPs, updating or changing a system is frequently simpler and less error.
7. Scalability:
● OOP ideas like polymorphism and inheritance can help a system scale more easily.
● It allows for the addition of new classes or modifications without affecting the system's
whole codebase.
8. Real-World Modeling
● This makes the code more aligned with human understanding of the problem domain.
9. Extensibility
● New features and functionalities can be added with minimal disruption to existing
code.
development process.
10. Ease of Troubleshooting
Applications of OOPS:
Object-Oriented Programming (OOP) has found widespread applications across various domains
due to its modular, reusable, and scalable nature. Here are some key applications of OOP:
● Software Development:
OOP is extensively used to build large-scale and complex systems. The modular structure
of OOP allows developers to organise code efficiently, making it more manageable and
maintainable.
● Game Development:
The client-server systems are those that involve a relationship between cooperating
programs in an application. In general, the clients will initiate requests for services and
the servers will provide that functionality. The client and server either reside in the same
system or communicate with each other through a computer network or the internet.
● Web Development:
Many web development languages, including Java, Python, and PHP, incorporate OOP
principles. OOP allows developers to create modular and scalable web applications. Each
component, such as user interfaces and data structures, can be encapsulated in objects for
better organisation and maintenance.
Object-relational mapping (ORM) frameworks, which bridge the gap between object-
oriented programming languages and relational databases, are built on OOP concepts.
This integration simplifies data manipulation and enhances database interactions in
software applications.
OOP is well-suited for GUI development, where user interfaces are represented as
objects with specific properties and behaviours. This allows for the creation of interactive
and user-friendly applications.
● Network Programming:
● Data types in Java specify how memory stores the values of the variable. Each variable
has a data type that decides the value the variable will hold.
● Data types specify the different sizes and values that can be stored in the variable.
1. Primitive data types: The primitive data types include boolean, char, byte, short, int,
long, float and double.
2. Non-primitive data types: The non-primitive data types include Classes, Interfaces, and
Arrays.
In Java, non-primitive data types, also known as reference data types, are used to store complex
objects rather than simple values.
1. Arrays
Arrays have a fixed size, which is specified when the array is created, and can be accessed
using an index. Arrays are commonly used to store lists of values or to represent matrices and
other multi-dimensional data structures.
Array Declaration:
type[] arrayName;
Create an Array:
To create an array, you need to allocate memory for it using the new keyword:
2. Strings:
Example:
System.out.println(greeting);
Java Variables:
● 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
● String - stores text, such as "Hello". String values are surrounded by double quotes
● Boolean - stores values with two states: true or false
To create a variable, you must specify the type and assign it a value:
Syntax:
● You learned from the previous chapter that you can use the println() method to output
values or print text in Java.
● Text must be wrapped inside double quotations marks "".
● If you forget the double quotes, an error occurs:
Example:
System.out.println("Hello World!");
System.out.println("Hello World!");
Output:
Java Input:
● This statement accepts an integer from the user using Stream class and parses it into int
data type using Integer.
● parseInt() method. readLine() means it'll take users input
Example:
The two terms type casting and the type conversion are used in a program to convert one data
type to another data type.
● In typing casting, a data type is converted into another data type by the programmer using
the casting operator during the program design.
● In typing casting, the destination data type may be smaller than the source data type when
Example:
float b = 3.0;
int a = (int) b; // converting a float value into integer
2. Type conversion :
● In type conversion, a data type is automatically converted into another data type by a
compiler at the compiler time.
● In type conversion, the destination data type cannot be smaller than the source data type,
that’s why it is also called widening conversion.
● One more important thing is that it can only be applied to compatible data types.
Syntax:
Example:
int x=30;
float y;
y=x; // y==30.000000.
====================================================================
UNIT - II
Operators
Operators:
● Java operators are special symbols that perform operations on variables or values.
● They can be classified into several categories based on their functionality.
● These operators play a crucial role in performing arithmetic, logical, relational, and
bitwise operations etc.
1. Arithmetic Operators:
Arithmetic operators are used to perform simple arithmetic operations on primitive and non-
primitive data types.
● * : Multiplication
● / : Division
● % : Modulo
● + : Addition
● – : Subtraction
Example:
System.out.println("a + b = " + (a + b));
System.out.println("a - b = " + (a - b));
System.out.println("a * b = " + (a * b));
System.out.println("a / b = " + (a / b));
System.out.println("a % b = " + (a % b));
Output:
a + b = 13
a-b=7
a * b = 30
a/b=3
a%b=1
2. Bitwise Operators
Bitwise Operators are used to perform the manipulation of individual bits of a number and with
any of the integer types. They are used when performing update and query operations of the
Binary indexed trees.
● & (Bitwise AND) – returns bit-by-bit AND of input values.
● | (Bitwise OR) – returns bit-by-bit OR of input values.
● ^ (Bitwise XOR) – returns bit-by-bit XOR of input values.
● ~ (Bitwise Complement) – inverts all bits (one’s complement).
Example:
System.out.println("d & e : " + (d & e));
System.out.println("d | e : " + (d | e));
System.out.println("d ^ e : " + (d ^ e));
System.out.println("~d : " + (~d));
Output:
d&e:8
d | e : 14
d^e:6
~d : -11
3. Relational Operators
Relational Operators are used to check for relations like equality, greater than, and less than.
They return boolean results after the comparison and are extensively used in looping statements
as well as conditional if-else statements. The general format is
● == , Equal to.
● != , Not equal to.
● < , Less than.
● <= , Less than or equal to.
● > , Greater than.
● >= , Greater than or equal to.
Example:
Output:
a > b: true
a < b: false
a >= b: true
a <= b: false
a == c: false
a != c: true
4. Logical Operators
Logical Operators are used to perform “logical AND” and “logical OR” operations, similar to
AND gate and OR gate in digital electronics. They have a short-circuiting effect, meaning the
second condition is not evaluated if the first is false.
● &&, Logical AND: returns true when both conditions are true.
● ||, Logical OR: returns true if at least one condition is true.
● !, Logical NOT: returns true when a condition is false and vice-versa
Example:
System.out.println("x && y: " + (x && y));
System.out.println("x || y: " + (x || y));
System.out.println("!x: " + (!x));
Output:
x && y: false
x || y: true
!x: false
5. Assignment Operator
‘=’ Assignment operator is used to assign a value to any variable. It has right-to-left
associativity, i.e. value given on the right-hand side of the operator is assigned to the variable on
the left, and therefore right-hand side value must be declared before using it or should be a
constant.
variable = value;
In many cases, the assignment operator can be combined with others to create shorthand
compound statements. For example, a += 5 replaces a = a + 5. Common compound operators
include:
Example:
System.out.println("f += 3: " + (f += 3));
System.out.println("f -= 2: " + (f -= 2));
System.out.println("f *= 4: " + (f *= 4));
System.out.println("f /= 3: " + (f /= 3));
System.out.println("f %= 2: " + (f %= 2));
Output:
f += 3: 10
f -= 2: 8
f *= 4: 32
f /= 3: 10
f %= 2: 0
6. Boolean Operators:
Boolean operators in Java are logical operators that perform logical operations and return a
boolean value. Boolean values can only be true or false.
● Boolean expressions are expressions that evaluate to true or false
● Boolean expressions are often used with comparison operators
● Boolean expressions are used in conditional statements
Boolean data type
● The boolean keyword is used to declare a variable that can only have the values true or
false
● For example, boolean user = true
Example:
int age = 25;
boolean is EligibleToVote = (age >= 18) && (age <= 65); // This will be true because 25 is
within the range
Syntax :
while (boolean condition)
{
loop statements...
}
Example:
import java.io.*;
class GFG {
public static void main(String[] args)
{
int i = 5;
while (i < 10)
{
i++;
System.out.println("GfG");
}
}
}
Output:
GFG
GFG
GFG
GFG
GFG
Do-while loop:
Syntax:
do
{
statements..
}
while (condition);
Example:
import java.io.*;
class GFG {
public static void main(String[] args)
{
int i = 5;
do {
i++;
System.out.println("GfG");
} while (i < 10);
}
}
Output
GFG
GFG
GFG
GFG
GFG
Jump Statements:
1. break
2. continue
3. Return
Break Statement:
Class break_statement
{
public static void main(String[] args)
{
int i=1;
for (;;i++) //infinite loop
{
if (i==5)
{
break;
}
else
{
System.out.println(i);
}
}
}
}
2. Continue statement in Java
● The continue statement in Java never terminates the execution of any loops.
● It can only work inside the "loop statement".
● The primary job of the "Continue statement" is the iteration of that specific loop.
● The nature of the Continue statement is to skip the current iteration and force for the next
one.
●
Example:
● Return statement is used to exit a method and, if needed, return a value to the called
● The return statement in Java is a type of jump statement which is primarily used only
inside the functions or methods.
● The purpose of using a Return statement is to terminate the current method of execution
and transfer the control to the next "calling method".
● There are two types of Return statements, which are "Return with a value" and "Return
without a value".
Return with a value: Where expression is the value or result that the function is returning.
Example:
Sum of a and b: 15