Core-Java Material
Core-Java Material
Project
Collection of Packages
Package:
Collection of Classes
Class
Collection of Variables and methods
Data Types in JAVA:
byte
short
int
long
double
float
char
Boolean
String-> Objective Data Type
What is Variable?
.Which store the value
Example:
Int id=10;
variable
int 10
value
the ID consists value id is 10
DitaType
Types of Variables:
Instance Variables:
These will declare in class level
.Which are stored in Heap memory Area
.For instance variables we should create the Object or Instance
reference Constructor
Static variables:
B y using static key word to reference variable we can make variable as
static variable
Static int id=10;
.We call static method by using
ClassName.VariableName;
className.varaible
.Static variables are stored in method memory area
Local Variables:
.Wecan provide local variables inside method
Which are stored in Stack Area.
Methods:
return Type
Access
mpdifier methodName
return Statement
T h e above method is instance method
For calling this method we need to create Instance or Object
override the private and final methods
we can't
Static methods:
If you are adding the static keyword to instance method, which static method.
We can static methods
Class Name.methodName();
we can't override static nmethods
which will stored in method area.
Constructor:
constructor is for initializing the data
in Object it contains constructor.
Employee ee=new Employeel);
It is two types
1) No argument constructor 2) argument constructor
b y default java will provide default zero argument constructor for every class
i f we provide argument constructor in class, the default constructor will be
override,
if you still want zero argument constructor you have to provide manually.
Encapsulation:
Encapsulation means protected the data in a class
By creating private properties and public setters and getters methods.
Example:
I f you are taking accountholder details, accountholder can able to check all his
data including balance, but for others we need to show name, IFSC, account
number, but not he balance
Here we are making properties as private and protecting the data.
Inheritance:
Polymorphism:
Poly means many, morphism mean changes
One things in different forms
It is two types.
1) Compile time polymorphism(Method overloading)
2) Run time polymorphism(Method overriding)
Method Overloading
Method name is same, parameters list are different in same class
Either number of parameters list are different, or data types are different or
order of parameters are different
For example:
Overriding:
Method name is same, parameter's also same but in different classes
override private, final and static methods.
we can't
For Example:
@Override
publicvoid loans()
System.out.println( "HDFC loans method");
Boolean cibiliscore=cibilScore();
if(cibiliScore)
Svstem.out.println("Approved
else {
loan");
System.out.println("you don't have suficient creditscore");
Interface:
Abstract:
abstract is a keyword
by adding abstract keyword to class we can make class as abstract
we can provide implementation for the abstract class in child class only
i t allows abstract methods and non-abstract methods
it allows instance blocks and static blocks
we can create constructor for abstract class
Exception Handling:
Exception is a class, which is child class of Throwable
Throwable contains child classes Exception & Eror
t disturbs the normal execution flow of the programme
We can handle the exceptions by using try, catch blocks
Or by using throws keyword
One try block may contain multiple catch blocks and one finally block
B u t for providing catch block or finaly block try block is mandatory.
Checked Exceptions:
1) ArithmeticException
if you are divided somethingValue/0, you will get arithmetic exception
ex:
Nul1PointerException:
public void nullpointer()
System.out.println("Nul1Pointer start ");
try {
Integer i=null;
int j-10
System.out.println(i+j);
catch (Nul1PointerException e)
e.printStackTrace ();
System.out.println("Nul1Pointer end ) ;
NumberformatException:
public void numberFormat Exception() {
System.out.println("number format start");
try
String id="teja";
Integer emp id=Integer. value0f(id);
System.out.println(emp_id);
}catch (NumberFormatException e) {
e.printstackTrace( )
System.out.println("number format end");
CloneNotsupportedException:
When you are write clone(), which is for Object Copy, then if that class is not implemented
to Clonable interface will get CloneNotSupportedException
NotserializableException:
if while serialization if you not provided serialversionUID the serializable implemented
class, you will get this exception.
I n this case server will run continuously running, till fill the stack memory
Final is a keyword
Final class we can't extend
>Final method we can't override
Final variable we can't re-initialize
Finally:
Finally is block, which we can use wether we getting exception or not if you wants to
print some data we can provide the data inside of finally block
Finalize()
i t is a Object class method, the garbage collector before calling un-usable objects it'll
call the finalizel) method
Serialization& de serialization:
I f you are not provided serialVesion UID will get not serializable exception
Static and transient variable's are not serailized
Clonable:
tis markerInterface
Which will use for to write the Clone();
Clonel) is copy of Object
When you are writing clonel), the class should be implements to Clonable interface
otherwise will get ClonNotSupportedException
Garabge Collector:
Garbage collector will call the unusable Objects
String s=null;
String s1 "Teja IT";
String s2=s1;
SObject is unusable Object
So, the garbage collector will call the unusable Object
I f we want call the externally the garbage collector, we need to use
System.gcl0
Garbage collector before collecting unusable Objects it will call the Object class
finalizel);
Access Modifiers:
private:
private class we can't extend
private method we can't override
private variable we can't out side of the class
protected:
package com.AcessModifier;
public class A
protected int acsess id=10;
protected void shop() {
System.out.println("A class shop method");
package com.AcessModifier;
public class B
public static void main(String[] args)
A a=new A();
System.out.println (a.acsess_id);
a.shop()
Collection:
interface
Iterable class
implements
Collection extends
List Queue
PriorityQueue HashSet
ArrayList
LinkedHash Set
Deque
LinkedList ***
Vector SortedSet
Array Deque
Stack TreeSet
Arraylist:
Arraylist is a class, which is child class of List interface
ArrayList is index based structure in Java
Arraylist is re-sizable array
ArrayList initial capacity is 10
Arraylist size is increased to double
Arraylist implements to RandomAccess interface
Get method of arraylist directly gets the element on specific index
U s e arraylist when get operations is more frequent than add and remove operations
Arraylist is Asynchrounous
LinkedList:
Linked datastructure is Node
New node will create for storing new element
Initial capacity Zero
For storing every element node will create
Linked list doesn't implementsto Random access
It is more preferable to add and remove the elements
Linkedlistis Asynchronous
Vector:
Stack:
List Set
Maintain insertion order doesn't maintain insertion order
List allows duplicate values don't allow duplicate values
Allow many null values it adds only one null value
Get method allow list, to get the value don't support get()
From specific index
Araylist, linkedlist, vector are the childs Hashset, linked Hashset, treeset are child
We can iterate the data with listiterator it works with iterator only
Also
List are re-sizable array it internally follows map datastrcture
Hashset:
LiskedHashset:
Treeset:
Comparable Comparato
i t used to compare instances of same compare instances same or different
class class
which is for natural sorting order which is for customize sorting order
comparable implements by default >we need to implement externally
all wrapper classes, String Integer,Long
Date etc,
original class must implements class itself implements Comparator
Comparable or any other class can implements
Provide sorting one criteria only it provides sorting many criteria's
CompareTol) Comparel)
>Java.lang java.util
Comparable Example:
public class BirlaProducts implements Comparable <BirlaProducts»{
private int product id;
private String product name;
private long cost
public BirlaProducts (int product id, String product name, long cost) {
super();
this.product_id = product_ids
this.product_name = product_name;
this.cost = cost;
Comparator Example:
public class Pidilite{
int product id;
String prodcut name;
int price;
public Pidilite(int product id, String prodcut name, int price) {
super();
this.product_id = product_id;
this.prodcut_name = prodcut_name;
this.price price;
public int getProduct id() {
return product id;
public void
static main(String[] args) (
Pidilite pi=new Pidilite(1, "m-seal", 35);
Pidilite p2=new Pidilite(2, "fevistick", 15);
Pidilite p3=new Pidilite(3, "fevicol", 40);
Pidilite p4=new Pidilite(4, "fevikwick", 10);
ArrayList<Pidilite al=new ArrayListPidilite2();
al.add(p1);
al.add(p2);
al.add(p3);
al.add(p4);
Collections.sort (al, new
BasedOnPrice 0);
for (Pidilite p:al)
System.out.println(p-getPrice());
System.out.println();
Collections.sort (al,new BasedOnName() );
for (Pidilite p:al)
System.out.println(p.getProdcut_name());
MAP::
Map Interface
intertace>>
ccinterface>
oTedMao
-implements TreeMap
extends LinkedHashMap
M a p is a interface
i t is combination of key, value pair
w e can call key, value pair is an entity
key is Object and value also a Object
HashMap:
HashMap is a class
which implements to map interface
it doesn't follow any insertion order
the values will stored in Hash buckets
it doesn't allow duplicate keys, values may be duplicate
i f you provide the duplicate key's it will override
i t allows one null key & many null values
n o t legacy class, Asynchronous
Internal working:
the data will be stored in Hash bucket
the default size of hashmap is 16
based hashing technique values will be stored in hashbucked
while storing the value it will compare the hashcode(), and compare with .equals()
if value is present in hashbucket, it will override
the null key Object will stored Zero th hashbucket.
Hashing Collision:
i f two different objects contain same hashcode then both will be stored in same bucket
this nothing but Hashing collision, we can achieve this problem by using quadratic
algorithm.
Linked HashMap:
i t ' s similar like Hashmap, butitfollows insertion order
weakHashMap:
in map, garbage collector can't collect un-usable objects,
but in weakHash Map 8garbage collector can able to collect un-usable Objects.
Remaining functionality working like HashMap Only.
HashTable:
i t is a legacy class
i t doesn't allow null key or null value
it extends to Dictionary class
it is Synchronized
it is threadsafe
Concurrent HashMap:
it is a class
we can say it is a combination of HashMap and Hashtable
i t not synchronized
but threadsafe
because, on each bucket at time 1 read thread and 1 write thread can be act
i s it is threadsafe and synchronized
1/Unmodifieblelist
List Integer> 1=Collections.unmodifiablelist (list) ;
1.add(12); for(Integer i:1)(
System.out.println(i);
i f you don't want to modify the list data, then you can go with Collection.unmodifablelist()
s t i l l if you are trying to modify the list data you will get UnsupportedOperationException