Which is the most efficient way of creating a string object in java? String s="helo"; or String s=new String("helo"); in terms of memory usage?
Thanks in advance...

Dani AI

Generated

and nailed the practical rule: avoid creating a brand-new String object when you can use a pooled literal. A few useful clarifications and actionable tips that go beyond the short answers in this thread.

The JVM keeps a pool of interned string values (see the language spec), so reusing the pooled value usually saves memory and GC work. Exact storage and behaviour have changed across JVM releases (older JVMs used PermGen; modern JVMs keep interned strings on the heap and Java 9 changed the internal string representation), so the concrete cost of interning vs fresh objects depends on your Java version and GC. See the spec and runtime notes for details: JLS — String Literals and String.intern() javadoc. For the Java 9 compact-strings change, see JEP 254.

When you actually need canonicalization (lots of repeated content from IO, logs, CSVs, etc.), prefer an explicit canonical map rather than blindly interning everything. Example pattern using a concurrent map to hold canonical instances (keeps control of memory use and lifecycle):

private static final ConcurrentMap<String,String> CANON = new ConcurrentHashMap<>();

public static String canonical(String s) {
  String prev = CANON.putIfAbsent(s, s);
  return prev == null ? s : prev;
}

Measure before optimizing: produce heap dumps and inspect with tools such as VisualVM or the Eclipse Memory Analyzer to see whether duplicate strings are a real problem (VisualVM, Eclipse MAT). If you must reduce duplicates globally, String.intern() is available but affects the intern pool and should be used deliberately.

Short takeaways: the pooled literal approach is the usual memory win (as noted). For large datasets, use targeted canonicalization and profiling to guide changes rather than blanket micro-optimizations.

Recommended Answers

All 4 Replies

Read it for yourself

String objects are immutable and you can always reuse something that is immutable because you know that it isn't going to get modified. When you are writing "String s = new String("hello");", you are creating an unnecessary object; not only that it is also inefficient cause objects creation itself is. Moreover an object created this way cannot be reused. If this statement gets called in a loop or in a method it would unnecessarily create several objects which could all have been represented by a single one. So always use the "String s = "hello";" version instead of the other, this way any other string object with the same string literal can reuse this one.

PS: You seem so intelligent when parotting intelligent people....Here the intelligent person is Joshua Bloch, psst...but let this be sold on my name.

EDIT : Beaten, Peter has pointed out the exact article/item I was mentioning from.

I'm just lazy to type it specially if I can find exact page of whatever I read previously on given topic ;)

Thanks for the replies.....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.