Java Interview Question Series 1

Pallavi Jadhav
Javarevisited
Published in
3 min readNov 16, 2022

Q1. How to create Immutable class in Java?

Immutable class in java means that once an object is created, we cannot change its content. In Java, all the wrapper classes(like Integer, Boolean, Byte, Short) and String class is immutable. We can create our own immutable class as well.

Requirements of an immutable class :-

  • The class must be declared as final so that child classes can’t be created.
  • Data members in the class must be declared private so that direct access is not allowed.
  • Data members in the class must be declared as final so that we can’t change the value of it after object creation.
  • A parameterized constructor should initialize all the fields performing a deep copy so that data members can’t be modified with an object reference.
  • Deep Copy of objects should be performed in the getter methods to return a copy rather than returning the actual object reference
  • There should be no setters.

Example:-

public final class Employee {
final String name;
public Employee(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
public class ImmutableDemo {
public static void main(String ar[]) {
Employee e = new Employee("ABC123");
String s1 = e.getName();
System.out.println("Name of the Employee: " + s1);
}
}

Q2. How to write a custom Comparator function in java ?

The Comparable interface defines a default ordering for the objects of a class. This default ordering is also called the natural ordering of the objects.

But what if you need to alter the default ordering just for a single requirement? For example, what if you want to sort the Employee object in the previous example based on their names, not IDs?

You can’t change the implementation of the compareTo() function because it will affect the ordering everywhere, not just for your particular requirement.

For such cases, Java provides a Comparator interface. You can define a Comparator and pass it to the sorting functions like Collections.sort or Arrays.sort to sort the objects based on the ordering defined by the Comparator.

The Comparator interface contains a method called compare() that you need to implement in order to define the ordering of the objects of a class -

public interface Comparator<T> {
int compare(T o1, T o2);
}

The implementation of the compare() method should return

  • a negative integer, if the first argument is less than the second,
  • zero, if the first argument is equal to the second, and
  • a positive integer, if the first argument is greater than the second.
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

// A class to represent a student.
class Student {
int rollno;
String name, address;

// Constructor
public Student(int rollno, String name, String address) {
this.rollno = rollno;
this.name = name;
this.address = address;
}

// Used to print student details in main()
public String toString() {
return this.rollno + " " + this.name + " " + this.address;
}
}

class Sortbyroll implements Comparator<Student> {
// Used for sorting in ascending order of
// roll number
public int compare(Student a, Student b) {
return a.rollno - b.rollno;
}
}

class Sortbyname implements Comparator<Student> {
// Used for sorting in ascending order of
// roll name
public int compare(Student a, Student b) {
return a.name.compareTo(b.name);
}
}

// Driver class
public class Main {
public static void main(String[] args) {
ArrayList<Student> ar = new ArrayList<Student>();
ar.add(new Student(111, "bbbb", "london"));
ar.add(new Student(131, "aaaa", "nyc"));
ar.add(new Student(121, "cccc", "jaipur"));

System.out.println("Unsorted");
for (int i = 0; i < ar.size(); i++)
System.out.println(ar.get(i));

Collections.sort(ar, new Sortbyroll());

System.out.println("\nSorted by rollno");
for (int i = 0; i < ar.size(); i++)
System.out.println(ar.get(i));

Collections.sort(ar, new Sortbyname());

System.out.println("\nSorted by name");
for (int i = 0; i < ar.size(); i++)
System.out.println(ar.get(i));
}
}
Unsorted
111 aaaa london
131 aaaa nyc
121 cccc jaipur

Sorted by namd and rolNum
111 aaaa london
131 aaaa nyc
121 cccc jaipur
java interview questions

Thank you for taking the time to check out my content. If you found this helpful, I would be so grateful if you could follow me and support my work.

--

--