📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
✅ Some premium posts are free to read — no account needed. Follow me on Medium to stay updated and support my writing.
🎓 Top 10 Udemy Courses (Huge Discount): Explore My Udemy Courses — Learn through real-time, project-based development.
▶️ Subscribe to My YouTube Channel (172K+ subscribers): Java Guides on YouTube
Before we get started, check out Complete guide to Java String, StringBuilder, and StringBuffer methods
YouTube Video
1. What is String in Java?
2. Is String a primitive type or an object in Java?
3. What are the different ways to create String Objects?
- By string literal
- By new keyword
1. Using String Literal
String s="javaguides";
2. Using new Keyword
String str = new String("Java Guides");
4. What do you mean by mutable and immutable objects?
String str = "Hello";
str.concat(" World"); // Returns a new string, the original 'str' is unchanged.
StringBuilder builder = new StringBuilder("Hello");
builder.append(" World"); // Modifies the original object.
5. What is String Constant Pool?
String Constant Pool is a pool of string literals stored in the Java Heap memory. When a string literal is created, Java checks the String Constant Pool first. If the string already exists, it returns a reference to the pooled instance; otherwise, it creates a new String and places it in the pool.6. How String Constant Pool Works
When a string is created in Java, the JVM first checks the string constant pool. If a string with the same value already exists in the pool, a reference to that string is returned. Otherwise, a new string object is created in the pool, and a reference to this new object is returned. This process is called "interning."For example:
String str1 = "JavaGuides"; // These two strings are both references
String str2 = "JavaGuides"; // to the same object in the constant pool
7. Does String is thread-safe in Java?
Yes, Strings are immutable in Java so once we create a String object then we can't change its content. Hence it’s thread-safe and can be safely used in a multi-threaded environment.8. Difference between == and equals() method in Java
In Java, the == operator is used to compare the references of objects, i.e., it checks if two variables point to the same memory location.public class Test {
public static void main(String[] args)
{
String s1 = new String("HELLO");
String s2 = new String("HELLO");
System.out.println(s1 == s2);
System.out.println(s1.equals(s2));
}
}
false
true
9. Difference between String and StringBuilder?
- A String is immutable in Java, while a StringBuilder is mutable in Java
- A String is thread-safe, whereas StringBuilder is not a thread-safe
- The performance of the String is slower than StringBuilder in the case of multiple concatenation operations. This is because the string is immutable in Java, and the concatenation of two string objects involves creating a new object
- A String class overrides the equals() method of the Object class. So you can compare the contents of two strings by the equals() method. StringBuilder class doesn't override the equals() method of an Object class.
10. Difference between String and StringBuffer
- A String class is immutable and whereas the StringBuffer class is mutable.
- A String is slow and consumes more memory when you perform too many concatenation String operations because every time it creates a new instance. But StringBuffer is fast and consumes less memory when you cancat strings.
- A String class overrides the equals() method of an Object class. So you can compare the contents of two strings by the equals() method. StringBuffer class doesn't override the equals() method of an Object class.
- String class uses a String constant pool to store the objects whereas StringBuffer class uses heap memory to store its objects.
11. Difference between StringBuilder and StringBuffer?
- A StringBuilder is not a thread-safe, whereas StringBuffer is a thread-safe
- A StringBuffer is slower than StringBuilder. Because StringBuffer is a thread-safe implementation and therefore slower than the StringBuilder.
12. String vs StringBuilder vs StringBuffer in Java
13. What does the String intern() method do?
The method intern() creates an exact copy of a String object in the heap memory and stores it in the String constant pool.Note that, if another String with the same contents exists in the String constant pool, then a new object won't be created and the new reference will point to the other String.
String s1 = "abc";
String s2 = new String("abc");
String s3 = new String("foo");
String s4 = s1.intern();
String s5 = s2.intern();
System.out.println(s3 == s4);
System.out.println(s1 == s5);
false
true
15. Why String is a popular HashMap key in Java?
Since String is immutable, its hashcode is cached at the time of creation and it doesn’t need to be calculated again. This makes it a great candidate for keys in a Map and its processing is fast than other HashMap key objects. This is why String is mostly used Object as HashMap keys.15. How many objects will be created in the following code and where they will be stored?
String s1 = new String("javaguides");
String s2 = "javaguides";
Answer:
- 1 object is stored in the String Pool (part of the heap).
- 1 object is directly stored in the heap memory (outside the StringPool).
16. How many objects will be created in the following code and where they will be stored?
String s1 = new String("javaguides");
String s2 = new String("javaguides");
Answer:
Three objects will be created. Here's the breakdown:- 1 object is stored in the StringPool (part of the heap).
- 2 objects are directly stored in the heap memory (outside the StringPool).
17. How do you concatenate two strings in Java?
String result = "Hello" + " World";
String result = "Hello".concat(" World");
18. What are the methods to compare two strings in Java?
String str1 = "Hello";
String str2 = "hello";
boolean isEqual = str1.equals(str2); // false
boolean isEqualIgnoreCase = str1.equalsIgnoreCase(str2); // true
int comparisonResult = str1.compareTo(str2); // -32 (because 'H' is 32 units away from 'h')
19. Java String Programs Asked in the Interviews
All these Java String programs have step-by-step explanations along with their output.- Java Program to Find the First Non-repeated Character in a String
- Java Program to Check Palindrome String
- Java Program to Find Duplicate Characters in a String
- Java Program to Find Duplicate Words in a String
- Java Program to Find All the Permutations of a String
- Java Program to Count Occurrences of Words in a String
- Java Program to Count the Occurrences of Each Character
- Java Program to Count Vowels and Consonants in a String
- Java program to Count the Number of Duplicate Words in a String
- Java Program to Count Number of Words in Given String
- Java Program to Count the Number of Occurrences of Substring in a String
- Java Program to Count the Occurrences of Each Character in String
- Java Program to Merge Two String Arrays
- Java Program to Remove Duplicate Words from String
- Java Program to Reverse a String(5 ways)
- Java Program to Reverse Each Word of a String
- Java Program to Swap Two Strings
- How to Check if the String Contains Only Digits
- How to Check if the String Contains Only Letters
- How to Check If the String Contains Only Letters or Digits
Comments
Post a Comment
Leave Comment