0% found this document useful (0 votes)
4 views35 pages

JAVA UNIT-1

This document provides comprehensive notes on Java, covering its history, features, architecture, and key concepts such as data types, control statements, arrays, classes, constructors, and memory management. It emphasizes Java's platform independence, object-oriented nature, and robust security features, alongside practical examples and explanations of various programming constructs. Additionally, it discusses important keywords like 'this', 'static', and 'final' that are integral to Java programming.

Uploaded by

f49077041
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views35 pages

JAVA UNIT-1

This document provides comprehensive notes on Java, covering its history, features, architecture, and key concepts such as data types, control statements, arrays, classes, constructors, and memory management. It emphasizes Java's platform independence, object-oriented nature, and robust security features, alongside practical examples and explanations of various programming constructs. Additionally, it discusses important keywords like 'this', 'static', and 'final' that are integral to Java programming.

Uploaded by

f49077041
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 35

JAVA NOTES

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.

Types of Arrays in Java


1. Single-Dimensional Array :- A single-dimensional array in Java is a linear
collection of elements of the same data type. It is declared and instantiated
using the following syntax.
2. Multi-Dimensional Array :- A multidimensional array in Java is an array of
arrays where each element can be an array itself. It is useful for storing data in
row and column format.

Anonymous Array in Java :-


Java anonymous arrays eliminate the requirement for separate declarations of
array variables by enabling developers to build and initialize arrays directly
within method calls or other expressions.
ArrayIndexOutOfBoundsException
In Java, the ArrayIndexOutOfBoundsException is a runtime exception thrown by
the Java Virtual Machine (JVM) when attempting to access an invalid index of an
array. This exception occurs if the index is negative, equal to the size of the array,
or greater than the size of the array while traversing the array.
1. //Java Program to demonstrate the case of
2. //ArrayIndexOutOfBoundsException in a Java Array.
3. public class TestArrayException{
4. public static void main(String args[]){
5. int arr[]={50,60,70,80};
6. for(int i=0;i&lt;=arr.length;i++){
7. System.out.println(arr[i]);
8. }
9. }}

Addition of Two Matrices in Java


Difference Bitween

1) Decision-Making Statements And Looping Statement


2) Break And Continue.
3) Procedure-based Programming and Object-Oriented Programming
4) Java and C++
🔷 What is a Class in Java?
A class in Java is a user-defined blueprint or prototype from which objects are
created. It is a fundamental concept in Object-Oriented Programming (OOP) that
encapsulates data (fields/properties) and behavior (methods/functions)
together.
✅ What is a Constructor?
A constructor in Java is a special method that is automatically called when an
object of a class is created. It is used to initialize the object (i.e., set the values
of its properties).

🔷 Types of Constructors in Java


Java mainly supports three types of constructors:
1. Default Constructor
2. Parameterized Constructor
3. Copy Constructor (user-defined)

✅ 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.

Object Access Modifiers in Java


In Java, access modifiers determine the visibility and accessibility of classes,
methods, and variables. They specify where the class, method, or variable can
be accessed from.
1. Public (public):
 The class, method, or variable is accessible from anywhere in the

application (both inside and outside the package).


2. Private (private):
 The class, method, or variable is accessible only within the same class.

 Cannot be accessed from outside the class.

3. Protected (protected):
 The class, method, or variable is accessible within the same package and

by subclasses (even if they are in different packages).


4. Default (No Modifier):
 If no access modifier is specified, it is considered default access, which

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).

Important Note: Method overloading is resolved at compile time, so it's an


example of compile-time polymorphism.
✅ What is Garbage Collection?
Garbage Collection (GC) in Java is the process of automatically freeing memory by
destroying unused objects. Java provides automatic memory management, so you don’t
need to explicitly deallocate memory (like in C/C++ using free()).

 Prevents memory leaks.


 Improves application performance.

🔹 When is an object eligible for garbage collection?


An object becomes eligible for GC when it is no longer referenced by any part of the program.

Student s = new Student(); // s points to the object


s = null; // Now the object is unreferenced → eligible for GC

🔹 Invoking the Garbage Collector Manually


Java allows you to request garbage collection using:

System.gc(); // Requests JVM to run Garbage Collector


Note: It's just a request. The JVM decides when to actually perform GC.

✅ WHAT IS THIS KEYWORD IN JAVA?


In java, the this keyword is a reference variable that refers to the current
object of the class.
It is commonly used to resolve ambiguity when instance variables and
constructor/method parameters have the same names.

Static keyword in java :-


The static keyword in java is used for memory management. It can be applied
to variables, methods, blocks, and nested classes. A static member belongs to
the class itself, rather than to any specific instance of the class.

✅ 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.

🔷 final Keyword in Java :-


The final keyword in Java is used to restrict modification. It can be applied to
variables, methods, and classes, and each usage has a different purpose.

✅ 1. Final Variable

A final variable becomes a constant. You must initialize it at declaration or in a


constructor (for instance variables).
✅ 2. Final Method

A method marked as final cannot be overridden by any subclass.

✅ 3. Final Class

A class declared as final cannot be extended.


🔷 What is a Wrapper Class?

A Wrapper Class in Java is used to wrap (convert) a primitive data type into an
object.

Java is an object-oriented language, and many libraries (like Collections) work


only with objects, not primitives. So, Wrapper classes are used to convert
primitive types into objects.
Implementation
✅ Why Java is Platform Independent

Java is considered platform independent because of its "Write Once, Run


Anywhere" capability. This means you can write a Java program on one
platform (like Windows) and run it on any other platform (like macOS, Linux)
without changing the code.

You might also like