Friday, January 28, 2011

intern() in java

We all know that string is the fundamental part of programming, every bit is as important as numbers. In java there is a string methos intern().

So what exactly is intern()? Well, as we know that in java there tow different ways of compare objects. You can use == or equals() method. The == operator compares the references of both objects, whereas equals() compares the data of both objects. So as part in first lesson we have learned that we should use equals() method, not == to compare string objects.

new String("Hello")==new String("Hello") will in fact return false because they are two different string objects. If we use equals() then it will return true. But equals() method can be fairly slow as it compares character-by-character.

Since the == compares identity, i.e. all it has to do is compare tow pointers to see if they are same or not. So it will be much faster then equals() method.

Wednesday, January 26, 2011

Thread.yeild() Vs Thread.Sleep() in java

I tried to find out this diff, bt no where its clearry given. All the details i found was jst bookies thing.
Finally i found this good answer, hope this will help you as well:-
yeild() : This allows other threads to execute which are in Runnable state and Our thread will also go in Runnable state. So that CPU Scheduler may choose other Thread from runnable pool torun . It may choose our thread too. Its not guranteed...
Sleep(): On call of this Our Thread goes to waiting state for specified time. So it gurantees that till that specified time CPU will not pick our thread. After time expires, it will be to Runnable State. Now it will be part of CPU allocation.

Monday, January 24, 2011

In JVM - Memory Type :)

Whenever we come across type of memory in JVM, it always reflects two.
  • Heap Memory
  • Non Heap Memory
all other memory jargon we hear about JVM, are logical part of these two.

Heap Memory
(also called Shared Memory)
This is the place where multiple Thread will share the instances. So, all the Class Instances (Objects) and Arrays stored in this.

Non-Heap Memory
It comprises of '
Mehtod area' and other memory required for internal processing.

Mehtod area : As it is a part of non-heap area, it stores per-class structures, code for methods and constructors. Per-class structure means runtime constants and static fields.

So basically these above 3 type are the main jargon when it comes for JVM memory. But there are some other technical term also comes, whenever we talks about JVM.

Memory Pool : This is created by JVM Memory Manager at the runtime. It may belong to Heap or non-Heap.

Runtime Constant Pool : Its a per-class or per-interface run time representation of the constant_pool table in a class file. Its got allocated from JVM Method area.

Java Stack: This is created privately for each thread. Each Thread will have Program Counter (PC) and Java Stack. PC stores the intermediate value, dynamic linking, return value of any method and dispatched exception. This is basically used in place of register.

Memory Generation : JVM garbage collector uses these generation wise collection into mainly two category :- Young Genration and Old Generation.
Young Generation
-> Eden Space - Shortlived objects and any newly created objects will be available.

-> Survivor Space -When GC happens, if an object is still alive and it will be moved to survivor space.

Old Generation
-> Tenured Space - GC moves live objects from survivor space to tenured generation.
-> PermGen Space - This is called permanent generation. It contains meta data of the virtual machine, class and method objects.

Key TakeAways:-
  • Local Variables are stored in Frames during runtime.
  • Static Variables are stored in Method Area.
  • Arrays are stored in heap memory.
Reference :-
  • MemoryPoolMXBean provides you api to explore the memory usage, threshold notifications, peak memory usage and memory usage monitoring.
  • Threads and Locks chapter of Java Language Specification talks lot about java memory
  • Chapter 5 of Inside the Java Virtual Machine, The Java Virtual Machine by Bill Venners





Saturday, January 22, 2011

Merge two linked lists: Node* MergeList(Node *list1, Node* list2). Do not create a node in the function while merging the lists.

Struct Node{
int data;
struct node* p;
}

we are assuming that both the given list are sorted.
node mergeSort(node a, node b){
if(a == null) return b;
if(b == null) return a;
if(a.value <= b.value){
a.next = mergeSort(a.next, b);
retrun a;
}else{
b.next = mregeSort(a, b.next);
return b;
}
}

Wednesday, January 19, 2011

Most optimal way to find the sum of 2 numbers represented as linked lists

Solution 1:
  1. reverse list-1
  2. reverse list-2
  3. find the sum and store it in a new list represented by list-3
  4. reverse the list.
The complexity of this should be of the O(n+m). Is there anyway to reduce
it, or do it better????
Solution2 :
You can do better without list reversal. WLOG(Without loss of generality) I'll assume that both lists h of equal length (prepend with 0 if necessary).

Start the addition from left right (from most significant to least significantple digit). You have 3cases, depending of the sum two digits:
  1. = 9 : keep the nine and increase a counter
  2. <>counter x nine, write sum, reset counter
  3. > 9 : increase last digit, write counter x zero, write sum (modulo 10), reset counter

  4. I'll work on the following example:

    2 568 794 +
    1 438 204
    --------- =
    4 006 998


    1. Add 2 + 1 = 3: case 3.
      • list = (3), counter = 0

    2. Add 5 + 4 = 9: case 1
      • list = (3), counter = 1

    3. Add 6 + 3 = 9: case 1
      • list = (3), counter = 2

    4. Add 8 + 8 = 16: case 3
      • list = (4, 0, 0, 6), counter = 0

    5. Add 7 + 2 = 9: case 1
      • list = (4, 0, 0, 6), counter = 1

    6. Add 9 + 0 = 9: case 1
      • list = (4, 0, 0, 6), counter = 2

    7. Add 4 + 4 = 8: case 2
      • list = (4, 0, 0, 6, 9, 9, 8), counter = 0
    Hope this will help to understand :)