Pages

Friday, 9 December 2016

String Buffer vs String Builder

In Java, these are the difference between String, String Builder and String Buffer:

StringString BufferString Builder
Immutable (cannot be changed after creation)Mutable (can be changed after creation)Mutable (can be changed after creation)
Thread Safe (Can be used across threads)Thread Safe (Can be used across threads)Not thread safe
Performance is goodPerformance is slow due to synchronized overheadPerformance is good
'+' operation can be used in String to add two strings'+' operation is not allowed'+' operation is not allowed
Stored in constant String poolStored in Heap area of memoryStored in Heap area of memory

Monday, 6 June 2016

Java Interview Questions on Atomic Variables and Volatile keyword

Here are the list of 5 good questions on Volatile and Synchronized keywords in Java:

1. What is Atomic variables and its significance:

Atomic variables performs atomic operation on its object. In other words, it operation will happen completely or will not happen at all, and no one can interrupt its operation in between. With its introduction in Java 1.5, it is being widely used in multi-thread environment without any synchronization.

2. Is Atomic variable more faster or variable through synchronized code?

For read access, it does not matter whether it is atomic or non-atomic, synchronized or non-synchronized. For write access, atomic variable does not require lock to write because all update to variable happens atomic (either happened or not happened completely) For eg: suppose you want to do i++ in a multi-threaded application and multiple threads can call this, you need to synchronized i++ call (as it is set to 3 registry levels call and you know it can be context switch at any point of registry level calls even in between) to avoid dirty reads and inconsistent write. Atomic variable has just 1 registry level calls.

3. What is the disadvantage of Atomic variables as compared to its primitive type?

All Objects in java requires more care by the programmer to avoid memory consistency errors, doesnt matter whether it is Atomic or not. While the siblings of Atomic variable can be primitive and not object, it doesn't falls into object category and hence not required to take care of memory consistency error The reason why every object requires to avoid memory consistency errors in multi-threaded application is because each thread stack caches the copy of object locally on thread stack (Runtime optimization) might result into not in sync with actual copy of heap if it gets modified by another thread (even in the same code but different thread stack). One solution to avoid is to use volatile for that object which can be changed by another thread frequently. Also the local copy tries to in sync with heap copy very fast but problem occurs if your thread access it more faster than sync happens.

4. How Atomic variables are implemented internally or how atomic variables achieves atomicity?

With the introduction of additional register being added to cpu, atomic variables harness it to achieve atomicity. Since its just 1 cpu register call, it cannot be interrupted in between.

5. What are the atomic variable classes in java?

There are only two classes: AtomicInteger and AtomicLong. There is no atomic level classes for other primitives like double, float etc.