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.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.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.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)How do you reverse a String in Java?
๐ Answer:
โข Using StringBuilder (Recommended):
String s = "Java";
String reversed = new StringBuilder(s).reverse().toString();
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());How do you convert a String to a character array (char[])?
๐ Answer:
โข By using the toCharArray() method:
String s = "hello";
char[] arr = s.toCharArray();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];
}
}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 orderWhat 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 10 Java Questions on Strings and Arrays
์์ฑ์
์นดํ ๊ณ ๋ฆฌ:
โ ํผ๋๋ก
์ถ์ถ ๋ณธ๋ฌธ ยท ์ถ์ฒ: dev.to ยท https://dev.to/tayjava/top-10-java-questions-on-strings-and-arrays-5d9m
๋ต๊ธ ๋จ๊ธฐ๊ธฐ