Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (1.4k points)

What is the difference between these two following statements?

String s = "text";

String s = new String("text");

1 Answer

0 votes
by (13.2k points)

String s = new String("text");

It will create a new string object explicitly. Hence, its compilation is slower than the other.

String s = "text";

It creates a string literal from the string constant pool, if available. It will be stored in Heap Space. For code optimization and better performance, use this one.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...