Java Programming - The Book For Beginners by Archies Gurav PDF
Java Programming - The Book For Beginners by Archies Gurav PDF
PROGRAMMING
THE BOOK FOR BEGINNERS!
ARCHIES GURAV
JAVA
PROGRAMMING
THE BOOK FOR BEGINNERS!
ARCHIES GURAV
"It is not the language that makes
appear simple!"
~ Robert C Martin
CONTENTS
INTRODUCTION..................................................................................1
HISTORY OF JAVA............................................................................2
FEATURES OF JAVA........................................................................3
SETTING UP THE DEVELOPMENT ENVIRONMENT.....4
FIRST JAVA PROGRAM.................................................................8
BEHIND THE SCENES OF JAVA CODE................................10
COMMENTS, DATA TYPES, VARIABLES AND
CONSTANTS.......................................................................................11
SCOPE AND LIFETIME OF VARIABLES...............................18
OPERATORS AND EXPRESSIONS.........................................19
TYPE CONVERSION, TYPE CASTING, COMMAND
LINE ARGUMENTS AND ENUMS............................................31
CONDITIONAL STATEMENTS.................................................35
LOOPS...................................................................................................39
CONTINUE AND BREAK STATEMENTS........................... 43
USING SCANNER CLASS............................................................46
TYPES OF FUNCTIONS...............................................................47
GARBAGE COLLECTION.............................................................51
STRINGS IN JAVA...........................................................................52
STRINGBUILDER AND STRINGBUFFER ..........................58
ARRAYS AND 2D ARRAYS........................................................61
ACCESS SPECIFIERS.....................................................................64
OOP CONCEPTS..............................................................................66
ARCHIES GURAV
CONTENTS
JAVA 8 FEATURES........................................................................79
INNER CLASSES..............................................................................86
PACKAGES.........................................................................................90
MULTI-THREADING IN JAVA................................................94
COLLECTIONS FRAMEWORK................................................98
JAVA GENERICS............................................................................102
EXCEPTION HANDLING...........................................................107
I/O AND FILE HANDLING..........................................................111
CONNECTING TO DATABASE (JDBC)................................121
GUI PROGRAMMING IN JAVA...............................................125
JAVA PROJECTS...........................................................................134
ARCHIES GURAV
INTRODUCTION
place to start."
INTRODUCTION • 1
ARCHIES GURAV
HISTORY OF JAVA
HISTORY OF JAVA • 2
ARCHIES GURAV
FEATURES OF JAVA
easy to understand.
1. Object-Oriented
2. Portable
3. Platform Independent
4. Secured
5. Robust
6. Architecture Neutral
7. Interpreted
8. Dynamic
9. High Performance
10. Multithreaded
Platform
Portable Independent
Object-Oriented Secured
High Architectural
Performance Neutral
Dynamic Interpreted
FEATURES OF JAVA • 3
ARCHIES GURAV
SETTING UP THE
DEVELOPMENT ENVIRONMENT
- Installation Instructions for Windows
Download the JDK Installer
- Access Java SE and Click the link that corresponds
Installation wizard.
ARCHIES GURAV
- Installation Instructions for Mac OS
Download the JDK Installer
- Download the JDK `.dmg` file, `jdk-17_macos-
start it.
- A Finder window appears that contains an icon of
installation application.
- The installation application displays the
Introduction window.
- A window appears that displays the message:
ARCHIES GURAV
- Installation Instructions for Linux
In this, there are two types of Instructions for
space.
ARCHIES GURAV
2. Installing the 64-Bit JDK on RPM-Based Linux
Platforms
Download the 64-Bit JDK on an RPM based Linux
Platform
- Download the file, `jdk-17_linux-x64_bin.rpm`.
- In RPM based platform ensure that you have a root
command:
`$ rpm -Uvh jdk-17_linux-x64_bin.rpm`
- Delete the .rpm file if you want to save disk space.
- Exit the root shell. Reboot is not required.
system.
ARCHIES GURAV
FIRST JAVA PROGRAM
ARCHIES GURAV
- The `void` keyword means that the method doesn't
returns a value.
execution.
ARCHIES GURAV
BEHIND THE SCENES OF JAVA CODE
SOURCE JAVA BYTE
CODE COMPILER CODE
OS JVM
error.
- The JVM handles any errors and exceptions that
exceptions.
ARCHIES GURAV
COMMENTS, DATA TYPES,
specific time.
Types of Java Comments
- There are 3 types of comments in Java.
1. Java Single Line Comment
- The Single Line Comment is used to comment
ARCHIES GURAV
2. Java Multi-Line Comment
- The Multi-Line Comment is used to comment
ARCHIES GURAV
- Compile the code using `javac` tool:
ARCHIES GURAV
Data Types:
- Data Types represent the different values to be
DATA TYPE
PRIMITIVE NON-PRIMITIVE
STRING
CHARACTER INTEGRAL
INTEGER FLOATING-POINT
ARCHIES GURAV
Size(Bits
-9223372036854775808 to
`long` 64
9223372036854775807
`char` 16 0 to 65535
Not
Variables in Java:
- A variable in Java is a named storage location in
ARCHIES GURAV
- A Small example to understand the types of variables
Constants in Java
- A constant in Java is a variable whose value cannot
keyword.
- A Small example to understand the constants in
Java:
ARCHIES GURAV
HANDS-ON ASSIGNMENTS
ON THIS TOPIC:
to the console.
console.
ARCHIES GURAV
SCOPE AND LIFETIME
OF VARIABLES
Local Variables:
- These are declared within a method and have a
Instance Variables:
- These are declared outside of any method and are
Static Variables:
- These are declared with the static keyword and are
ARCHIES GURAV
OPERATORS AND EXPRESSIONS
Operators:
- Operator is a symbol that is used to perform
operations.
- There are many types of operators in Java:
Unary Operator
- The operator that can be used to perform
Operator.
1. Unary Minus (`-`): Negates the value of the
operand.
boolean operand.
ARCHIES GURAV
4. Increment (`++`): Increments the value of the
operand by 1.
operand by 1.
- Post/Pre Increment/Decrements.
- Pre-Increment/Decrement: The operator is
ARCHIES GURAV
Arithmetic Operator
- The operator that can be used to perform
second.
ARCHIES GURAV
5. Modulus (`%`): Returns the remainder of
Shift Operator
- The shift operators are used to shift the bits of
a binary representation of an integer value to the left
or right..
1. Left Shift (`<<`): Shifts the bits of the first
ARCHIES GURAV
3. Unsigned Right Shift(`>>>`): Shifts the bits of
Relational Operator
- Relational operators are used to compare two
ARCHIES GURAV
3. Less Than(`<`): Returns `true` if the value of the
ARCHIES GURAV
Logical Operators
- Logical operators are used to perform logical
ARCHIES GURAV
A B A || B
operand.
A !A
true false
false true
Bitwise Operator
- Bitwise operators are used to perform bit-
ARCHIES GURAV
2. Bitwise OR (` |`): Performs a bit-level OR
ARCHIES GURAV
Ternary Operator(Conditional)
- The ternary operator is a shorthand for an if-
else statement.
Syntax:
condition ? expression1 : expression2
Example:
Assignment Operator
- The assignment operator `=` is used to assign
a value to a variable.
- For Example:
int x = 10;
- Java also supports compound assignment
ARCHIES GURAV
Expressions in Java
- In Java, an expression is a combination of values,
10 + 20
x+y
x<y
of evaluation.
ARCHIES GURAV
HANDS-ON ASSIGNMENTS
ON THIS TOPIC:
operator.
variables.
three numbers.
ARCHIES GURAV
TYPE CONVERSION, TYPE
when:
The 2 data types are compatible
When we assign value of smaller data type
or narrowing.
This is useful for incompatible data types
done.
Here, target-type specifies the desired
ARCHIES GURAV
COMMAND LINE ARGUMENTS(CLA)
- The concept of passing arguments to the main
ARCHIES GURAV
ENUMS IN JAVA
- Enums in Java are a special type of class that
a for-each loop:
ARCHIES GURAV
HANDS-ON ASSIGNMENTS
ON THIS TOPIC:
operator.
ARCHIES GURAV
CONDITIONAL STATEMENTS
If Statement:
- The if statement in Java is used to execute a
printed.
CONDITIONAL STATEMENTS • 35
ARCHIES GURAV
If-else Statement:
- The if-else statement in Java is a type of
boolean expression.
- Syntax:
if(condition){
//code to be executed if condition is true
}else {
// code to be executed if condition is false
}
- Here is an example of an if-else statement in
Java:
CONDITIONAL STATEMENTS • 36
ARCHIES GURAV
Switch Statement:
- A switch statement in Java is a type of
Java:
ARCHIES GURAV
HANDS-ON ASSIGNMENTS
ON THIS TOPIC:
or not.
.
3. Write a program to find the largest of three
not.
CONDITIONAL OPERATORS • 38
ARCHIES GURAV
LOOPS
- In Java, loops are used to repeat a block of code for a
met.
For Loop:
- A `for` loop is used when you know the number
condition is met
- Syntax:
for (initialization; condition; increment/decrement) {
// code to be executed
}
- Example:
ARCHIES GURAV
While Loop:
- A `while` loop is used when you don't know the
LOOPS • 40
ARCHIES GURAV
Do-While Loop:
- The `do-while` loop in Java is similar to a while
LOOPS • 41
ARCHIES GURAV
HANDS-ON ASSIGNMENTS
ON THIS TOPIC:
positive numbers.
LOOPS • 42
ARCHIES GURAV
CONTINUE AND BREAK
STATEMENTS
- The `continue` and `break` statements are control
iteration.
- Example:
ARCHIES GURAV
Break:
- The break statement is used to exit a loop
ARCHIES GURAV
HANDS-ON ASSIGNMENTS
ON THIS TOPIC:
numbers.
ARCHIES GURAV
USING SCANNER CLASS
- The Scanner class in Java is part of the `java.util`
ARCHIES GURAV
TYPES OF FUNCTIONS
Function:
- Functions, also known as methods, are blocks of
function does.
- parameters: These are the values passed to the
name.
TYPES OF FUNCTIONS • 47
ARCHIES GURAV
- There are two types of functions in Java.
Instance Methods:
- These are methods that are associated with
Java
TYPES OF FUNCTIONS • 48
ARCHIES GURAV
Static Methods:
- These are methods that are associated with
class to be created.
- Here's an example of a static method in Java
ARCHIES GURAV
HANDS-ON ASSIGNMENTS
ON THIS TOPIC:
numbers.
TYPES OF FUNCTIONS • 50
ARCHIES GURAV
GARBAGE COLLECTION
- Garbage collection is an important feature of Java
GARBAGE COLLECTION • 51
ARCHIES GURAV
STRINGS IN JAVA
- In Java, a string is a sequence of characters,
string object.
For Example:
STRINGS IN JAVA • 52
ARCHIES GURAV
- Here are some common string functions in Java:
1. `length()`: Returns the length of the string.
characters in uppercase.
characters in lowercase.
STRINGS IN JAVA • 53
ARCHIES GURAV
6. `equals(Object obj)`: Returns true if the string is
7. `equalsIgnoreCase(String anotherString)`:Returns
STRINGS IN JAVA • 54
ARCHIES GURAV
11. `lastIndexOf(String str)`: Returns the index of the
otherwise.
newChar.
STRINGS IN JAVA • 55
ARCHIES GURAV
16. `replaceAll(String regex, String replacement)`:
STRINGS IN JAVA • 56
ARCHIES GURAV
HANDS-ON ASSIGNMENTS
ON THIS TOPIC:
vowels in a string.
palindrome.
character in a string.
ARCHIES GURAV
STRINGBUILDER AND
STRINGBUFFER
- `StringBuilder` and `StringBuffer` are both classes in
`StringBuffer` is that,
- `StringBuffer` is thread-safe, which means that
ARCHIES GURAV
- `StringBuilder` is not thread-safe, which means that
ARCHIES GURAV
HANDS-ON ASSIGNMENTS
ON THIS TOPIC:
StringBuilder.
StringBuffer.
ARCHIES GURAV
ARRAYS AND 2D ARRAYS
- In Java, an array is a collection of variables of the
ARCHIES GURAV
- A 2D array is an array of arrays in which each element
array in Java:
like this:
ARCHIES GURAV
HANDS-ON ASSIGNMENTS
ON THIS TOPIC:
elements in an array.
element in an array.
elements of a 2D array.
array.
2D array.
ARCHIES GURAV
ACCESS SPECIFIERS
- Access specifiers in Java are used to define the
variables in a program.
- There are four access specifiers in Java:
1. Public: Public is the most commonly used access
ACCESS SPECIFIERS• 64
ARCHIES GURAV
1. Default (No Keyword): A default or package-private
ACCESS SPECIFIERS• 65
ARCHIES GURAV
OBJECT ORIENTED PROGRAMMING
- Object-oriented programming (OOP) is a
functionality.
- OOP is based on the concept of objects, which are
instances of classes.
- A class is a blueprint or template that defines the
OOP CONCEPTS• 66
ARCHIES GURAV
- In this example, the Car class has three instance
and year.
- After creating the object of the class and assigning it
ARCHIES GURAV
- `super` keyword: In Java, the super keyword is used
ARCHIES GURAV
- Here is an example:
account.
- This makes the BankAccount class more secure and
ARCHIES GURAV
2. Inheritance: Inheritance is a fundamental concept in
inherits
Class A Class B
- Here is an example:
OOP CONCEPTS• 70
ARCHIES GURAV
Multilevel Inheritance: Multilevel inheritance
- Here is an example:
OOP CONCEPTS• 71
ARCHIES GURAV
Hierarchical Inheritance: Hierarchical inheritance
class.
Class A
Class B Class C
- Here is an example:
- In this example, both the Dog class and the Cat class
method from the Animal class, but each class adds its
own unique method (bark for Dog and meow for Cat).
- Overall, inheritance is a powerful concept in Java that
ARCHIES GURAV
3. Polymorphism: Polymorphism is another
Here is an example:
OOP CONCEPTS• 73
ARCHIES GURAV
Method Overriding: Method overriding is a
class.
- Polymorphism allows us to write more flexible and
OOP CONCEPTS• 74
ARCHIES GURAV
4. Abstraction: Abstraction is a fundamental concept
variables.
Here is an example:
ARCHIES GURAV
Interfaces: An interface is a collection of abstract
makeSound() method.
OOP CONCEPTS• 76
ARCHIES GURAV
- An interface only defines the methods that a class
like this:
ARCHIES GURAV
HANDS-ON ASSIGNMENTS
ON THIS TOPIC:
OOP CONCEPTS • 78
ARCHIES GURAV
JAVA 8 FEATURES
- Java 8 introduced a range of new features that
follows:
alphabetically.
- We use a lambda expression (a, b) -> a.compareTo(b)
JAVA 8 FEATURES• 79
ARCHIES GURAV
- This lambda expression implements the Comparator
readable way.
the Stream API. You can use the filter() and mapToInt()
methods as follows:
ARCHIES GURAV
3. Date and Time API: Java 8 also introduced a new
dates.
follows:
DateTimeFormatter.
JAVA 8 FEATURES• 81
ARCHIES GURAV
4. Default Methods: Default methods are a new feature
implements them.
methods.
JAVA 8 FEATURES• 82
ARCHIES GURAV
- Example: Suppose you have a functional interface
JAVA 8 FEATURES• 83
ARCHIES GURAV
6. Optional: The Optional class is a new addition to Java
available.
ARCHIES GURAV
HANDS-ON ASSIGNMENTS
ON THIS TOPIC:
in alphabetical order.
JAVA 8 FEATURES • 85
ARCHIES GURAV
INNER CLASSES
- Java provides a feature called inner classes that
ARCHIES GURAV
2. Static Inner Classes: A static inner class is a class that
INNER CLASSES• 87
ARCHIES GURAV
- Example:
INNER CLASSES• 88
ARCHIES GURAV
HANDS-ON ASSIGNMENTS
ON THIS TOPIC:
displays a message.
INNER CLASSES• 89
ARCHIES GURAV
PACKAGES
- Java packages are a mechanism for organizing
programs.
- Creating a package: To create a package in Java, you
"com.example.mypackage" package.
PACKAGES• 90
ARCHIES GURAV
- Package Naming Conventions: Java package names
PACKAGES• 91
ARCHIES GURAV
- In this example, the "java.util" package is imported
"java.util" package:
PACKAGES• 92
ARCHIES GURAV
HANDS-ON ASSIGNMENTS
ON THIS TOPIC:
arithmetic operations.
ARCHIES GURAV
MULTI-THREADING IN JAVA
- Java is a multi-threaded programming language,
What is a Thread?
- A thread is a lightweight process that can execute
ARCHIES GURAV
- Example:
code at a time.
MULTI-THREADING IN JAVA• 95
ARCHIES GURAV
- Example:
time.
costly.
MULTI-THREADING IN JAVA• 96
ARCHIES GURAV
HANDS-ON ASSIGNMENTS
ON THIS TOPIC:
class.
threads.
MULTI-THREADING IN JAVA• 97
ARCHIES GURAV
COLLECTIONS FRAMEWORK
- The Collection Framework in Java is a group of
programming language.
- The Collection Framework in Java includes the
following components:
1. Interfaces: The Collection Framework provides a
out) manner.
Dequeue: A collection that allows insertion and
COLLECTIONS FRAMEWORK• 98
ARCHIES GURAV
2. Classes: The Collection Framework provides a set of
classes include:
ArrayList: A resizable array implementation of the
List interface.
LinkedList: A doubly linked list implementation of
appropriate interface.
- Example: Let's consider an example of how the
COLLECTIONS FRAMEWORK• 99
ARCHIES GURAV
- In the above example, we create an ArrayList to store
loop through the sorted list and print out each name.
ARCHIES GURAV
HANDS-ON ASSIGNMENTS
ON THIS TOPIC:
Framework.
Collections Framework.
Framework.
ARCHIES GURAV
JAVA GENERICS
- Generics are used to make Java code more type-safe
maintainable.
called Box, which can work with any type of object. The
ARCHIES GURAV
- Let's see how we can use this class:
Generics:
write generic code that can work with any type. The '?'
ARCHIES GURAV
- Type Erasure: Java Generics use type erasure to
generic classes.
ARCHIES GURAV
- In the above example, we have defined a generic
ARCHIES GURAV
HANDS-ON ASSIGNMENTS
ON THIS TOPIC:
ARCHIES GURAV
EXCEPTION HANDLING
- Exception handling is the process of handling the
Types of Exceptions:
- In Java, there are two types of exceptions: checked
ARCHIES GURAV
Try-Catch Blocks: The try-catch block is used to handle
- Example:
our program.
EXCEPTION HANDLING• 108
ARCHIES GURAV
- Syntax:
- Example:
console.
ARCHIES GURAV
HANDS-ON ASSIGNMENTS
ON THIS TOPIC:
found.
ARCHIES GURAV
I/O & FILE HANDLING
- In Java, input/output (I/O) operations and file
Java.
- This chapter will cover the following topics:
1. Overview of I/O and File Handling in Java
2. Reading and Writing to Files
3. Reading and Writing to Text Files
4. Reading and Writing to Binary Files
5. Buffered I/O
6. Serialization
7. Compression and Decompression
binary data.
- The java.io package contains a variety of classes that
ARCHIES GURAV
- These classes are the abstract base classes for all
ARCHIES GURAV
3. Reading and Writing to Text Files:
- The Java BufferedReader and BufferedWriter classes
line.
ARCHIES GURAV
4. Reading and Writing to Binary Files:
- Reading and writing binary files in Java involves
ARCHIES GURAV
5. Buffered I/O:
- Buffered streams in Java allow for efficient reading
ARCHIES GURAV
6. Serialization:
- Serialization is a process in which an object in Java is
ARCHIES GURAV
- In the above example, we have a Student class that
ARCHIES GURAV
7. Compression and Decompression:
- In Java, the I/O and File Handling API provides a way
ARCHIES GURAV
- In this example, we create a new ZipOutputStream
FileInputStream.
- To decompress data, you create an instance of
ARCHIES GURAV
HANDS-ON ASSIGNMENTS
ON THIS TOPIC:
inside it.
object to a file.
ARCHIES GURAV
CONNECTING TO DATABASE
(JDBC)
- Java Database Connectivity (JDBC) is an API for Java
JDBC Architecture:
- The JDBC architecture consists of two main layers:
1. JDBC API: The JDBC API provides a set of classes
database.
ii. Statement: It is used to execute SQL queries.
iii. PreparedStatement: It is used to execute
query.
ARCHIES GURAV
a. JDBC-ODBC Bridge Driver: This driver acts as a
running.
c. Network Protocol Driver: This driver uses a
systems.
d. Thin Driver: This driver is similar to the network
ARCHIES GURAV
- If the application needs to access a local database,
database:
ARCHIES GURAV
HANDS-ON ASSIGNMENTS
ON THIS TOPIC:
ARCHIES GURAV
GUI PROGRAMMING
- Graphical User Interface (GUI) is a type of user
Introduction to AWT:
- AWT stands for Abstract Window Toolkit.
- AWT was introduced with the initial release of Java,
toolkit perform:
1. Component: The base class for all AWT
components.
3. LayoutManager: An interface for objects that
ARCHIES GURAV
5. EventQueue: A queue that manages AWT events.
6. Window: A top-level container that represents a
dialog box.
8. Frame: A window with a title bar and other
decorations.
9. Menu: A menu bar or pop-up menu.
10. MenuBar: A container for menus that can be added
to a window.
11. MenuItem: An item in a menu.
12. PopupMenu: A pop-up menu that can be attached
to a component.
13. Button: A component that represents a button.
14. Label: A component that displays a text label.
15. TextField: A component that allows the user to
enter text.
16. TextArea: A component that displays multiple lines
of text.
17. Checkbox: A component that represents a
checkbox.
18. List: A component that displays a list of items.
19.Scrollbar: A component that provides a scrollbar
for a container.
20. Canvas: A component that provides a drawing
surface.
GUI PROGRAMMING• 126
ARCHIES GURAV
- Let's Create a GUI with AWT: To create a GUI with
visibility.
Introduction to Swing:
- Swing was introduced later, and it provides a
ARCHIES GURAV
- Let's know what do each interface and class of AWT
toolkit perform:
1. JFrame - A top-level container that represents the
components.
3. JButton - A component that represents a clickable
button.
4. JLabel - A component that displays text or an
image.
5. JTextField - A component that allows the user to
tabular format.
10. JScrollPane - A container that provides scrolling
in a container.
11. ActionListener - An interface that defines a
clicks.
GUI PROGRAMMING• 128
ARCHIES GURAV
12. MouseListener - An interface that defines
releases.
14. SwingUtilities - A utility class that provides
ARCHIES GURAV
- Then, we have added these components to the
visibility.
Swing
- Swing is a more advanced and newer GUI library.
- Swing is based on a set of Java classes and
native components
- Swing components are lightweight and offers a
creating a GUI.
GUI PROGRAMMING• 130
ARCHIES GURAV
Event Handling in Swing and AWT
- Event handling is an important part of GUI
a button in Swing:
ARCHIES GURAV
BorderLayout: Arranges the components in the
container.
FlowLayout: Arranges the components in a row,
column.
BorderLayout.
ARCHIES GURAV
HANDS-ON ASSIGNMENTS
ON THIS TOPIC:
delete customers.
number.
GUI PROGRAMMING• 133
ARCHIES GURAV
JAVA PROJECTS
1. Employee Management System: Create a system
exchange rates
a set of options.
ARCHIES GURAV