Top 10 Java Questions on Strings and Arrays

์ž‘์„ฑ์ž

์นดํ…Œ๊ณ ๋ฆฌ:

โ† ํ”ผ๋“œ๋กœ
DEV Community ยท Lฦฐฦกng Quแป‘c Tรขy ยท 2026-07-14 ๊ฐœ๋ฐœ(SW)

Lฦฐฦกng Quแป‘c Tรขy

  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.

์›๋ฌธ์—์„œ ๊ณ„์† โ†—

์ถ”์ถœ ๋ณธ๋ฌธ ยท ์ถœ์ฒ˜: dev.to ยท https://dev.to/tayjava/top-10-java-questions-on-strings-and-arrays-5d9m

์ฝ”๋ฉ˜ํŠธ

๋‹ต๊ธ€ ๋‚จ๊ธฐ๊ธฐ

์ด๋ฉ”์ผ ์ฃผ์†Œ๋Š” ๊ณต๊ฐœ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ํ•„์ˆ˜ ํ•„๋“œ๋Š” *๋กœ ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค