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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:24:21+00:00 2026-06-03T01:24:21+00:00

I have coded like follows, for(int i=0 ; i<n; i++){ String a = someObject.getFirstName();

  • 0

I have coded like follows,

for(int i=0 ; i<n; i++){

String a = someObject.getFirstName(); //may not come same value..
doManipulationon(a);
}

During code review, people asked me to remove the string literal a and use someObject.getFirstName() directly for manipulation. They afraid that string object will be created in heap on every iteration.

Is this the correct way?

I thought assigning the getter into a string variable gives code more readable.

  • 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-03T01:24:22+00:00Added an answer on June 3, 2026 at 1:24 am

    First there is no string literal here.

    A string literal is a string expression enclosed in double quotes, like “dog”.

    Your reviewers are stating that they do not like the temporary variable a used to hold an expression that you are manipulating in the very next line.

    Apparently they want you to say

    doManipulationon(someObject.getFirstName());
    

    Removing temporary variables leads to more compact code. This is usually, but not always, a good idea. You can use temporary variables when your “intermediate expression” has an interesting meaning, but in your case, using the name a was not helpful. You could have said

    String firstname = someObejct.getFirstName();
    

    In long expressions, especially, assigning partial results to variables that are given meaningful names does add to readability.

    Oh, and as to their comment about new string objects being added to the heap in your example — no, none will.

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

Sidebar

Related Questions

I have a coded string that I'd like to retrieve a value from. I
I have something like this hard-coded: private string[,] m_RolesForUser = new string[,] { {John,President,Chair},
Suppose I have a function f(n:Int):Option[String] . I would like to find such 1
I have a sample class like this: SampleClass { string code; int id; string
I have codes just like follows: class test: def do_something(): pass test1 = test()
I have a hard coded URL like so: https://bupacouk.bwa.local.internal.bupa.co.uk/cash-plan-quote/quoteAction.do?getBenefitLevelDetails=getBenefitLevelDetails&productPolicyId=7841#a1 When Javascript is enabled i
Suppose I had a WCF service that I have coded up, like Clemens Vasters's
I have 4 hard coded divs. After the third div I would like to
I have my main application class as follows and what I would like to
When I have a Dictionary<string, int> actual and then create a completely new Dictionary<string,

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.