Sunteți pe pagina 1din 4

Java Tricky Concepts

Encapsulation: Encapsulation provides objects with the ability to hide their internal characteristics and
behavior using access modifiers: public, private & protected.
Inheritance provides an object with the ability to acquire the fields and methods of another class, called
base class. Inheritance provides re-usability of code and can be used to add additional features to an
existing class, without modifying it.
Polymorphism is the ability of programming languages to present the same interface for differing
underlying data types.
Abstraction: is the process of separating ideas from specific instances, using abstract classes and
interface
Abstraction VS Encapsulation: Encapsulation is usually achieved by hiding information about the
internal state of an object and thus, this strategy provides abstraction automatically.
A user cannot override static methods in Java, because method overriding is based upon dynamic
binding at runtime and static methods are statically binded at compile time.
Auto-boxing is the automatic conversion made by the Java compiler between the primitive types and
their corresponding object wrapper classes.
Overloading => same class, same method name, but different signature
Overriding => same signature; child class redefines the same method i.e. in a parent class.
No multiple inheritances in java. Each class is able to extend only on one class, but is able to
implement more than one interface.
Interface VS Abstract Class:

All methods in an interface are implicitly abstract. On the other hand, an abstract class may contain
both abstract and non-abstract methods.

A class may implement a number of Interfaces, but can extend only one abstract class.

In order for a class to implement an interface, it must implement all its declared methods. However,
a class may not implement all declared methods of an abstract class. Though, in this case, the
sub-class must also be declared as abstract.

Variables declared in a Java interface is by default final. An abstract class may contain non-final
variables.

Process VS Thread: A process is a program in execution. There can be any threads in a process. And
a thread is sometimes called a light weight process
A thread can acquire the lock for an object by using the synchronized keyword. The synchronized
keyword can be applied in a method level (coarse grained lock) or block level of code (fine grained
lock).
A monitor is basically a guardian that watches over a sequence of synchronized code and ensuring
that only one thread at a time executes a synchronized piece of code. Each monitor is associated
with an object reference.
Deadlock - A condition that occurs when two processes are waiting for each other to complete, before
proceeding.

The most basic interfaces that reside in the Java Collections Framework are:
1. Collection - which represents a group of objects known as its elements
a. Set - a collection that cannot contain duplicate elements. (iterable)
b. List - an ordered collection and can contain duplicate elements. (iterable)
c. Queue - a collection used to hold multiple elements prior to processing.
d. Deque [Double Ended Queue]- A linear collection that supports element insertion and
removal at both ends.
2. Map - an object that maps keys to values and cannot contain duplicate keys.

Every collection that contains Tree in its name is sorted. ( Using binary search tree)
Fail Safe iterator VS Fail fast iterator
"Fail safe: it won't fail.
Fail-safe iterator doesn't throw any Exception if Collection is modified structurally while
one thread is Iterating over it because they work on clone of Collection instead of original
collection and thats why they are called as fail-safe iterator.
"Fail fast": it may fail ... and the failure condition is checked aggressively so that the failure
condition is detected before damage can be done. Fail-fast Iterators fail as soon as they
realized that structure of Collection has been changed since iteration has begun.
== vs .equals:
== is a reference comparison, i.e. both objects point to the same memory location

.equals() evaluates to the comparison of values in the objects (if its default implementation is
overridden like in String)
The Contract between .equals() and .hashCode():
Contract: objects which are .equals() MUST have the same .hashCode().
The problem you will have is with collections where unicity of elements is calculated according to
.equals(), for instance keys in a HashMap. As its name implies, it relies on hash tables, and hash
buckets are a function of the object's .hashCode().
If you have two objects which are .equals() but have different hash codes, you lose!
Comparable interface VS Comparator interface:
Comparable interface:
compareTo(): This method compares two objects, in order to impose an order between
them. Specifically, it returns a negative integer, zero, or a positive integer to indicate that
the input object is less than, equal or greater than the existing object.
Comparator inteface:
Compare(): this method is same as compareTo method
Equals(): this method returns true or false, not a ve 0 or +ve value
System.gc() and Runtime.gc(): Hints JVM to run Garbage Collector
Finalize() is called by GC just before releasing a Objects memory.
Garbage collection and memory heap:
For the HotSpot Java VM, the memory pools for serial garbage collection are the following.
1. Eden Space (heap): The pool from which memory is initially allocated for most objects.
2. Survivor Space (heap): The pool containing objects that have survived the garbage
collection of the Eden space.
3. Tenured Generation (heap): The pool containing objects that have existed for some time in
the survivor space.
4. Permanent Generation (non-heap): The pool containing all the reflective data of the virtual
machine itself, such as class and method objects. With Java VMs that use class data
sharing, this generation is divided into read-only and read-write areas.
5. Code Cache (non-heap): The HotSpot Java VM also includes a code cache, containing
memory that is used for compilation and storage of native code.
This means that if you have an object foo (which is an instance of some class), the more garbage
collection events it survives (if there are still references to it), the further it gets promoted. It starts in the
young generation (which itself is divided into multiple spaces - Eden and Survivor) and would eventually
end up in the tenured generation if it survived long enough.
Java command line parameter to change the heap size: Xms denotes starting size of Heap while Xmx denotes maximum size of Heap

Checked exception as they are checked at compile time and only if the exception is handled or
throwed the compiler will compile the code.
Unchecked Exception they are not checked at compile time and may cause runtime exception!
Execution Flow of Try catch and finally block

S-ar putea să vă placă și