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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:28:53+00:00 2026-05-27T05:28:53+00:00

If I have code like below is it feasible? String b = abc; String

  • 0

If I have code like below is it feasible?

String b = "abc";
String c = "def";
for (int i=0;i<100000000;i++){
    String a = b + c; // i got a different object , ahhh!
}

How does it affect the system? Can we improve it and how?

Doesn’t that follow String pool concept, since i am creating String without new operator i’m ending with 1 object, in result it was creating 100000000 object(i was wrong) but i didn’t unserstand how (checked with == operator)

for example

final String b = "abc";
final String c = "def";
for (int i=0;i<100000000;i++){
    String a = b + c; //same object referred again and again
}

gives same object , i was able to check with == operator

Isn’t that both the example follow String pool concept? Why does if i have final for my String variable varies the outcome of different object or same object .

  • 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-27T05:28:53+00:00Added an answer on May 27, 2026 at 5:28 am

    If b and c are not marked final, the compiler probably assumes that at some point in the code, different strings might be assigned to those variables. As such, when you write a=b+c, the compiler can’t assume anything about the contents of b and c (they could even come from the user) so it has to concatenate them and produce a brand new string.

    When they’re final, the compiler can know for sure that b is always "abc" and c is always "def", and possibly even infer that (b+c)=="abcdef", and as such put it in a pool or simply do the concatenation before the loop.

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

Sidebar

Related Questions

i have a code in c# like below string s = new string('~',25); int
I met an interesting issue about C#. I have code like below. List<Func<int>> actions
I have sample java code like below. String testEfdDirectoryPath=D:\\test; String efdExecutable = test.cmd; File
I have a code like this below, which gives me a $link that equals
Hi I have below pseudo code with throws an exception like this throw new
I have a data that looks like this . And my code below simply
I have HTML code like below <input type = textarea id=sentence1> Here is my
I have a menubar and tabs the code like below: <div id=menu> <ul> <li><a
Let us say we have some code that looks like below: @interface SomeClass :
I have a String like below: <script language=JavaScript type=text/javascript src=http://dns.net/adj/myhost.com/index;size=5x10;zipc=12345;myzon=north_west;|en;tile=10;ord=7jkllk456?></script> I want to access

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.