Month: October 2015

Java Details

Nice Java Tutorials (Excellent multi-threading tutorial)

1. Advantages/Disadvantages of using Linked List for HashMap implementation in Java

2.    Serialization/Deserialization process; Difference between Serializable and Externalizable Interfaces

3. Copy Constructor, Volatile, Cloneable, Comparable, Comparator

4. Memory Leaks in Java

5. Application Performance Problems debugging

6. Equals and Hashcode

7.  Garbage Collection

8.  Where are Static Variables Stored?

9.  Calling member functions from constructor?

10. Why is String immutable in java

11. Synchronizing on string objects

12.  Java clone

13.  Mutable and Immutable Classes/Objects

14.  String concatenation

15. Aggregation vs Composition

16. Design Patterns in Java

17.  What does the following Java program print?

public class Test {
    public static void main(String[] args) throws Exception {
        char[] chars = new char[] {'\u0097'};
        String str = new String(chars);
        byte[] bytes = str.getBytes();

Answer: The trikyness of this question lies on character encoding and how String to byte array conversion works. In this program, we are first creating a String from a character array, which just has one character ‘\u0097’, after than we are getting byte array from that String and printing that byte. Since \u0097 is within the 8-bit range of byte primitive type, it is reasonable to guess that the str.getBytes() call will return a byte array that contains one element with a value of -105 ((byte) 0x97). However, that’s not what the program prints and that’s why this question is tricky. As a matter of fact, the output of the program is operating system and locale dependent. On a Windows XP with the US locale, the above program prints [63], if you run this program on Linux or Solaris, you will get different values.

To answer this question correctly, you need to know about how Unicode characters are represented in Java char values and in Java strings, and what role character encoding plays in String.getBytes(). In simple word, to convert a string to a byte array, Java iterate through all the characters that the string represents and turn each one into a number of bytes and finally put the bytes together. The rule that maps each Unicode character into a byte array is called a character encoding. So It’s possible that if same character encoding is not used during both encoding and decoding then retrieved value may not be correct. When we call str.getBytes() without specifying a character encoding scheme, the JVM uses the default character encoding of platform to do the job. The default encoding scheme is operating system and locale dependent. On Linux, it is UTF-8 and on Windows with a US locale, the default encoding is Cp1252. This explains the output we get from runing this program on Windows machines with a US locale. No matter which character encoding scheme is used, Java will always translate Unicode characters not recognized by the encoding to 63, which represents the character U+003F (the question mark, ?) in all encodings.

Read more:

18. compareTo

19.  ThreadPool

20. Performance implications of final keyword

21.  Why should variables accessed in anonymous inner class be declared final


23.  Java8 features

24.  Java Generics :

25.  WeakHashMap

26.  BlockingQueue (Producer Consumer Problem)


28.  Spring Boot:

29. Spring Boot Actuator

30. How to expose an endpoint with spring boot