Java Notes by Harshal Chaudhari
Java Notes by Harshal Chaudhari
CHAUDHARI
1. Beginner friendly
2. Simple & open source
3. Object oriented language
4. Secured language
5. Platform Independent
6. Most in demand
7. Multi Language support-English, Marathi, Hindi,
Kannada, telugu, Frech, Japanese etc.
ASCII values--->Unicode
128-->64k+
a-z, A-Z, 0-9, all symbols
1. Deep community support-20lac java programer
Language Comparison
History of Java
● Invented in 1995 - James Gosling- SunMicrosys-
Oracle
● First name given to java language as Oak—>
Java
● Java latest-17.0.1
● High level programming language→ Humans can
read it.
● WORA- Write Once Run Anywhere
_____________________________________________
________________________
Java Applications
_____________________________________________
_______________________
Java Downloading
Java Versions
Starting Java 1.0 to 1.8, 9,......17
Methods/functions/tasks
Methods Datamembers
Functions/ Characteristics/
tasks Features
Person Eat, sing, Height, colour,
dance, sleep weight, age
Online Payment () Sender name,
Payment { mobile no,
—-----Code of Account Num,
that method Balance
}
//task-money
transfer
Mathematical Addition() int x=10
Operation { int y=20
int z;
z=x+y;
//z=10+20=30
Print z=30
}
Data Type
Data types are used to represent the type of
data/information which we want to use in a java
program.
Ex- number, name
- In java programming it is made mandatory to
define/declare datatype before the declaration
of variable
- Ex- int a=10;
int→datatype, a→variable
Types of DataTypes
A. Primitive DataTypes
● Memory size of Primitive data types is
fixed
● All the primitive datatypes are keywords
● There are 8 types of primitive datatypes
are available- byte, short, int, long, float,
double, char, Boolean.
32GB=32*1024MB
1GB=1024MB
1Mb=1024KB
1KB=1024 bytes=1024*8= bits
1 byte= 8 bit
Bit→0 and 1
8 7 6 5 4 3 2 1
+ Or 0 0 0 0 0 0 1
-
2. Fractional DataTypes
3. Character
Variables (Part-I)
● Variable are nothing but a piece of memory
used to store some information
Ex- int i=10;
● One variable can store one information at a
single time
● Variable can be used for information
reusability
● Steps to be followed to utilise variable in
program-
1. Variable declaration
2. Variable Initialization
3. Variable Usage
Rules for Variables-
1. Uppercase(A-Z), lowercase(a-z), digits(0-9),
Underscore (_), dollar($)
2. First character must be letter
3. Blank spaces are not allowed
4. No java keywords can be used as variables
5. Variable are case sensitive
Operators in Java
Operator is nothing but a symbol which informs the
compiler to perform specific mathematical operation
or logical operations
Types of Operators
Type of Operator Example Result
Operator symbol (int A=8, B=3,
C=5)
+ A+B 11
- A-B 5
Arithmetic * A*B 24
Operators
/ A/B 2
% A%B 2
Remainder
< A<B false
<= A<=B false
Relational > A>B true
Operators
>= A>=B true
== A==B false
!= A!=B true
Not Equals
&& (A<B) && False
AND (B>C)
Logical T T T
Operators
T F F
F T F
F F F
|| (A==B) || (B! True
OR =C)
T T T
T F T
F T T
F F F
! ! (A>B) false
NOT
+= A+=B → 11
A=A+B
-= A-=B → 5
Assignment A=A-B
Operator *= A*=B → 24
A=A*B
/= A/=B → 2
A=A/B
%= A%=5 → 3
A=A%5
=a=b Value of b will
be assigned
to a
Conditional Statements/ Decision Making/
Selection
else
{
Statement(s)-
if
condition is
false
}
4.Nest
if(main
ed_if
condition)
{
Statement(s)-
if main
condition is
true
if(sub
condition)
{
Statement(s)-
if condition is
true
}
else
{
Statement(s)-
if sub-
condition is
false
}
}
else
{
Statement(s)-
if main
condition is
false
}
5.Swit
switch (key) {
ch
case case value1:
break;
case value2:
break;
case value3:
break;
default:
break;
}
Logical Loops
The logical loops in java are essential to
execute a block of code multiple times.
Following loops are present in java-
1.For Loop
2.While Loop
3.Do-While Loop
4.For each Loop
All these types of loops execute a set of
repeated statements as long as a
given/specified condition remains true.
This particular condition is called a loop
control.
//for( variable
declarartion &
initialization ;
condition ;
increment/decr
ement of value)
{
// body
}
//sequence of
cursor
movement
1. init
2. condition
verify - 5,8
3. for loop block
- 6,9
4. i/d (++/--)
- 7,10
11. (in case of
condition false)
for loop exit
2 Whi
le
3 Do
Whi
le
Collection / Arrays
4 For Mostly used
eac for(DataType
h localVariable:datasource)
{
}
Java Keywords:
These are reserved words
Methods:
Main Method
Static
● static is a keyword in java
● Whenever static is preceded by any class
properties then for that memory is allocated only
once in the program.
● Static method is executed only once in the
program.
● main() method of java executes only once
throughout the java program execution and
hence it declares must be static.
Void
● void is a special datatype also known as no
return type, whenever it is preceded by main()
method that will be never return any value to the
operating system.
● main() method of java is not returning any value
and hence its return type must be void.
String args[ ]
String args[ ] is a String array used to hold command
line arguments in the form of String values.
· String[] args
· String args[]
· String []args
of
package com.aspire.java.Methods;
Types of Constructor
1. Default Constructor- we don't write, it is provided
by compiler
2. User defined Constructor--we actually write in
program
Method Constructor
OOPs
Object Oriented Programing
1.Inheritance-The process of
obtaining/acquiring features ( the data
members and methods) from one class
(Parent class/base class/main class) to
another class (child class/derived/sub
class) is known as inheritance.
3. Encapsulation-Encapsulation is a
process of wrapping data and methods
in a single unit is called encapsulation.
4.Polymorphism-The process of
representing one Form in multiple
forms is known as Polymorphism
1.Inheritance
2. Multilevel Inheritance:
● Multilevel Inheritance takes place between
3 or more than 3 classes.
3. Multiple Inheritance:
5. Hybrid
1. local variable
● Creating variable inside method/block is
known as local variable.
● Scope of local variable remains only within
the method & they are temporary.
2. global variable
● Creating variable outside method/block is
known as global variable.
● Scope of global variable remains throughout
the class & they are permanent.
3. class/static variable
● Declaring the variable using a static
keyword is known as class/static variable
because to access a static variable class
name is used.
● To access static variable from diff class we
need to make use of below statement:
● Syntax:- classname.variablename;
4. Instance/non-static variable
● To access non-static variable we need to
make use of below statement:
-Create object of class.
Syntax:- objectname.variablename;
Static data Non static data
member- member-Global
Global
Static Yes No
block/method
Non static Yes Yes
Block/method
1. private
2. default
3. protected
4. public
1. private:
● If you declare any member of class as private
then scope of that member remains only
within the class
● It can't be accessed from other classes.
2. default:
● If you declare any member of class as
default then scope of that member remains
only within the package
● It can't be accessed from other packages.
● There is no keyword to represent the default
access specifier.
3. protected:
● If you declare any member of class as
protected then scope of that member
remains only within the package that class
which is present outside the package can
access it by one condition ie. inheritance
operation (child class ref)
4. public:
● If you declare any member of class as public
then scope of that member remains
throughout the project.
Polymorphism
● It is one of the OOPs principle where one
object shows different behaviours at different
stages of the life cycle.
● Polymorphism is a Latin word where poly
stands for many & morphism stands for forms.
● In java Polymorphism is classified into 2
types:
1. Compile time Polymorphism
2. Runtime Polymorphism
Abstraction
Abstraction is one of the oops principles in java.
Abstraction means hiding the implementation code and
providing only functionality to the end user is called
abstraction.
the concept of exposing only the required essential
characteristics and behaviour with respect to a context.
Abstract Class:
● Abstract class is defined as a class which
is declared with an "abstract" keyword.
Ex- abstract public class A
Concrete class:
It is defined as a class which provides definitions
for all the abstract/ incomplete/undefined methods
which are present in abstract class with the help of
extends keyword(inheritance).
Interface
It is one of the oops principles.
It is used to achieve 100% abstraction.
Interface is used to declare only incomplete
methods in it.
Features of Interface:
1. Methods declared inside an Interface are by
default public & abstract.
2. Variables or data members declared in
Interface are by default static and final.
3. Interface don’t have Constructor inside it.
4. We can not create object of Interface directly.
5. To create object of Interface programmers
need to make use of the Implementation class.
6. Interfaces support multiple inheritance.
Implementation class:
It is defined as a class which provides definitions
for all the abstract or incomplete methods those
are present in interface with the help of
implements keyword.
Benefits of using Interface
· We can achieve fully abstraction(100%).
Parent
1 More 1 More
Parent parent parent parent
class class interfac interfac
e es
Child 1 child extend X implem implem
class s ents ents
More extend X implem Implem
child s ents ents
class
1 child X X extend extend
interfac s s
e
More X X extend extend
child s s
interfac
e
Generalization:
● Generalization is the process of extracting
shared characteristics from two or more
classes and combining them into a
generalized superclass.
● Generalization files can be normal java
class or abstract class or Interface, but
only Interface is recommended.
____________________________________
____________________
Encapsulation
● Encapsulation is a process of wrapping data
and methods in a single unit.
● Encapsulation is achieved in java language
by class concept.
● Encapsulation can be achieved using class
keyword, state represents declaration of
variables on attributes and behaviour
represents operations in terms of method.
Benefits of encapsulation
● Provides abstraction between an object and
its clients.
● Protects an object from unwanted access by
clients.
● Example: A bank application forbids (restrict)
a client to change an Account's balance
Casting:
Converting one type of information into another
type is called casting
In java casting is classified into 2 types:
1. Primitive casting-deals with primitive
data type
2. Non-primitive casting-
1. Primitive casting:
● Converting one data type of information
into another data type is called casting
● Primitive casting is classified into 3
types:
1. implicit casting
2. explicit casting
3. boolean casting
i. Implicit casting:
● Converting lower data type info into
higher data type info is called implicit
casting.
● Implicit casting is also called widening
casting, where memory size goes on
increasing,
Eg. byte a =10;
short b;
int c;
long d;
float e;
double f;
b=(short)a; // typecast a into
short
b=a;
c=a; // c=(int) a; // typecast a into
int
d=a;
e=a;
f=a;
c=b;
e=c;
i. Up casting:
● Assigning subclass/child class property into
superclass/parent class is called up-casting.
● Up-casting can be performed only after
performing an inheritance operation
● After performing inheritance, the property
which are present inside superclass/parent
class comes into subclass/child class
● In the subclass programmer can declare new
properties.
● At the time of up-casting the properties which
are inherited from superclass are only eligible
for the up-casting operation.
● The new properties which were declared
inside subclass are not eligible for up-casting
operation.
methods Global
Variables
Child Child class
c=new methods
Child();
Parent p Parent class
=new methods
Parent();
Parent Child class
object=n methods
ew which are
Child(); inherited from
parent class
Types of Arrays
1.Single Dimensional Array
1. int[] a;
2. int a[];
3. int []a;
2.Multidimensional Array
1. int[][] a;
2. int a[][];
3. int [][]a;
4. int[] a[];
5. int[] []a;
6. int []a[];
j=0 1 2 3 4 5
i=0 a[0] a[0] 1 14
[0]=11 [1]=12 3
1 a[1] a[1] 1 18
[1]=15 [1]=16 7
2 19 20 2 22
1
3
4 a[4]
[5]
Array creation
1. String ar[] = new String[5]
ar[0]=
ar[1]=
ar[2]=
ar[3]=
ar[4]=
2. int ar []= {30,40,50,20,10};
3. ar.length --> Length/size of Array
4. Array index --> ar[i][j]
5. Arrays.sort(ar)
String
1. String is a non-primitive data type, memory
size is not fixed.
2. String is used to store collection of characters
3. String is a class present inside the "java.lang"
package.
4. String class is the final class that can't be
inherited to other classes.
5. At the time of String declaration, initialization,
object creation takes place.
6.String objects are immutable in nature/can’t be
changed.
7. Object creation of String can be done in 2 ways:
1. without using new keyword
2. Using new keyword
8. String objects are going to get stored inside the
String pool area which is present inside the heap
area.
Methods/functions of String:
1. toUpperCase();-
2. toLowerCase();
3. length()
4. contains()
5. equalsIgnoreCase()
6. equals(s1)
7. isEmpty()
8. charAt(3)
9. endsWith("")
10. startsWith("Institute")
11. substring(4)
12. substring(4, 7)
13. concat(s1)
14. indexOf('')
15. lastIndexOf('')
16. replace("", "")
17. String ar[] = s4.split("")
1. List(I)
2. set(I)
3. queue(I)
1. List(I)
1. Duplicate are allowed in list
2. Allows any number of null
values
3. Order of insertion - maintain
Types of List:
1. Arraylist (IC)
2. Vector (IC)------Legacy class
3. LinkedList (IC)
2. Set(I)
1. Doesn't allow duplicate
2. allow only 1 null value (except
treeset)
3. order of insertion - random
insertion (except linkedHashset)
Types of Sets:
1. Hashset (IC)
2. LinkedHashset (IC)
3. Treeset (IC)
3. queue(I)
1. priority Queue (IC)a
# Cursor
1.Iterator-- all the collection object --universal
cursor
2.List Iterator -- only for list interface type
implementation classes --not universal cursor
3.enumeration -- Legacy class(vector) --not
universal cursor
4.enumeration
1)Only applicable for legacy classes and hence it
is not a universal cursor.
2)By using enumeration we can get only read
access.
3)Enumeration and iterator only forward direction
cursor not backward.--single directional cursor
1. List (I):
Types of List:
1. Arraylist (IC)
2. Vector (IC)------Legacy class
3. LinkedList (IC)
1. Arraylist(IC)
1. duplicate are allowed in Arraylist
2. allows any no of null values
3. order of insertion-maintain
4. Default capacity for Arraylist is 10
5. data structure: Resizable
6. Incremental capacity= (current capacity*3/2)+1
7. best choice: retrieval operation (random access
interface is implemented in Arraylist & vector)
8. worst choice: manipulation operation i.e.
insertion in between Arraylist or delete ()
9. storage type: index
2. Vector
1. duplicate are allowed in vector
2. allows any no of null values
3. order of insertion-maintain
4. Default capacity for Vector is 10
5. data structure: doubly
6. Incremental capacity= current capacity*2
7. best choice: retrieval operation (random access
interface is implemented in Arraylist & vector)
8. worst choice: manipulation operation i.e.
insertion in between Vector or delete ()
9. Vector is a legacy class.
10. storage type: index
3. LinkList
1. duplicate are allowed in Arraylist
2. allows any no of null values
3. order of insertion-maintain
4. no Default capacity in linkedlist
5. data structure: linear
7. best choice: manipulation operation i.e.
insertion in between linkedlist or delete()
8. worst choice: retrieval operation
9. storage type: index
2. Set(I)
1. doesn't allow duplicate
2. allow only 1 null value(except treeset)
3. order of insertion-random insertion
Types of sets:
1. Hashset(IC)
2. LinkedHashset(IC)
3. Treeset(IC)
1.Hashset:
1. Doesn't allow duplicate values
2. Allow only 1 null value.
3. order of insertion-random insertion
4. no default capacity
5. DS: Hashtable
6. storage type: hashtable
7. best choice: To remove duplicate elements
when order of insertion is not
Mandatory.
2. LinkedHasSet:
1. Doesn't allow duplicate values
2. Allow only 1 null value.
*3. order of insertion-maintained
4. no default capacity
*5. DS: Hybrid (liner+ hashtable)
6. storage type: hashtable
7.best choice: To remove duplicate elements
when order of insertion is
Mandatory
3. TreeSet:
1. doesn't allow duplicate
2. null values: not allowed/ no null value
*3. order of insertion- Ascending order.
4. no default capacity.
5. DS: Balanced tree
6. storage type: hashtable
7.best choice: To remove duplicate elements
when order of insertion
Ascending order.
Note: we can store only homogeneous data
in TreeSet
Common program to compare all Set & List
Classes of Collection
Map/HashMap
● Key-Unique
● Values-May be unique or duplicate
● Data type of key and value- May be same or
different.
● Data storage- Ascending order as per key