Thursday, 26 April 2018

Advantage and Disadvantage of Interface

Interface:
 only know the customer requirement and don't know the implementation than go for interface.


Advantage:


  • 100% abstract
  • Multiple inheritance
  • All variables are public static and final.
  • all methods are abstract 
Disadvantage:

  • can't have contractor
  • can't serialized the object because you can't create the object of Interface.
  • can't declare method as static , final, synchronized
  • can't declare variable with private,protected,transient,volatile
  • can't declare instance block or static block for interface.

Friday, 20 April 2018

Primitive Data Types


A primitive type is predefined by the language and is named by a reserved keyword. Primitive values do not share state with other primitive values. The eight primitive data types supported by the Java programming language are:

Type
Bit size
Range
Default value
byte
8-bit
f -128 and a maximum value of 127 (inclusive)
0
short
16-bit
 -32,768 and a maximum value of 32,767
0
int
32-bit
-231 and a maximum value of 231-1.
0
long
64-bit
-263 and a maximum value of 263-1
0
float
32-bit IEEE 754 floating poin

0.0f
double
64-bit IEEE 754 floating poin

0.0d
boolean

True,false
false
char
6-bit Unicode character
 minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive
'\u0000'

Locks in java






If you want to share or update the same data then the activity must be synchronized to avoid the error.in java using synchronize, wait, and notify keyword we can achieve in java.

If you want to perform and share same data then the activity must have control over it or you can say have the lock.








There are 4 type of lock:

1. Fat lock: where multiple thread have waited simultaneously for the lock.

2. Thin lock:  No thread waited for this lock,

3. Recursive lock: Same thread have taken the lock server times.

3. Lazy lock: Lock accrued by thread and not sure will it release or not

Thursday, 19 April 2018

Heap memory area in java


Java objects reside in an area called the heap. The heap is created when the JVM starts up and may increase or decrease in size while the application runs. When the heap becomes full, garbage is collected. During the garbage collection objects that are no longer used are cleared, thus making space for new objects.

The heap is sometimes divided into two areas (or generations) called the nursery (or young space) and the old space. The nursery is a part of the heap reserved for allocation of new objects. When the nursery becomes full, garbage is collected by running a special young collection, where all objects that have lived long enough in the nursery are promoted(moved) to the old space, thus freeing up the nursery for more object allocation. When the old space becomes full garbage is collected there, a process called an old collection.

The reasoning behind a nursery is that most objects are temporary and short lived. A young collection is designed to be swift at finding newly allocated objects that are still alive and moving them away from the nursery. Typically, a young collection frees a given amount of memory much faster than an old collection or a garbage collection of a single-generational heap (a heap without a nursery).

The heap is the run-time data area from which memory for all class instances and arrays is allocated.
The heap may be of a fixed size or may be expanded as required by the computation and may be contracted if a larger heap becomes unnecessary.


if a computation requires more heap than can be made available by the automatic storage management system, the Java Virtual Machine throws anOutOfMemoryError.


  • heavy Java process, insufficient Heap size will cause the popular java.lang.OutOfMemoryError: 




Change the size of Heap.

-Xms<size> - Set initial Java heap size
-Xmx<size> - Set maximum Java heap size

Thursday, 12 April 2018

Why Map is not child interface of Collection ?

Now a days in interview interviewer always ask questions related to Collection and map
And they also ask why map interface is not child of collection interface.

Here is very simple resion

Collection talks about individual object. in collection we can store only individual objects.
Where Map talks about storing  the object as key and value pairs. here key and value are two objects.

That's why Map doesn't extends the Collection interface.

Wednesday, 7 March 2018

How to find methods present in class in java




How to find Min and Max value from stack

Its very simple.

1. Sort the value in stack using Collections.sort()
2.then find the min by using Collections.min()
3.find the max bu using Collections.max() method


package ListExample;

import java.util.Collections;
import java.util.Stack;

public class StackExample {

public static void main(String[] args) {
Stack stack = new Stack();

stack.push(12);
stack.push(12);
stack.push(9);
stack.push(25);
stack.push(55);
stack.push(65);
System.out.println(stack);//printing without sorting
Collections.sort(stack);//Sorting the stack

System.out.println("Max number is "+Collections.max(stack));//Printing Max number in stack

System.out.println("Min number is "+Collections.min(stack));//Return Minimum number in stack


}

}


session-per-request in hibernate

The most common pattern in a multi-user client/server application is session-per-request . In this model, a request from the client is sent...