Professional Documents
Culture Documents
All Difference Between in Core Java
All Difference Between in Core Java
All Difference Between in Core Java
Defined within a
Defined outside a method at the Defined outside a method at the
method or a code
class level class level
block
Local Variable Instance Variable Static Variable
Is only accessible in
the method/code
Is accessible throughout the class Is accessible throughout the class
block where it is
declared
Remains in memory
Remains in memory as long as Remains in memory as long as
as long as the
the object is in memory program executes
method executes
Return type A constructor can not have any return A method can have a
type. return type.
Key Constructors Methods
Name A constructor must have same name A method name can not be
as that of the class. same as class name.
Abstraction Encapsulation
The objects are encapsulated that The object need not to abstract that result in
helps to perform abstraction. encapsulation.
➢Difference between Compile Time and Run time
Polymorphism
In Compile time Polymorphism, the call In Run time Polymorphism, the call
is resolved by the compiler. is not resolved by the compiler.
Abstract class can have abstract and Interface can have only abstract methods.
non-abstract methods. Since Java 8, it can have default and static
methods also.
An abstract class can extend another An interface can extend another Java
Java class and implement multiple Java interface only.
interfaces.
A Java abstract class can have class Members of a Java interface are public by
members like private, protected, etc. default.
Example: Example:
public abstract class Shape{ public interface Drawable{
public abstract void draw(); void draw();
} }
The current instance of the class is The current instance of the parent class is
represented by this keyword. represented by the super keyword.
In order to call the default constructor of In order to call the default constructor of
the current class, we can use this the parent class, we can use the super
keyword. keyword.
We can use it to access only the current We can use it to access the data
class data members and member members and member functions of the
functions. parent class.
➢Difference between this() and super()
this() super()
It is used for invoking the current class It is used for invoking parent class
method. methods.
It can be used anywhere in the It is always the first line in the child class
parameterized constructor. constructor.
Type It can be classified into two All errors in Java are unchecked.
categories i.e. checked and
unchecked.
Known or Only checked exceptions are Errors will not be known to the
unknown known to the compiler. compiler.
The compiler checks a checked The compiler does not check these types
exception. of exceptions.
They are the sub-class of the exception They are runtime exceptions and hence
class. are not a part of the Exception class.
Here, the JVM needs the exception to Here, the JVM does not require the
catch and handle. exception to catch and handle.
Java throw keyword is used throw an Java throws keyword is used in the
exception explicitly in the code, inside method signature to declare an
the function or the block of code. exception which might be thrown by the
function while the execution of the code.
Type of exception Using throw keyword, Using throws keyword, we can declare
we can only propagate unchecked both checked and unchecked exceptions.
exception i.e., the checked exception However, the throws keyword can be used
cannot be propagated using throw only. to propagate checked exceptions only.
throw is used within the method. throws is used with the method
signature.
It can be single-dimensional or
multidimensional It can only be single-dimensional
For and for each generally is used for Here iterator is used to traverse
iterating over arrays riverArrayList
length keyword can give the total size() method is used to compute the
size of the array. size of ArrayList.
Primitive data types can be stored Primitive data types are not directly
directly unlikely objects added unlikely arrays, they are
Array ArrayList
They can not be added here hence They can be added here hence
type unsafe makingArrayList type-safe.
Arrays are fixed in size i.e once the The collection is dynamic in size i.e
array with the specific size is declared based on requirement size could be get
then we can't alter its size afterward. altered even after its declaration.
Arrays due to fast execution consumes Collections, on the other hand, consume
more memory and has better less memory but also have low
performance. performance as compared to Arrays.
Arrays can hold the only the same type Collection, on the other hand, can hold
of data in its collection i.e only both homogeneous and heterogeneous
homogeneous data types elements are elements.
allowed in case of arrays.
Arrays can hold both object and On the other hand, collection can hold
primitive type data. only object types but not the primitive
type of data.
Arrays due to its storage and internal Collection on the other hand with
implementation better in performance. respect to performance is not
recommended to use.
➢Difference between ArrayList and LinkedList
ArrayList LinkedList
Vector may have a default size. List does not have default size.
Vector List
It uses a Hashtable to store the elements. It uses a HashTable and doubly linked list
to store and maintain the insertion order
of the elements.
It does not provide any insertion order. It provides an insertion order; we can
We can not predict the order of predict the order of elements.
elements.
It allows only one null element. It also allows only one null element.
5) We can sort the list elements of We can sort the list elements of
Comparable type Comparator type
by Collections.sort(List) method. by Collections.sort(List,
Comparator) method.