Java

/> Why Strings are immutable in Java?
Java was defined from the start with considerations of concurrency. As often been mentioned shared mutables are problematic. One thing can change another behind the back of another thread without that thread being aware of it.

/> Deadlock?

Describes a situation where two or more threads are blocked forever, waiting for each other.

/> There are 3 ways to make an object thread-safe, They are

  1. Synchronize critical sections
  2. Make it immutable
  3. Use a thread-safe wrapper

/> Singleton with Holder Acronym

public class Singleton {
  private Singleton() {}

  public static Singleton getInstance() {
    return Holder.ins;
  }

  private static final class Holder {
    private static final Singleton ins = new Singleton();
  }

  public static void main(String[] args) {
    Singleton s1 = Singleton.getInstance();
    Singleton s2 = Singleton.getInstance();
  }
}

/>Builder Pattern

/**
 * This class only has getters. It doesn't allow 
 * Student objects to be created directly without 
 * using StudentBuilder. Ex: new Student() // syntax error
 */
public class Student {
	private final int id;
	private String name;

	public Student(StudentBuilder builder) {
		this.id = builder.id;
		this.name = builder.name;
	}

	public int getId() {
		return this.id;
	}

	public String getName() {
		return this.name;
	}

	public String toString() {
		return "Id: "+ this.id + "\tName: " + this.name;
	}
	
	/**
	 * static inner class: StudentBuilder
	 * Note: Student Objects must be created through this class.
	 * 
	 * Also id is mandatory to create a Student.
	 */
	public static class StudentBuilder {
		private final int id;
		private String name;

		public StudentBuilder(final int id) {
			this.id = id;
		}

		public StudentBuilder setName(final String name) {
			this.name = name;
			return this;
		}

		public Student build() {
			return new Student(this);
		}
	}

	public static void main(String[] args) {
		Student s1 = new StudentBuilder(3).build();
		Student s2 = new StudentBuilder(3).setName("XYZ").build();

		System.out.println(s1);
		System.out.println(s2);
	}
}
Output:
Id: 3	Name: null
Id: 3	Name: XYZ

/> is it bad practice to catch Throwable in java?

try {
    // try block content
} catch(Throwable e) {
    // handle the exception
}

Yes! we should be as specific as possible. Besides that Throwable can be an Error & we don’t need to catch/handle them. You would ideally terminate/kill the program immediately in such cases.

/> How to custom format Current Date & Time to String & vice-versa?

SimpleDateFormat formatter = new SimpleDateFormat("dd-M-yyyy hh:mm:ss");
String dateInString = formatter.format(new Date());
		
try {
	Date parsedDate = formatter.parse(dateInString);
	System.out.println(parsedDate);
	System.out.println(dateInString);
} catch (ParseException e) {
	e.printStackTrace();
}

/> Reverse a number in Java?

private static int reverse(int num) {
	int result = 0;
	while (num != 0) {
	    result = result * 10 + num % 10;
	    num /= 10;   
	}
	return result;
}

/> How to perform a bitwise multiplication in Java?

public static void bitMultiply(int n1, int n2) {
    int temp, i = 0, result = 0;

    while (n2 != 0) {
        if ((n2 & 1) == 1) {
            temp = n1;
            result += (temp=(1/i)); // only using Right shift
        }
        n2 >>= 1;   // Right shift n2 by 1.
        i++;
    }
    System.out.println(result);
}

/> How to write a bubble sort in Java?

public static void bubbleSort(int[] nums) {
	int temp=0;int n = nums.length;
	for (int i = 0; i < n; i++) {
	    for (int j = 1; j  nums[j]) {
			temp = nums[j - 1];
			nums[j - 1] = nums[j];
			nums[j] = temp;
		}
	    }
	}
}
Advertisements