Member-only story
π― Java String Object Creation: Interview Questions & Best Practices
2 min read 1 day ago
π Introduction
Strings in Java are one of the most commonly used objects, and understanding how they are created is crucial for performance and memory management. This guide covers different ways to create String
objects, best practices, and frequently asked interview questions.
Story List Categories:
- About Me & List of Stories
- Java β All things Java-related.
- Java Interview Playbook: Your Go-To Reading List β For interview preparation.
- JAVA-8 β Dedicated to Java 8 topics.
- Spring Boot & Spring β Focused on Spring and Spring Boot.
- Microservices Topics List β Covering various microservices to
π Ways to Create String Objects in Java
1οΈβ£ Using String Literals
String s1 = "Hello";
String s2 = "Hello";
System.out.println(s1 == s2); // true
β Stored in the String Pool β If the string already exists in the pool, no new object is created.
2οΈβ£ Using new
Keyword
String s1 = new String("Hello");
String s2 = new String("Hello")β¦