With the following line you are not creating a new String object in the heap but reusing a string literal (if already available):

String message = "Hai";

"Hai" is a string literal in the string literal pool. Since, strings are immutable, they are reusable so they are pooled in the string literal pool by the JVM. And this is the recommended way, because you are reusing it.

But, with the following you are actually creating a new object (in the heap):

String message = new String("Hai");

new String("Hai") is a new String object. In this case, even if the literal "Hai" was already in the string literal pool, a new object is created. This is not recommended because chances are that you might end with more than one String objects with the same value.

Also see this post: Questions about Java's String pool

Are there other classes which do not require new to create object ??

Actually, you can not create any object in Java without using the keyword new.

e.g.

Integer i = 1;

Does, not mean that the Integer object is created without using new. It's just not required for us to use the new keyword explicitly. But under the hood, if the Integer object with value 1 does not already exist in cache (Integer objects are cached by JVM), new keyword will be used to create it.

Answer from Bhesh Gurung on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › dsa › can-string-be-considered-as-a-keyword
Can String be considered as a Keyword? - GeeksforGeeks
July 23, 2025 - As there is no keyword "string" in the above-mentioned list, a string is not a keyword. ... Java provides a method iskeyword() which is used to find whether the given string is a keyword or not.
🌐
W3Schools Blog
w3schools.blog › home › is string a keyword in java?
is string a keyword in java?
April 23, 2018 - No, String is not a keyword in java. String is an object which is created by using String class. String objects are immutable and they can’t modified i.e.
🌐
GeeksforGeeks
geeksforgeeks.org › java › strings-in-java
Java Strings - GeeksforGeeks
If we notice if we use new keyword or string literals both store the values in the string but the difference is if we use the string literals or intern() the string object it will store the values in the string constant pool which is present inside the heap as shown in the image. ... import java.io.*; import java.lang.*; class Geeks { public static void main(String[] args) { // Declare String without using new operator String name = "GeeksforGeeks"; // Prints the String.
Published   February 12, 2019
🌐
TutorialsPoint
tutorialspoint.com › how-to-check-if-a-string-is-a-valid-keyword-in-java
How to check if a string is a valid keyword in Java?
import java.util.*; public class Demo{ static boolean valid_identifier(String my_str, int n){ if (!((my_str.charAt(0) >= 'a' && my_str.charAt(0) <= 'z') || (my_str.charAt(0)>= 'A' && my_str.charAt(1) <= 'Z') || my_str.charAt(0) == '_')) return false; for (int i = 1; i < my_str.length(); i++){ if (!((my_str.charAt(i) >= 'a' && my_str.charAt(i) <= 'z') || (my_str.charAt(i) >= 'A' && my_str.charAt(i) <= 'Z') || (my_str.charAt(i) >= '0' && my_str.charAt(i) <= '9') || my_str.charAt(i) == '_')) return false; } return true; } public static void main(String args[]){ String my_str = "Hi_there!3"; int n = my_str.length(); if (valid_identifier(my_str, n)) System.out.println("It is valid"); else System.out.println("It is invalid"); } }
🌐
DataCamp
datacamp.com › doc › java › java-strings
Java Strings
Java keywordsIntroduction To JavaJava File HandlingJava Language BasicsJava ArraysJava Object-Oriented Programming ... In Java, strings are objects that represent sequences of characters. The String class is part of the java.lang package and provides a range of methods to manipulate and work ...
🌐
W3Schools
w3schools.com › java › java_strings.asp
Java Strings
A String in Java is actually an object, which means it contains methods that can perform certain operations on strings.
Top answer
1 of 5
30

With the following line you are not creating a new String object in the heap but reusing a string literal (if already available):

String message = "Hai";

"Hai" is a string literal in the string literal pool. Since, strings are immutable, they are reusable so they are pooled in the string literal pool by the JVM. And this is the recommended way, because you are reusing it.

But, with the following you are actually creating a new object (in the heap):

String message = new String("Hai");

new String("Hai") is a new String object. In this case, even if the literal "Hai" was already in the string literal pool, a new object is created. This is not recommended because chances are that you might end with more than one String objects with the same value.

Also see this post: Questions about Java's String pool

Are there other classes which do not require new to create object ??

Actually, you can not create any object in Java without using the keyword new.

e.g.

Integer i = 1;

Does, not mean that the Integer object is created without using new. It's just not required for us to use the new keyword explicitly. But under the hood, if the Integer object with value 1 does not already exist in cache (Integer objects are cached by JVM), new keyword will be used to create it.

2 of 5
9

The Java language specification allows for representation of a string as a literal. You can consider it a shortcut initialization for a String that has one important side-effect that is different from regular initialization via new

String literals are all interned, which means that they are constant values stored by the Java runtime and can be shared across multiple classes. For example:

class MainClass (
    public String test = "hello";
}

class OtherClass {
   public String another = "hello";

   public OtherClass() {
       MainClass main = new MainClass();
       System.out.println(main.test == another);
   }
}

Would print out "true" since, both String instances actually point to the same object. This would not be the case if you initialize the strings via the new keyword.

🌐
O'Reilly
oreilly.com › library › view › java-for-dummies › 9781118239742 › a68_06_9781118239742-ch03.html
String Class - Java For Dummies Quick Reference [Book]
String Class Package: java.lang In Java, strings are reference types based on the String class, not value types like int or boolean. As a result, a string variable holds a reference... - Selection from Java For Dummies Quick Reference [Book]
Find elsewhere
🌐
Edureka
edureka.co › blog › java-string
Java Strings - How to Declare String in Java With Examples
February 13, 2025 - This is wrong information. Please correct it. ( Corrections are in bold letters) By new keyword : Java String is created by using a keyword “new”. For example: String s=new String(“Welcome”); It creates two objects (
🌐
Oracle
docs.oracle.com › javase › tutorial › java › data › strings.html
Strings (The Java™ Tutorials > Learning the Java Language > Numbers and Strings)
In this case, "Hello world!" is ... object with its value—in this case, Hello world!. As with any other object, you can create String objects by using the new keyword and a constructor....
🌐
Runestone Academy
runestone.academy › ns › books › published › apcsareview › Strings › sbasics.html
4.1. What is a String? — AP CSA Java Review - Obsolete
Class names in Java, like String, begin with a capital letter. All primitive types: int, double, and boolean, begin with a lowercase letter. This is one easy way to tell the difference between primitive types and class types. Run the following code. What does it print? The code above declares an object variable named greeting and sets the value of greeting to the Java keyword null to show that it doesn’t refer to any object yet.
🌐
Programiz
programiz.com › java-programming › string
Java String (With Examples)
It looks like we are able to change the value of the previous string. However, this is not true. ... So far, we have created strings like primitive types in Java. Since strings in Java are objects, we can create strings using the new keyword as well.
🌐
TheServerSide
theserverside.com › definition › Java-string
What Is a Java String?
Memory is split into two high-level blocks, the stack and the heap. Java stores object values in heap memory; references to the value are stored in the stack. Another way to create strings is to use the new keyword, as in the following example.
🌐
Wikipedia
en.wikipedia.org › wiki › List_of_Java_keywords
List of Java keywords - Wikipedia
October 20, 2025 - In the Java programming language, a keyword is any one of 68 reserved words that have a predefined meaning in the language. Because of this, programmers cannot use keywords in some contexts, such as names for variables, methods, classes, or as any other identifier.
🌐
W3Schools
w3schools.com › java › java_ref_keywords.asp
Java Keywords
Java Wrapper Classes Java Generics Java Annotations Java RegEx Java Threads Java Lambda Java Advanced Sorting ... How Tos Add Two Numbers Swap Two Variables Even or Odd Number Reverse a Number Positive or Negative Square Root Area of Rectangle Celsius to Fahrenheit Sum of Digits Check Armstrong Num Random Number Count Words Count Vowels in a String Remove Vowels Count Digits in a String Reverse a String Palindrome Check Check Anagram Convert String to Array Remove Whitespace Count Character Frequency Sum of Array Elements Find Array Average Sort an Array Find Smallest Element Find Largest Element Second Largest Array Min and Max Array Merge Two Arrays Remove Duplicates Find Duplicates Shuffle an Array Factorial of a Number Fibonacci Sequence Find GCD Check Prime Number ArrayList Loop HashMap Loop Loop Through an Enum
🌐
TutorialsPoint
tutorialspoint.com › can-string-be-considered-as-a-keyword
Can String be considered as a Keyword?
Java also contains the 'String' term, but it is not a keyword. In Java, the 'String' refers to a predefined class containing various methods to manipulate the text data.
🌐
freeCodeCamp
freecodecamp.org › news › strings-in-java
Strings in Java
February 1, 2020 - In Java, a String is an Object. Strings should not be confused with char as characters are literally 1 value rather than a sequence of characters. You can still use 1 value within a String, however it is preferred to use char when you are checking ...