0% found this document useful (0 votes)
2 views

Java Notes

The document outlines the key features of the Java programming language, including its simplicity, object-oriented nature, and platform independence. It explains the roles of JVM, JRE, and JDK, as well as concepts related to object-oriented programming such as inheritance, polymorphism, abstraction, encapsulation, and the differences between abstract classes and interfaces. Additionally, it covers Java collections, data structures, exception handling, multitasking, and synchronization in threads.

Uploaded by

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

Java Notes

The document outlines the key features of the Java programming language, including its simplicity, object-oriented nature, and platform independence. It explains the roles of JVM, JRE, and JDK, as well as concepts related to object-oriented programming such as inheritance, polymorphism, abstraction, encapsulation, and the differences between abstract classes and interfaces. Additionally, it covers Java collections, data structures, exception handling, multitasking, and synchronization in threads.

Uploaded by

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

A list of the most important features of the Java language is-

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.

polymorphism is ability to take more than one form.


There are two types of polymorphism in object orientated programming.
1. compile time polymorphism
To achieve this type of polymorphism, method overloading is used.
it is also called static polymorphism.
it is also know as early binding.
For eg.
Shape ob=new Shape();
ob.area(4);
ob.area(4,5);
ob.area(4,3.14);
2. Run time polymorphism
to achieve this type of polymorphism, inheritance,
method overriding and upcasting is used.
it is also known as late binding or dynamic methd dispatching.
For eg.
Parent ob=new Child();
ob.car();
ob=new Parent();
ob.car();

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

In Method overloading these condition should be satisfy:-


1. same name or same method name;
2. within the same class;
3. must have different arguments
3.1 no. of arguments;
3.2 types of arguments;
3.3 sequence of arguments;
In method overriding these condition should be satisfy:-
1. same name or same method name;
2. within different class;
3. must have same arguments
3.1 no. of arguments;
3.2 types of arguments;
3.3 sequence of arguments;
4. the concepts of inheritance must be applied.

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

In Java, we use abstract class and interface to achieve abstraction.


A class that implements an interface must implement all the methods declared in the
interface.
-> There are two ways to achieve abstraction in java
1. Through Abstract class (0 to 100%)
2. Through Interface (100%)

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

Ques-> what is the Difference between abstract class and interface?


*abstract class
1. Abstract class can have abstract and non-abstract methods.
2. Abstract class doesn't support multiple inheritance.
3. Abstract class can have final, non-final, static and non-static variables.
4. The abstract keyword is used to declare abstract class.
*interface
1. interface can have only abstract methods.
2. Interface supports multiple inheritance.
3. Interface has only static and final variables.
4. The interface keyword is used to declare 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.

Ques-> What is the difference between abstraction and encapsulation?


Abstraction hides the implementation details whereas encapsulation wraps code and
data into a single unit.

// There are two types of modifier in java:-


1. access modifier
2. non-access modifier
There are four types of access modifier:-
a.private
b.protected
c.public
d.default
->Private
The access level of a private modifier is only within the same class.
It cannot be accessed from outside the class.
->Protected
The access level of a protected modifier is within the same package and outside the
package through
child class. if we do not make the child class, it cannot be accessed from outside the
package.
->Public
The access level of public modifier is within the class,within the package and outside the
package.

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

* Two main restrictions are applied to the static methods.


1. The static method can not use non-static data member or call the non-static method
directly.
2. this and super cannot be used in static context as they are non-static.
Ques-> why is the main method static?
main method is static because so that JVM could call the main method directly.
If we make the main method non-static, JVM will have to create its object first
and then call main() method which will lead to the extra memory allocation.

Ques-> can we override the static methods?


No, we can't override static methods.

Ques-> What is static block?


static block is used to initialize the static data member. It is executed before the main
method, at the time of classloading.

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.

2. Thread based MultiTasking


Executing several task simultaneously, where each task is a seperate independent
path of the same process, is
known as Thread based MultiTasking. It is based on Programetic Level.

To create User defined Thread,


a. Extends Thread class
b. Implements Runnable interface.

// if any thread dont wants to perform operation for a specific amount of time, then we
should use sleep..

similar like delay in c


public static native void sleep(long) throws java.lang.InterruptedException;
public static void sleep(long, int) throws java.lang.InterruptedException;

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.

synchronized keyword can be used either before method name, or as a block.


Lock - >
1. object level lock
a. using method //synchronized before method
b. using block //synchronized(this)

2. class level lock.


a. using method // synchronized static
b. using block // synchronized(classname.class)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy