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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:37:25+00:00 2026-06-14T03:37:25+00:00

With newer compilers I find myself trying to write code that is easier to

  • 0

With newer compilers I find myself trying to write code that is easier to read but may be more memory hungry if the optimization I hope are being done under the hood are not actually being done.
Take this code for example, very simple case

while (scanner.hasNextLine() && !result)
{
    String line = scanner.nextLine();
    result = line.indexOf(searchString) >= 0;
}

Would it be fair to assume ( Using Eclipse Juno, Java 7 ) that this will generate the same byte code as

while (scanner.hasNextLine() && !result)
{
    result = scanner.nextLine().indexOf(searchString) >= 0;
}

The former although 2 lines of code reduces the length of the second line and makes it easier on the eye. IMHO
But would it also result in an unessecary String object being created? I hope not …

  • 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-14T03:37:26+00:00Added an answer on June 14, 2026 at 3:37 am

    You cannot escape the String being created. The fact that you assign it to a local variable is irrelevant here and actually at the bytecode level this fact will not even be noticed: at that level, with or without an explicit variable, the reference to the result must be placed on the stack in order to be passed into the next method invocation in the chain.

    Your idea that an unnecessary String instance is created may stem from an instinct coming from another language, such as C: the assignment String s = ... only copies the reference to the one and only string instance. This is due to the fact that all Java objects reside on the heap, so you always need to explicitly copy an object to actually involve another instance. For example, if you wrote String line = new String(scanner.nextLine()), that would indeed create an unnecessary instance of String.

    To conclude, there is no optimization involved in any version of your code, so choose according to stylistic preference only.

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

Sidebar

Related Questions

I think that some newer languages like JS can do this natively, but I
I'm trying to do some really simple stuff in C++, but I can't find
I have Googled and read, but didn't find answers for these three simple questions...
I'm trying to compile hackport ( here is sources: https://github.com/Nensha/hackport ) with newer cabal
I'm newer to Xcode development and am trying to save the state of my
I'm a newer and if the question is so easy I apologize for that.
When i start writing something complex, I find that restart the writing like 10
Im learning C++ and I ran into something strange that I couldn't find any
I tried looking for the answer but didn't find it. I accidentally deleted my
I'm trying to compile some code I found on GitHub https://github.com/tapio/Wendy I'm just trying

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.