What do we understand from string pool in java

0 votes
I came across the term string pool. What does it mean?
Apr 30, 2018 in Java by sophia
• 1,400 points
700 views

1 answer to this question.

0 votes

This prints true (even though we don't use equals method: correct way to compare strings)

    String x = "p" + "qr";
    String y = "pq" + "r";
    System.out.println(x == y);

When compiler optimizes your string literals, it sees that both x and y have same value and thus you need only one string object. It's safe because String is immutable in Java.
As result, both x and y point to the same object and some little memory saved.

Name 'string pool' comes from the idea that all already defined string are stored in some 'pool' and before creating new String object compiler checks if such string is already defined.

answered Apr 30, 2018 by Daisy
• 8,140 points

Related Questions In Java

0 votes
2 answers

What is the use of @Override annotation in Java ? When do we use it ?

@Override annotation is used when we override ...READ MORE

answered Aug 14, 2019 in Java by Sirajul
• 59,230 points
3,471 views
0 votes
1 answer

How do I create a Java string from the contents of a file?

If you're looking for an alternative that ...READ MORE

answered Apr 19, 2018 in Java by Rishabh
• 3,620 points
1,179 views
0 votes
2 answers

How can we remove an element from an array in Java?

You can use ArrayUtils class remove method ...READ MORE

answered May 24, 2018 in Java by UshaK
2,734 views
0 votes
1 answer

How can we split a comma separated String in Java?

String str = "..."; // write the ...READ MORE

answered Jun 12, 2018 in Java by Parth
• 4,640 points
2,375 views
+5 votes
4 answers

How to execute a python file with few arguments in java?

You can use Java Runtime.exec() to run python script, ...READ MORE

answered Mar 27, 2018 in Java by DragonLord999
• 8,450 points

edited Nov 7, 2018 by Omkar 81,222 views
+1 vote
1 answer

How to handle drop downs using Selenium WebDriver in Java

First, find an XPath which will return ...READ MORE

answered Mar 27, 2018 in Selenium by nsv999
• 5,500 points
8,358 views
0 votes
1 answer

What are the differences between getText() and getAttribute() functions in Selenium WebDriver?

See, both are used to retrieve something ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,500 points
17,432 views
0 votes
1 answer

Selenium JARS(Java) missing from downloadable link

Nothing to worry about here. In the ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,500 points

edited Aug 4, 2023 by Khan Sarfaraz 4,875 views
0 votes
4 answers

Remove extra spaces from string in java

import java.util.regex.Matcher; import java.util.regex.Pattern; String pattern="[\\s]"; String replace=""; part="name=john age=13 year=2001"; Pattern ...READ MORE

answered Sep 10, 2018 in Java by Parth
• 4,640 points
3,054 views
0 votes
3 answers

How can we use java.String.format in Scala?

String aString = "world"; int aInt = 20; String.format("Hello, ...READ MORE

answered Aug 29, 2018 in Java by Daisy
• 8,140 points
1,764 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP