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 7741635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:04:33+00:00 2026-06-01T09:04:33+00:00

String str1 = Test; final String str2 = new String(Test); As we know, the

  • 0
String str1 = "Test";
final String str2 = new String("Test");

As we know, the str1 will be created in String pool and str2 will be created in Heap. Here, both objects are immutable. Is there any difference in performance(while performing GC)? Is it correct to say creating String objects in Heap leads to memory leak?

  • 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-06-01T09:04:34+00:00Added an answer on June 1, 2026 at 9:04 am

    No, String objects in the heap don’t lead to memory leaks. They can be garbage collected just like regular objects.

    However, you might be using a lot of memory to hold string objects that are identical in content. Holding 1 million references to the same object is more memory-efficient than creating 1 million different objects.

    Note that you can use the intern function to add a string obtained at runtime to the String pool.

    public class Test {
       public static void main(String[] args) {
          new Test().run();
       }
    
       private void run() {
          String str1 = "Test";
          String str2 = "Test";
          System.out.println(str1 == str2);
    
          String str3 = new String("Test");
          String str4 = new String("Test");
          System.out.println(str3 == str4);
    
          String str5 = str3.intern();
          String str6 = str3.intern();
          System.out.println(str5 == str6);
       }
    }
    

    Output:

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

Sidebar

Related Questions

we make the following String objects? String str1 = new String(ABC); String str2 =
string str1(someString); string str2 = string(str1);//how many copies are made here //copy2 = copy1?
Considering the code below: How is the str1 object created without using new String()
String str1 =C:\Users\mehdi\Documents\NetBeansProjects\project1\src\res\1 (21).png ; String str2 = \res\1 (21).png; jLabel24.setIcon(new ImageIcon(getClass().getResource( ? )
Assuming I have the following strings: string str1 = Hello World!; string str2 =
Using the STL C++ hash_map... class MyKeyObject { std::string str1; std::string str2; bool operator==(...)
I'd like to do something like this String.concat '\n' [str1; str2 ... strn] so
String[] a = c.toArray(new String[0]); First: Do I need type cast here? (I think
object obj = Hello; string str1 = (string)obj; string str2 = obj.ToString(); What is
I wrote the function (defun test () (let ((str1 foo) (str2 bar)) (loop for

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.