What is the main difference between pass-by-reference and pass-by-value?

Other languages use pass-by-reference or pass-by-pointer. But in Java no matter what type of argument you pass the corresponding parameter (primitive variable or object reference) will get a copy of that data, which is exactly how pass-by-value (i.e. copy-by-value) works. In Java, if a calling method passes a reference of… Continue reading

How will you write an immutable class?

Writing an immutable class is generally easy but there can be some tricky situations. Follow the following guidelines: A class is declared final (i.e. final classes cannot be extended). public final class MyImmutable { … } All its fields are final (final fields cannot be mutated once assigned). private final… Continue reading

What are the benefits of immutable objects?

Immutable classes can greatly simplify programming by freely allowing you to cache and share the references to the immutable objects without having to defensively copy them or without having to worry about their values becoming stale or corrupted. Immutable classes are inherently thread-safe and you do not have to synchronize… Continue reading

What is an immutable object?

Immutable objects whose state (i.e. the object’s data) does not change once it is instantiated (i.e. it becomes a read-only object after instantiation). Immutable classes are ideal for representing numbers (e.g. java.lang.Integer, java.lang.Float, java.lang.BigDecimal etc are immutable objects), enumerated types, colors (e.g. java.awt.Color is an immutable object), short lived objects… Continue reading

What is the main difference between a String and a StringBuffer class?

String StringBuffer / StringBuilder String is immutable: you can’t modify a string object but can replace it by creating a new instance. Creating a new instance is rather expensive. //Inefficient version using immutable String String output = “Some text” Int count = 100; for(int i =0; i<count; i++) { output… Continue reading

What is the difference between “==” and equals(…) method? What is the difference between shallow comparison and deep comparison of objects?

When you are using a collection of objects e.g. using a java.util.Set of persist-able Hibernate objects etc. It is easy to implement these methods incorrectly and consequently your program can behave strangely and also is hard to debug. So, you can expect these questions in your interviews. == [ shallow… Continue reading

What are some of the best practices relating to Java collection?

Use ArrayList, HashMap etc as opposed to Vector, Hashtable etc, where possible to avoid any synchronization overhead. Even better is to use just arrays where possible. If multiple threads concurrently access a collection and at least one of the threads either adds or deletes an entry into the collection, then… Continue reading

What are static factory methods?

Some of the above mentioned features like searching, sorting, shuffling, immutability etc are achieved with java.util.Collections class and java.util.Arrays utility classes. The great majority of these implementations are provided via static factory methods in a single, non-instantiable (i.e. private constrctor) class. Speaking of static factory methods, they are an alternative… Continue reading