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

  • Home
  • SEARCH
  • 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 297911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T06:40:58+00:00 2026-05-12T06:40:58+00:00

This is a question that arose mostly of pure curiosity (and killing some time).

  • 0

This is a question that arose mostly of pure curiosity (and killing some time). I’m asking specifically about Java for the sake of concreteness.

What happens, in memory, if I concatenate a string (any string) with an empty string, e.g.:

String s = "any old string";
s += "";

I know that afterward, the contents of s will still be “any old string”, since an empty ASCII string is stored in memory as just an ASCII null (since, at least in Java, strings are always null-terminated). But I am curious to know if Java (the compiler? the VM?) performs enough optimization to know that s will be unchanged, and it can just completely omit that instruction in the bytecode, or if something different happens at compile and run times.

  • 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-12T06:40:59+00:00Added an answer on May 12, 2026 at 6:40 am

    It’s bytecode time!

    class EmptyString {
        public static void main(String[] args) {
            String s = "any old string";
            s += "";
        }
    }
    

    javap -c EmptyString:

    Compiled from "EmptyString.java"
    class EmptyString extends java.lang.Object{
    EmptyString();
      Code:
       0:   aload_0
       1:   invokespecial   #1; //Method java/lang/Object."":()V
       4:   return
    
    public static void main(java.lang.String[]);
      Code:
       0:   ldc     #2; //String any old string
       2:   astore_1
       3:   new     #3; //class java/lang/StringBuilder
       6:   dup
       7:   invokespecial   #4; //Method java/lang/StringBuilder."":()V
       10:  aload_1
       11:  invokevirtual   #5; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
       14:  ldc     #6; //String
       16:  invokevirtual   #5; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
       19:  invokevirtual   #7; //Method java/lang/StringBuilder.toString:()Ljava/lang/String;
       22:  astore_1
       23:  return
    
    }
    

    You can see that += causes a StringBuilder to be created regardless of what it’s concatenating, so it can’t be optimized at runtime.

    On the other hand, if you put both String literals in the same expression, they are concatenated by the compiler:

    class EmptyString {
        public static void main(String[] args) {
            String s = "any old string" + "";
        }
    }
    

    javap -c EmptyString:

    Compiled from "EmptyString.java"
    class EmptyString extends java.lang.Object{
    EmptyString();
      Code:
       0:   aload_0
       1:   invokespecial   #1; //Method java/lang/Object."":()V
       4:   return
    
    public static void main(java.lang.String[]);
      Code:
       0:   ldc     #2; //String any old string
       2:   astore_1
       3:   return
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have seen this question that talks about getting the last part of a
This question arose out of something strange that I noticed after investigating this question
I have some confusion with this that arose from messing around with exporting generic
G'day, Edit: While this question covers a situation that can arise in programming a
Found this question that explained a way to communicate between layers. However there is
I found this question that is discussing what I would like to do, but
I found this question that was very useful in learning the basics of the
I have a UIView subclass ( CustomView for purposes of this question) that has
This a question that is more on the lines of server efficiency and availability.
This question is sort of a sequel to that question. When we want to

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.