DEV Community

Top 10 Java Questions on Strings and Arrays

  1. What is the difference between String, StringBuilder, and StringBuffer?
    📌 Answer:
    • String: Immutable. Every modification creates a new object in memory.
    • StringBuilder: Mutable, fast, and not thread-safe.
    • StringBuffer: Mutable and thread-safe (synchronized), but slower than StringBuilder due to synchronization overhead.

  2. Why is String in Java immutable?
    📌 Answer:
    • Security: Strings are frequently used as parameters for network connections, URLs, and class loaders. Immutability prevents malicious alteration.
    • Efficient Hashing: The hash code is cached upon creation, making Strings highly efficient and safe to use as keys in HashMap or elements in HashSet.
    • Thread-Safety: Multiple threads can safely share the same String instance without the need for synchronization.
    • String Pool Support: Enables memory savings by allowing the JVM to reuse existing instances from the String constant pool.

  3. What is the String pool in Java?
    📌 Answer:
    • It is a special memory region within the heap used to store String literals.
    • When creating a string like "hello", the JVM first checks the pool. If the string already exists, it reuses the existing reference instead of creating a new object.
    • The intern() method can be used to manually add a String object to the pool.

  4. What is the difference between == and .equals() when comparing Strings?
    📌 Answer:
    • ==: Compares object references (memory addresses).
    • .equals(): Compares the actual content (character sequence) of the Strings.
    String a = "abc";
    String b = new String("abc");
    System.out.println(a == b); // false (different memory addresses)
    System.out.println(a.equals(b)); // true (same content)

  5. How do you reverse a String in Java?
    📌 Answer:
    • Using StringBuilder (Recommended):
    String s = "Java";
    String reversed = new StringBuilder(s).reverse().toString();

  6. How do you check if a string is a palindrome?
    📌 Answer:
    • A palindrome is a string that reads the same forward and backward.
    String s = "madam";
    boolean isPalindrome = s.equals(new StringBuilder(s).reverse().toString());

  7. How do you convert a String to a character array (char[])?
    📌 Answer:
    • By using the toCharArray() method:
    String s = "hello";
    char[] arr = s.toCharArray();

  8. How do you find the largest element in an integer array?
    📌 Answer: Iterate through the array while keeping track of the maximum value:
    int[] arr = {5, 9, 2, 7};
    int max = arr[0];
    for (int i = 1; i < arr.length; i++) {
    if (arr[i] > max) {
    max = arr[i];
    }
    }

  9. How do you sort an array in Java?
    📌 Answer:
    • For primitive or object arrays, use the built-in method: Arrays.sort(arr);
    • For object arrays, you can use a custom comparator (e.g., to sort in descending order):
    // Note: 'arr' must be an array of objects (e.g., Integer[]), not primitives (int[])
    Arrays.sort(arr, (a, b) -> b - a); // sorts in descending order

  10. What is the difference between an Array and an ArrayList in Java?
    📌 Answer:
    • Array:
    â—¦ Fixed size (cannot be resized after creation).
    â—¦ Can contain both primitive types (e.g., int, char) and objects.
    • ArrayList:
    â—¦ Dynamic size (automatically resizes as elements are added or removed).
    â—¦ Can only contain objects (uses wrapper classes like Integer or Character for primitives).
    â—¦ Provides a rich set of utility methods: add(), remove(), contains(), etc.

Top comments (0)