JAVA UNIT-1
JAVA UNIT-1
UNIT - 1
Java :- Java is a high-level, object-oriented programming language. It is based on
the concept of "objects," which are instances of classes. Java programs can run
on any device or platform that supports the Java Virtual Machine (JVM), making
it platform-independent.
Java was designed to have strong exception handling, memory management,
and type-checking mechanisms, making it more reliable.
History of Java :-
Java’s story began in 1991 at Sun Microsystems (founded by Vinod Khosla,
Scott McNealy, and James Gosling) as part of a project code-named Oak.
James Gosling, often called the "father of Java," led the team that worked
on Oak.
In 1994, Oak was renamed Java after Java coffee, a type of coffee from
Indonesia.
Java was officially released to the public in 1995. The key philosophy
behind Java at this time was "Write Once, Run Anywhere" (WORA).
Sun Microsystems introduced the Java Development Kit (JDK), which
included the Java Runtime Environment (JRE) and other development
tools like the javac compiler.
Features of Java :-
Simple
Easy-to-learn syntax, similar to C/C++, but eliminates complex features like
pointers.
Object-Oriented
Supports core OOP principles: Encapsulation, Inheritance, Polymorphism,
and Abstraction.
Platform-Independent (WORA)
Java code is compiled into bytecode, which can run on any platform with a
compatible JVM (Java Virtual Machine).
Secure
Strong security features, including bytecode verification, access control,
and sandboxing for running programs safely.
Multithreading
Built-in support for multithreading, enabling the execution of multiple
tasks concurrently.
Robust
Strong memory management (automatic garbage collection), exception
handling, and type safety reduce errors and improve reliability.
High Performance
Just-In-Time (JIT) compiler for runtime performance optimization. Java also
supports multi-threading for faster execution.
Community and Ecosystem
Large developer community and a vast ecosystem of third-party libraries,
frameworks (like Spring, Hibernate), and tools (like Maven, Gradle).
Java Architecture (JDK, JVM, JRE) :-
Java Architecture refers to the components that make up the Java programming
language, which work together to enable Java applications to run on any
platform. The architecture is designed to be platform-independent, making Java
programs flexible and portable across different operating systems.
The most important components of Java architecture are:
1. JDK (Java Development Kit)
2. JVM (Java Virtual Machine)
3. JRE (Java Runtime Environment)
1. JDK (Java Development Kit)
The JDK is a software development kit that provides all the necessary tools to
develop Java applications. It includes the following:
JVM (Java Virtual Machine): The JVM is part of the JDK and is responsible
for running Java programs.
JRE (Java Runtime Environment): The JRE is also included in the JDK, as it
is necessary for running Java programs.
2. JVM (Java Virtual Machine)
The JVM is an abstract machine that enables Java programs to run on any
platform. The JVM is the engine that executes Java bytecode, making Java
platform-independent. Here are key points about the JVM:
Platform Independence: The JVM allows Java programs (compiled into
bytecode) to run on any platform that has a compatible JVM
Bytecode Execution: When Java source code is compiled, it is converted
into bytecode, which is executed by the JVM.
Memory Management: The JVM handles memory management, including
the allocation and deallocation of memory (via garbage collection).
JIT Compiler: The JVM uses a Just-In-Time (JIT) compiler to optimize
bytecode by converting it into native machine code at runtime for better
performance.
JVM Architecture
Class Loader:
Class Loader is a subsystem used to load class files. Class Loader first loads the
Java code whenever we run it.
JVM Memory
The Java Virtual Machine (JVM) is responsible for running Java programs and
managing memory during the execution of Java applications.
Native Interface
Java Native Interface works as a mediator between Java method calls and native
libraries.
Execution Engine:
It is the central part of the JVM. Its main task is to execute the byte code and
execute the Java classes.
3. Java Runtime Environment
It provides an environment in which Java programs are executed. JRE takes our
Java code, integrates it with the required libraries, and then starts the JVM to
execute it.
Java Tokens :-
Java, like other programming languages, uses a set of basic building blocks called
tokens. Tokens are the smallest units of a program that convey meaning. In Java,
tokens include keywords, identifiers, literals, operators, and punctuation.
1. Data Types in Java
Data types define the type of data a variable can store. In Java, data types are
divided into two categories:
Primitive Data Types: These are predefined data types in Java.
byte, short, int, long (for integers)
float, double (for floating-point numbers)
char (for single characters)
boolean (for true/false values)
Reference Data Types: These refer to objects and arrays.
String, arrays, classes, interfaces, etc.
2. Literals in Java
A literal is a constant value assigned to a variable. Java has several types of
literals:
Integer Literals: 100, 200L (suffix L for long integers)
Floating-point Literals: 3.14, 10.5f (suffix f for float)
Character Literals: 'A', 'Z'
String Literals: "Hello, Java!"
Boolean Literals: true, false
3. Variables in Java
Variables in Java store data values and can be classified based on their scope
(where they are accessible) and lifetime (how long they exist).
Local Variables: Declared inside methods, blocks, or constructors.
Instance Variables: Declared in a class but outside any method.
Static Variables: Declared with the static keyword; shared across all
instances of the class.
Global Variables: In Java, there is no true concept of global variables as all
variables must be declared within a class. However, you can achieve a
similar effect by using static fields in a class.
4. Scope and Lifetime of Variables
Scope: Refers to the region of the program where a variable is accessible.
o Local variables have scope within the method or block in which they
are declared.
o Instance variables have scope throughout the class, accessible by any
method.
o Static variables are accessible throughout the class, similar to
instance variables, but shared among all objects.
Lifetime: Refers to how long a variable exists in memory.
o Local variables exist only during the execution of the method or
block.
o Instance variables exist as long as the object exists (until garbage
collected).
o Static variables exist as long as the class is loaded in memory.
5. Operators in Java
Operators perform operations on variables and values. Java provides different
types of operators:
1. Arithmetic Operators: +, -, *, /, % (for mathematical calculations)
2. Relational Operators: ==, !=, >, <, >=, <= (for comparisons)
3. Logical Operators: &&, ||, ! (for logical conditions)
4. Assignment Operators: =, +=, -=, *=, /= (for assigning values)
5. Unary Operators: ++, --, +, -, ! (operate on a single operand)
Java Control Statements :-
Control statements in Java allow the program flow to be directed based on
conditions or loops. These statements control the flow of execution and include:
1. Decision-Making Statements
o if statement
o if-else statement
o switch statement
2. Looping Statements
o for loop
o while loop
o do-while loop
3. Jump Statements
o break
o continue
1. Decision-Making Statements :-
Decision-making statements evaluate the Boolean expression and
control the program flow depending upon the result of the condition provided.
There are two types of decision-making statements in Java, i.e., If statement and
switch statement.
If Statement :
The if statement evaluates a condition and if the condition is true, the block of
code inside the if statement is executed.
If-else Statement :
The if-else statement provides two branches: one for when the condition is true
and one for when it's false.
if-else-if ladder :
The if-else-if statement contains the if-statement followed by multiple else-if
statements. In other words, we can say that it is the chain of if-else statements
that create a decision tree where the program may enter in the block of code
where the condition is true.
Nested if-statement :
In nested if-statements, the if statement can contain a if or if-else statement
inside another if or else-if statement.
Switch Statement :-
The switch statement contains multiple blocks of code called cases and a single
case is executed based on the variable which is being switched. The switch
statement is easier to use instead of if-else-if statements. It also enhances the
readability of the program.
2. Loop Statements :-
In programming, sometimes we need to execute the block of code repeatedly
while some condition evaluates to true. However, loop statements are used to
execute the set of instructions in a repeated order.
for Loop :
The for loop is used when you know how many times you want to execute a
statement or a block of statements.
while Loop :
The while loop is used when you want to repeat a block of code an unknown
number of times, based on a condition.
do-while Loop :
The do-while loop is similar to the while loop, but the condition is checked after
the block of code is executed. This ensures that the block is executed at least
once.
for-each loop :
Java provides an enhanced for loop to traverse the data structures like array or
collection. In the for-each loop, we don’t need to update the loop variable.
Jump Statements :
Jump statements are used to transfer the control of the program to the specific
statements. In other words, jump statements transfer the execution control to
the other part of the program.
break Statement
The break statement is used to break the current flow of the program and
transfer the control to the next statement outside a loop or switch statement.
However, it breaks only the inner loop in the case of the nested loop..
continue Statement :
The continue statement doesn’t break the loop, whereas, it skips the specific
part of the loop and jumps to the next iteration of the loop
immediately.
Java array :-
Java array is an object which contains elements of a similar data type.
Additionally, The elements of an array are stored in a contiguous memory
location. It is a data structure where we store similar elements. We can store
only a fixed set of elements in a Java array.
✅ 1. Default Constructor
A default constructor is a constructor without any parameters.
If you do not define any constructor in your class, Java provides a default one
automatically.
2. Parameterized Constructor
A constructor that takes arguments to initialize the object with custom values.
Java. It is used to provide initial values to the object during creation.
✅ 3. Copy Constructor (User-defined)
Java doesn't provide a built-in copy constructor like C++, but you can define one
manually to copy values from another object.
3. Protected (protected):
The class, method, or variable is accessible within the same package and
means the class, method, or variable is accessible only within the same
package.
🔷 Method Overloading in Java
Method Overloading is a concept where multiple methods with the same name
exist in the same class but with different parameters (either in the number of
parameters or the type of parameters).
✅ 1. Static Variable
A static variable is shared among all objects of the class. It is created only once
in memory, at the time of class loading.
✅ 2. Static Method
A static method belongs to the class, not to instances. It can be called without
creating an object.
✅ 3. Static Block
A static block is used to initialize static data. It runs only once, when the class is
loaded into memory.
✅ 1. Final Variable
✅ 3. Final Class
A Wrapper Class in Java is used to wrap (convert) a primitive data type into an
object.