- Difference between a = a + b and a += b ?
The += operator implicitly cast the result of addition into the type of variable used to hold the result. When you add two integral variable e.g. variable of type byte, short, or int then they are first promoted to int and them addition happens. If result of addition is more than maximum value of a then a + b will give compile time error but a += b will be ok as shown below
byte a = 127; byte b = 127; b = a + b;
// error : cannot convert from int to byte b += a; // ok
- Can I store a double value in a long variable without casting?
No, you cannot store a double value into a long variable without casting because the range of double is more that long and you we need to type cast. It’s not dificult to answer this question but many develoepr get it wrong due to confusion on which one is bigger between double and long in Java.
- Which one will take more memory, an int or Integer?
An Integer object will take more memory an Integer is the an object and it store meta data overhead about the object and int is primitive type so its takes less space.
- Why is String Immutable in Java?
The String is heavily used and making it immutable allow some optimization easy sharing same String object between multiple clients.
- Can we use String in the switch case?
Yes from Java 7 onward we can use String in switch case but it is just syntactic sugar. Internally string hash code is used for the switch.
- What is constructor chaining in Java?
When you call one constructor from other than it’s known as constructor chaining in Java. This happens when you have multiple, overloaded constructor in the class.
- Can we make array volatile in Java?
Yes, you can make an array volatile in Java but only the reference which is pointing to an array, not the whole array. What I mean, if one thread changes the reference variable to points to another array, that will provide a volatile guarantee, but if multiple threads are changing individual array elements they won’t be having happens before guarantee provided by the volatile modifier. (tricky)
- Can volatile make a non-atomic operation to atomic?
Volatile is not about atomicity, but there are cases where you can use a volatile variable to make the operation atomic.
If you know that a long field is accessed by more than one thread e.g. a counter, a price field or anything, you better make it volatile. Why? because reading to a long variable is not atomic in Java and done in two steps, If one thread is writing or updating long value, it’s possible for another thread to see half value (fist 32-bit). While reading/writing a volatile long or double (64 bit) is atomic.
- What are practical uses of volatile modifier?
One of the practical use of the volatile variable is to make reading double and long atomic. Both double and long are 64-bit wide and they are read in two parts, first 32-bit first time and next 32-bit second time, which is non-atomic but volatile double and long read is atomic in Java. Another use of the volatile variable is to provide a memory barrier, just like it is used in Disrupter framework. Basically, Java Memory model inserts a write barrier after you write to a volatile variable and a read barrier before you read it. Which means, if you write to volatile field then it’s guaranteed that any thread accessing that variable will see the value you wrote and anything you did before doing that right into the thread is guaranteed to have happened and any updated data values will also be visible to all threads, because the memory barrier flushed all other writes to the cache.
- What guarantee volatile variable provides?
volatile variables provide the guarantee about ordering and visibility e.g. volatile assignment cannot be re-ordered with other statements but in the absence of any synchronization instruction compiler, JVM or JIT are free to reorder statements for better performance. volatile also provides the happens-before guarantee which ensures changes made in one thread is visible to others. In some cases volatile also provide atomicity e.g. reading 64-bit data types like long and double are not atomic but read of volatile double or long is atomic.