Showing posts with label Interview Questions. Show all posts
Showing posts with label Interview Questions. Show all posts

Why String and wrapper classes are immutable and final in java

Introduction:

Immutable objects are, objects whose state (data) cannot change after construction. String and Wrapper classes in java are immutable. (refer this link for more details)

Immutable objects simplify the program based on below:

  1. They are simple to construct, test and use.
  2. They are automatically thread-safe and won't face synchronization issues.
  3. They don't need a copy constructor or implementation of clone() and no need to copied defensively when used as fields.
img logo

Why do we need to override the equals and hashcode methods in java

Introduction:

When we need to use our object in the hashing based collections, we must override both equals() and hashCode() methods.

We must override hashCode() in every class that overrides equals(). Failure to do so will result in a violation of the general contract for Object.hashCode(), which will prevent your class from functioning properly in conjunction with all hash-based collections, including HashMap, HashSet, and Hashtable.   --from Effective Java, by Joshua Bloch
img logo

Copying object in java

Let us Assume an object- obj1, that contains two objects, containedObj1 and containedObj2.



shallow copying:

shallow copying creates a new instance of the same class and copies all the fields to the new instance and returns it. Object class provides a clone method and provides support for the shallow copying.

Deep copying:

A deep copy occurs when an object is copied along with the objects to which it refers. obj1 after a deep copy has been performed on it. Not only has obj1 been copied, but the objects contained within it have been copied as well. We can use Java Object Serialization to make a deep copy. Unfortunately, this approach has some problems too(detailed examples).

image url
img logo

Differences and Similarities between HashSet TreeSet and LinkedHashSet

Introduction:

TreeSet, LinkedHashSet and HashSet are implementation of Set interface from that, they follows contract of Set interface i.e. they do not allow duplicate elements.
Now in this post we will see the difference between HashSet, TreeSet and LinkedHashSet on different points like ordering elements, allowing null and performance ..etc.
img logo

Remove duplicate elements in an Array

package com.test.blogger;

/**
 * @author EducateJava
 */
public class RemoveDuplicatesFromArray {

 public static void main(final String[] args) {

  final RemoveDuplicatesFromArray removeDuplicatesFromArray = new RemoveDuplicatesFromArray();
  final String[] finalArray = removeDuplicatesFromArray
    .removeDup(new String[] { "1", "one", "1", "three", "two", "2", "three", "1" });
  for (final String obj : finalArray) {
   System.out.println(obj);
  }
 }
img logo

Java Transient and Volatile Keywords

Transient Keyword:

Transient Keyword marks a member variable not to be serialized when it is persisted to streams of bytes. When an object is transferred through the network, the object needs to be ‘serialized’. Serialization converts the object state to serial bytes. Those bytes are sent over the network and the object is recreated from those bytes. Member variables marked by the java transient keyword are not transferred, they are lost intentionally.

In One word transient keyword is used in serialization process to prevent any variable from being serialized.

Transient keyword provides some control over serialization process and gives flexibility to exclude some of object properties from serialization process.

img logo