Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7049853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:04:18+00:00 2026-05-28T03:04:18+00:00

public String toString() { return NotSubscribed; } We are trying to limit String’s creation

  • 0
public String toString()
{
     return "NotSubscribed";
}

We are trying to limit String’s creation in our application after the objects are instantiated. So, just wondering if this creates a new String every time this toString is called. It might be a bad question, but I don’t know the answer. Please suggest.

—

I just remembered that if a String is created, it is stored in the String Pool and after that it is always retrieved from the pool without being re-created. Am I right?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T03:04:19+00:00Added an answer on May 28, 2026 at 3:04 am

    No, that won’t create another string each time. String constants are interned – so actually the constant “NotSubscribed” will refer to the same string object throughout your code, not even just every time this method is executed.

    From section 3.10.5 of the Java Language Specification:

    Each string literal is a reference (§4.3) to an instance (§4.3.1, §12.5) of class String (§4.3.3). String objects have a constant value. String literals-or, more generally, strings that are the values of constant expressions (§15.28)-are “interned” so as to share unique instances, using the method String.intern.

    However, in your edit:

    I just remembered that if a String is created, it is stored in the String Pool and after that it is always retrieved from the pool without being re-created. Am I right?

    No, that’s not right. Only string constants are interned by default. For example, every time you run this code:

    public List<String> createStrings() {
        List<String> list = new ArrayList<String>();
        for (int i = 0; i < 10; i++) {
            list.add("foo" + i);
        }
        return list;
    }
    

    … that will create another 10 strings.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i wrote a simple function in controller public string LinkProjectSquareFilter(int squareId) { return squareId.ToString();
I have a method in Eclipse as below. public String toString() { return HouseVo
Given the 2 toString() implementations below, which one is preferred: public String toString(){ return
Consider the following code snippet below. class X { public String toString() { return
The canonical use for ReflectionToStringBuilder is as follows public String toString() { return (new
public class Atribut { int classid; @Override public String toString() { return Integer.toString(classid); }
Let's say I want to generate this output: public String toString() { return this.getFirstName()
Given the following java enum: public enum AgeRange { A18TO23 { public String toString()
I am doing the following class RuleObject implements Comparable{ @Override public String toString() {
public class MyFrame extends JFrame { public MyFrame(String title) { setSize(200, 200); setTitle(Integer.toString(super.getSize().width)); setLayout(new

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.