Java Notes
Java Notes
Simple
Object-Oriented
Portable
Platform independent
Secured
//JVM
JVM-java virtual machine
1. It provides run time environment in which java bytecode can be executed.
2. Tasks of JVM are-
Loads code
verifies code
Execute code
3. It is called a virtual machine because it doesn't physically exist.
//JRE
JRE-java runtime environment
1. It is the implementation of JVM.
2. It physically exists.
3. It contains a set of libraries + other files that JVM uses at runtime.
//JDK
JDK-java development kit
1. It is a full featured software development kit.
2. It contains JRE + Development tools.
JVM, JRE, and JDK are platform dependent because the configuration of each OS is
different from each other.
/*Wrapper classes provide a way to use primitive data types (int, boolean, etc..) as objects.
The wrapper class in Java provides the mechanism to convert primitive into object and
object into primitive.
The automatic conversion of primitive data type into its corresponding wrapper class is
known as autoboxing.
The automatic conversion of wrapper type into its corresponding primitive data type is known
as unboxing.
*/
-> Object-Oriented Programming is a methodology to design a program using classes and
objects.
-> Any entity that has state and behavior is known as an object.
-> Collection of objects is called class.
There are mainly 4 pillars of Oops:-
Inheritance
Polymorphism
Abstraction
Encapsulation
//Inheritance
Inheritance is the feature by which object of
one class acquire properties of another class.
There are 5 types of inheritance in object orientated programming.
1. simple or single
2. multilevel
3. hierarchical
4. multiple
5. hybrid
in java first 3 types are performed by extends keyword, and other are performed by
implements keyword i.e. using interface.
//Polymorphism
In Upcasting Parent class reference variable is
initialized by child class memory reference.
//Methodoverloading
A method with same name can be used multiple times either by changing number of
arguments
or by changing data type of arguments if number of arguments are same.
//Methodoverriding
If subclass (child class) has the same method as declared in the parent class, it is known as
method overriding in Java.
If a subclass provides the specific implementation of the method that has been declared by
one of its parent class.
//Abstraction
abstraction refers to the act of representing
essential features, without including background
details or explanation.
1. abstract method can not have any body.
2. if any class contains atleast any one abstract method then class must be an abstract
class.
but vice versa is not true.
3. if any class inherit an abstract class then either override all abstract method of parent
class
in child class or make child class as abstract class.
4. abstract class can not be instantiated.
*Interface
There are mainly three reasons to use interface:-
1. It is used to achieve abstraction.
2. By interface, we can support the functionality of multiple inheritance.
3. It can be used to achieve loose coupling.
interface method are by default abstract and public.
interface variable are by default final and static.
we can not create object of interface.
//Encapsulation
Encapsulation in Java is a process of wrapping code and data together into a single unit.
We can create a fully encapsulated class in Java by making all the data members of the
class private.
and then we can use setter and getter methods to set and get the data in it.
->Static
static is keyword or non-access modifier which is applicable before variable, method and a
block.
by declaring any property as static it will be common property for all the object or as a class
level property.
static variable belongs to the class not an object.
static keyword can be used with class level variable not with local variable.
Ques-> What is the difference between static method and instance method?
*static method
1. A method that is declared as static is known as the static method.
2. We don't need to create the objects to call the static methods.
3. Non-static data members cannot be accessed in the static context directly.
*non-static method
1. A method that is not declared as static is known as the instance method.
2. The object is required to call the instance methods.
3. Static and non-static variables both can be accessed in instance methods.
//Final
final is a modifier which is applicable for variable,methods and classes.
1. if a variable is declared as final then it will become constant and we can't change the
value for that variable.
2. if a method is declared as final then we can't override that method in the child class.
3. if a class is declared as final then we can't extend that class.
//finally
finally is a block always associated with try catch to maintain the cleanup code
//finalize()
finalize() is a method which is invoked by the garbage collector just before to destroying an
object to perform cleanup activities.
//String
in java, string objects are immutable. Immutable simply means unmodifiable or
unchangeable.
1. The String class equals() method compares the original content of the string. It compares
values of string for equality.
2. The == operator compares references not values.
3. The String class compareTo() method compares values lexicographically and returns an
integer value that describes
if first string is less than, equal to or greater than second string.
Suppose s1 and s2 are two String objects. If:
s1 == s2 : The method returns 0.
s1 > s2 : The method returns a positive value.
s1 < s2 : The method returns a negative value.
*String
1. String class is immutable.
2. String is slow and consumes more memory when you concat too many strings because
every time it creates new instance.
*StringBuffer
1. StringBuffer class is mutable.
2. StringBuffer is fast and consumes less memory when you cancat strings.
3. StringBuffer is synchronized i.e. thread safe. It means two threads can't call the methods
of StringBuffer simultaneously.
4. StringBuffer is less efficient than StringBuilder.
*StringBuilder
1. StringBuilder is non-synchronized i.e. not thread safe. It means two threads can call the
methods of StringBuilder simultaneously.
2. StringBuilder is more efficient than StringBuffer.
Ques-> What is the difference between the array and linked list
//Array
1. It is linear data structue.
2. It has static size.
3. It can't be extended or reduced.
4. we can random access the elements.
5. memory wastage
6. memory allocation is at compile time.
7. no extra memory is required.
//Linked List
1. It is linear data structure.
2. It has dynamic size.
3. It can be extended and reduced.
4. we can't random access the element
5. no memory wastage.
6. memory allocation is at run time.
7. Extra memory is required.
//collection
-> A Collection in java represents a single unit of objects, i.e., a group.
-> The Collection framework provides an architecture to store and manipulate the group of
objects.
-> Java Collection framework provides many interfaces (Set, List, Queue) and classes
(ArrayList, vector,linkedlist, HashSet, LinkedHashSet, TreeSet).
** LIST **
//ArrayList
1. ArrayList internally uses a dynamic array to store the elements.
2. Manipulation with ArrayList is slow because it internally uses an array.
3. ArrayList is better for storing and accessing data.
4. ArrayList increments 50% of current array size if the number of elements exceeds from its
capacity.
//LinkedList
1. LinkedList internally uses a doubly linked list to store the elements.
2. Manipulation with LinkedList is faster than ArrayList because it uses a doubly linked list.
3. LinkedList is better for manipulating data.
//Vector
1. Vector uses a dynamic array to store the data elements.
2. It is similar to ArrayList.
3. However, It is synchronized.
4. Vector increments 100% means doubles the array size if the total number of elements
exceeds than its capacity.
** SET **
//HashSet
1. HashSet contains unique elements only.
2. HashSet allows null value.
3. HashSet class is non synchronized.
4. HashSet doesn't maintain the insertion order. Here, elements are inserted on the basis of
their hashcode.
//LinkedHashSet
1. Java LinkedHashSet class contains unique elements only like HashSet.
2. Java LinkedHashSet allows null value.
2. Java LinkedHashSet class is non synchronized.
3. Java LinkedHashSet class maintains insertion order.
//TreeSet
1. Java TreeSet class contains unique elements only like HashSet.
2. Java TreeSet class doesn't allow null element.
3. Java TreeSet class is non synchronized.
4. Java TreeSet class maintains ascending order.
-> A list can contain duplicate elements whereas Set contains unique elements only.
exception are events that occurs during the execution of programs that disrupts the normal
flow instructions.
Exception Handling is a mechanism to handle runtime errors such as
ClassNotFoundException, IOException etc.
To handle exceptions 5 keywords are used in java.
1. try
2. throw
3. catch
4. throws
5. finally
MultiTasking ?
Executing several task simultaneously, is the concept of MultiTasking.
There are 2 typs of MultiTasking
1. Process based MultiTasking
Executing several task simultaneously, where each task is a seperate independent
process(applications.), is
known as Process based MultiTasking. It is based on OS level.
// if any thread dont wants to perform operation for a specific amount of time, then we
should use sleep..
sleep(long ms)
sleep(long ms, int ns)
// if any thread wants to perform after completing of some other threads, then we should
use join...
public final void join() throws java.lang.InterruptedException;
public final synchronized void join(long) throws java.lang.InterruptedException;
public final synchronized void join(long, int) throws java.lang.InterruptedException;
//A thread is a lightweight subprocess, the smallest unit of processing. It is a separate path of
execution.
//Threads are independent. If there occurs exception in one thread, it doesn't affect other
threads. It uses a shared memory area.
Thread_syllabus.
1. creating custom thread.
2. Thread basic methods.(getname, set...)
3. interrupting thread execution(join,sleep...)
4. Thread Life cycle
5. synchronization
6. garabage collection
7. singleton
8. finalize..
// synchronization:
If muliple Threads are trying to operate simultaneously on same java object, then there may
a chance of
data inconsistency problem, to overcome this situation synchronized keyword is used.
It is applicable only for methods and blocks but not for classes and variables.
If any method or block is declared as synchronized then at a time only one Thread is allowed
to execute.