Java Notes
Java Notes
History of Java 💻
Java was written by James Gosling along with Mike Sheridan and Patrick
Laughton while they were working at Sun Microsystems. Initially, it was named
Oak Programming Language. The Java team members, also known as the Green
Team, initiated a revolutionary task to develop a language for digital devices such
as set-top boxes, televisions, etc.
What is Java? 🤔
Java is a programming language and a platform. Java is a high-level, object-
oriented programming language.
Features of Java 🎉
1. Simple
Java is easy to learn and its syntax is quite simple, lean, and
easy to understand.
2. Object-Oriented
3. Robust
JAVA NOTES 1
4. Platform Independent
5. Secure
6. Multi-Threading
7. Architectural Neutral
8. Portable
9. High Performance
JAVA NOTES 2
performance with the use of just-in-time compilation.
JVM Architecture:
Component Description
Local variables and partial results are stored here. Each thread has a
Stack
private JVM stack created when the thread is created.
Native Method
It contains all native methods used in the application.
Stack
Native Method Native method interface gives an interface between Java code and
Interface native code during execution.
Native Method Native libraries consist of files required for the execution of native
Libraries code.
JAVA NOTES 3
executed.
Java Operators 💻
1. Arithmetic Operators
2. Relational Operators
3. Assignment Operators
5. Conditional Operators
JAVA NOTES 4
6. Bitwise Operators
Application of Java 💼
Java is being used in:
Arrays in Java 📊
What is an Array?
One-Dimensional Array
The general form to declare a one-dimensional array is:
Datatype array_variable_name[];
Here, Datatype can be any data type, and array_variable_name is the name of the
array.
Example:
int intArray[];
JAVA NOTES 5
This declares an array of integers.
Multidimensional Array
A multidimensional array displays values in the form of rows and columns, like a
table. It is also known as an array of arrays.
Example:
Syntax:
if (condition) {
statements
}
Example:
if (x > 0) {
System.out.println("Positive");
}
JAVA NOTES 6
If-Else Statement:
Syntax:
if (condition) {
statements
} else {
statements
}
Example:
if (x > 0) {
System.out.println("Positive");
} else {
System.out.println("Not Positive");
}
If-Else-If Statement:
Syntax:
if (condition1) {
statements
} else if (condition2) {
JAVA NOTES 7
statements
} else {
statements
}
Example:
if (x > 0) {
System.out.println("Positive");
} else if (x < 0) {
System.out.println("Negative");
} else {
System.out.println("Zero");
}
Switch Statement:
Syntax:
switch (variable) {
case constant1:
statements
break;
case constant2:
statements
break;
default:
statements
}
Example:
JAVA NOTES 8
switch (k) {
case 0:
System.out.println("Equal");
break;
case 1:
System.out.println("Not Equal");
break;
default:
System.out.println("Unknown");
}
Example:
Example:
ABC:
for (i = 1; i <= 10; i++) {
for (j = 1; j <= 10; j++) {
JAVA NOTES 9
if (j == 5) {
break ABC;
}
System.out.println("Hello");
}
}
Continue Statement:
Example:
Example:
XYZ:
while (true) {
while (true) {
if (i < 2) {
continue XYZ;
}
System.out.println("Hello");
JAVA NOTES 10
break XYZ;
}
}
Return Statement:
Example:
Syntax:
Example:
While Loop:
JAVA NOTES 11
While loops are used to execute a block of code repeatedly
while a certain condition is true.
Syntax:
while (condition) {
statements
}
Example:
i = 1;
while (i <= 10) {
System.out.println("Hello");
i++;
}
Do-While Loop:
Syntax:
do {
statements
} while (condition);
Example:
i = 1;
do {
System.out.println("Hello");
JAVA NOTES 12
i++;
} while (i <= 10);
For-Each Loop:
Syntax:
Example:
📚
}
```## Classes and Objects
### Constructors
JAVA NOTES 13
#### Types of Constructors
✔️ ❌ ❌ ❌
| --- | --- | --- | --- | --- |
✔️ ✔️ ❌ ❌
| **Private** | | | | |
✔️ ✔️ ✔️ ✔️
| **Default** | | | | |
| **Protected** | | | (Inheritance) | (Inheritanc
| **Public** | ✔️ | ✔️ | ✔️ | ✔️ |
e) |
* `abstract`
* `assert`
* `boolean`
JAVA NOTES 14
* `break`
* `byte`
* `case`
* `catch`
* `char`
* `class`
* `const`
* `continue`
* `default`
* `do`
* `double`
* `else`
* `enum`
* `extends`
* `final`
* `finally`
* `float`
* `for`
* `goto`
* `if`
* `implements`
* `import`
* `instanceof`
* `int`
* `interface`
* `long`
* `native`
* `new`
* `package`
* `private`
* `protected`
* `public`
* `return`
* `short`
* `static`
* `strictfp`
JAVA NOTES 15
* `super`
* `switch`
* `synchronized`
* `this`
* `throw`
* `throws`
* `transient`
* `try`
* `void`
* `volatile`
* `while`
## Inheritance in Java 👪
**Inheritance** is a mechanism in which one class can inherit
the properties and behavior of another class. Inheritance is
implemented using the `extends` and `implements` keywords.
## Purpose of Inheritance 🤔
* For **Method Overriding** (to achieve runtime polymorphis
m).
JAVA NOTES 16
* For **Code Reusability**.
🤔
| **Access Modifier** | Same or different | Same or more perm
issive |## What is an Applet?
JAVA NOTES 17
There are two ways to run an applet:
* By HTML file
* By AppletViewer tool (for testing purposes)
**Applet Code:**
```java
import java.applet.Applet;
import java.awt.Graphics;
HTML Code:
<html>
<body>
<applet code="First.class" width="300" height="800"></app
let>
</body>
</html>
JAVA NOTES 18
for testing purposes only.
Applet Code:
import java.applet.Applet;
import java.awt.Graphics;
javac First.java
appletviewer First.java
init() method
start() method
paint() method
stop() method
destroy() method
JAVA NOTES 19
The start() method makes the applet active and thereby eligible
for processor time.
The same applet can work on all installed versions of Java at the same time,
rather than just the latest plug-in version only.
Most web browsers cache applets so will be quick to load when visiting a web
page.
Can move the work from the server to the client, making the web solution
more scalable with the number of users/clients.
JAVA NOTES 20
Disadvantages
Requires the Java plug-in.
JAVA NOTES 21
Listener: A listener is also known as an event handler. It is responsible for
generating a response to an event. From an implementation point of view, the
listener is also an object.
JDBC in Java
JDBC stands for Java Database Connectivity, which is a standard Java API for
database-independent connectivity between the Java programming language and
a wide range of databases.
JDBC Architecture
JDBC architecture consists of two layers:
JAVA NOTES 22
JDBC Driver API: This supports the JDBC Manager-to-Driver connection.
JDBC Components
The JDBC API provides the following interfaces and classes:
HTML Basics
HTML stands for HyperText Markup Language, which is used to create electronic
documents (called pages) that are displayed on the World Wide Web.
HTML Tags
An HTML tag is a code that describes how a Web page is structured and
displayed. HTML tags are defined by the characters < and > .
Tag Description
<a> Anchor tag creates a link to other internet locations or files.
JAVA NOTES 23
Audio and
Not part of the specification Part of the specification
Video
Almost impossible to get true Helps identify location of user with the
Geolocation
geolocation of user help of JS Geolocation API
BLOCKQUOTE
BLOCKQUOTE tags are used to separate a section of text from
the surrounding text.
B and I Tags
B tags define bold text.
HTML Structure 🗂️
<html>
<head>
<!-- Head content -->
</head>
<body>
<!-- Body content -->
</body>
</html>
📊
JAVA NOTES 24
Table Tags 📊
COL Tags
COL tags define column properties for a table.
COLGROUP Tags
COLGROUP tags define groups of table columns.
TABLE Tags
TABLE tags define a table.
TR Tags
TR tags define a table row.
TD Tags
TD tags define a table data cell.
BUTTON Tags
BUTTON tags create a clickable button.
CAPTION Tags
CAPTION tags add a caption to a table.
CENTER Tags
CENTER tags center text, images, etc.
CODE Tags
CODE tags are used to indicate a code snippet.
JAVA NOTES 25
DD Tags
DD tags define a definition description.
DIR Tags
DIR tags define a directory list.
DL Tags
DL tags define a definition list.
DFN Tags
DFN tags emphasize a definition.
DIV Tags
DIV tags define a logical section of a web document.
HEAD Tags
HEAD tags contain metadata about the document.
Servlets 🚀
Definition
JAVA NOTES 26
Servlets are platform-independent because they are written in Java.
Architecture
HTTP Server
|
| HTTP Protocol
|
| Web Browser
|
| Servlet
|
| Database/Application
Servlet Tasks
Read the explicit data sent by the clients (browsers).
Read the implicit HTTP request data sent by the clients (browsers).
🌟
JAVA NOTES 27
Servlets and JSP 🌟
Creating a Servlet Example
To create a servlet example, there are 6 steps:
Create a Servlet
Servlet Interface
The Servlet interface provides methods that all Servlets must implement. These
methods are:
Method Description
init() Initialize the Servlet
service() Handle client requests
destroy() Clean up resources
HEAD: Asks for only the header part of what GET would return
JAVA NOTES 28
OPTIONS: Asks for a list of the HTTP methods to which the thing at the
requested URL can respond
TRACE: Asks for the loopback of the request for testing or troubleshooting
Features of JSP
Extension to Servlet: JSP is an extension to Servlet technology
Portable: JSP tags are processed and executed by the server-side web
container, so they are browser-independent and J2EE server-independent
Less code than Servlet: Reduces code using tags such as action tags, JSTL,
and custom tags
Elements of JSP
JAVA NOTES 29
There are 5 different types of scripting elements in JSP:
Let me know if you'd like me to add anything else! 😊## JSP Fundamentals 📚
JSP Comments
JSP comments are used to add notes about the code within a JSP page. They are
only visible in the JSP page and are not included in the servlet source code during
translation. They do not appear in the HTTP response.
"JSP comments are used to add notes about the code within a
JSP page."
Scriptlet Tag
The Scriptlet Tag allows you to write Java code inside a JSP page.
Example
<html>
<head>
<title>My First JSP Page</title>
</head>
<body>
<%
JAVA NOTES 30
int count = 0;
out.println(++count);
%>
</body>
</html>
Declaration Tag
The Declaration Tag is used to declare variables or methods. It is similar to
declaring a variable or method in a Java class.
Example
<html>
<head>
<title>My First JSP Page</title>
</head>
<body>
<%!
int count = 0;
%>
Page Count is: <%= ++count %>
</body>
</html>
Expression Tag
The Expression Tag is used to print the value of a Java expression.
JAVA NOTES 31
Syntax: <%= expression %>
Example
<html>
<head>
<title>My First JSP Page</title>
</head>
<body>
The result is: <%= 2*5 %>
</body>
</html>
Directive Tag
The Directive Tag is used to define page-dependent properties.
Example
JSP Architecture 🏢
JSP is part of a 3-tier architecture:
Web Server: receives the request and passes it to the JSP engine
1. The user makes a request to a JSP page via the internet in their web browser.
JAVA NOTES 32
3. The web server accepts the requested .jsp file and passes it to the JSP
engine.
5. The servlet output is sent via the internet to the user's web browser.
Download and Install Java: download the latest version of Java SE from
Oracle's website and install it on your system.
Download and Install Apache Tomcat: download the latest version of Apache
Tomcat from the Apache website and install it on your system.
Note: Make sure to set the environment variables for Java and Tomcat after
installation.
JAVA NOTES 33