Java Arrays and Strings
Java Arrays and Strings
Answer:
Answer: Using Arrays.copyOf(Object src, int srcPos, Object dest, int destPos, int length)
13. How do you sort an element in java?
Answer: By Using ‘Arrays.sort()’ method.
14. What is an ArrayIndexOutOfBound Exception?
Answer: The virtual machine throws an ArrayIndexOutOfBound Exception if the length of an array in
negative, equal to array size or greater than array size while traversing an array.
15. How do you handle the ArrayIndexOutOfBoundsException in java?
Answer: By using a ‘try-catch’ block
Example:
try {
int element = arr[index];
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Index out of bounds: " + e.getMessage());
}
16. What is jagged array in java?
Answer: If we are creating an odd number of columns in a 2D Array, is called as jagged array.
Strings
1. What is String in java?
Answer: In generally String is a sequence of characters. In java, String is an object that represents
the sequence of characters. The java.lang.String class is used to create a string object.
2. How to create a string object?
Answer: There are two ways to create a string object:
- String literals.
- By new keyword.
3. Why java uses the concept of String literals?
Answer: To make java more memory efficient.
4. What is immutable String in java?
Answer: Immutable means unmodifiable or unchangeable. Immutable string means once String
is created, its data or state cannot be changed but a new string object is created.
5. Why string objects are immutable?
Answer: Because of Security, Synchronization, Concurrency, Cache, Class loader.
6. What is String constant pool?
Answer: String constant pool is a special memory area which is used to store string literals.
7. Why String class is final in java?
Answer: because no one can override the methods of the String class.
8. How do you compare java Strings?
Answer: There are three ways to compare Strings in java.
- By using equals() method: compares the original content of the string.
- By == operator: compares the reference of the string not values.
- By using compareTo() method: compares the string lexicographically and returns the integer
value. If s1 == s2 - returns 0, if s1>s2 - returns positive value, if s1<s2 - returns negative
value.
9. What is substring in java?
Answer: substring is a subset of another String.
10. What is StringBuffer class?
Answer: StringBuffer class is a thread-safe (Synchronized) which is used to create mutable strings.
11. What is String Builder class?
Answer: StringBuilder class is not thread-safe(non-Synchronized) which is used to create mutable
Strings in java.
12. What are the differences between String & StringBuffer?
Answer:
- Strings are immutable whereas stringBuffer or StringBuilder is mutable.
- String is slow and consumes more memory when we concatenate too many strings.
Whereas StringBuffer is fast and consumes less memory.
- String uses String constant pool whereas StringBuilder uses heap memory
13. What are the differences between StringBuffer & StringBuilder?
Answer:
StringBuffer:
- StringBuffer is Synchronized i.e., thread safe.
- StringBuffer is less efficient than stingBuilder.
StringBuilder:
- StringBuffer is non-Synchronized i.e., not thread safe.
- StringBuilder is more efficient than stringBuffer.