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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T09:08:05+00:00 2026-06-07T09:08:05+00:00

Why in Java you’re able to add Strings with the + operator, when String

  • 0

Why in Java you’re able to add Strings with the + operator, when String is a class? In theString.java code I did not find any implementation for this operator. Does this concept violate object orientation?

  • 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-07T09:08:07+00:00Added an answer on June 7, 2026 at 9:08 am

    Let’s look at the following simple expressions in Java

    int x=15;
    String temp="x = "+x;
    

    The compiler converts "x = "+x; into a StringBuilder internally and uses .append(int) to “add” the integer to the string.

    5.1.11. String Conversion

    Any type may be converted to type String by string conversion.

    A value x of primitive type T is first converted to a reference value
    as if by giving it as an argument to an appropriate class instance
    creation expression (§15.9):

    • If T is boolean, then use new Boolean(x).
    • If T is char, then use new Character(x).
    • If T is byte, short, or int, then use new Integer(x).
    • If T is long, then use new Long(x).
    • If T is float, then use new Float(x).
    • If T is double, then use new Double(x).

    This reference value is then converted to type String by string
    conversion.

    Now only reference values need to be considered:

    • If the reference is null, it is converted to the string “null” (four ASCII characters n, u, l, l).
    • Otherwise, the conversion is performed as if by an invocation of the toString method of the referenced object with no arguments; but
      if the result of invoking the toString method is null, then the
      string “null” is used instead.

    The toString method is defined by the primordial class Object
    (§4.3.2). Many classes override it, notably Boolean, Character,
    Integer, Long, Float, Double, and String.

    See §5.4 for details of the string conversion context.

    15.18.1.

    Optimization of String Concatenation :
    An implementation may choose to perform conversion and concatenation
    in one step to avoid creating and then discarding an intermediate
    String object. To increase the performance of repeated string
    concatenation, a Java compiler may use the StringBuffer class or a
    similar technique to reduce the number of intermediate String objects
    that are created by evaluation of an expression.

    For primitive types, an implementation may also optimize away the
    creation of a wrapper object by converting directly from a primitive
    type to a string.

    The optimized version will not actually do a full wrapped String conversion first.

    This is a good illustration of an optimized version used by the compiler, albeit without the conversion of a primitive, where you can see the compiler changing things into a StringBuilder in the background:

    http://caprazzi.net/posts/java-bytecode-string-concatenation-and-stringbuilder/


    This java code:

    public static void main(String[] args) {
        String cip = "cip";
        String ciop = "ciop";
        String plus = cip + ciop;
        String build = new StringBuilder(cip).append(ciop).toString();
    }
    

    Generates this – see how the two concatenation styles lead to the very same bytecode:

     L0
        LINENUMBER 23 L0
        LDC "cip"
        ASTORE 1
       L1
        LINENUMBER 24 L1
        LDC "ciop"
        ASTORE 2
    
       // cip + ciop
    
       L2
        LINENUMBER 25 L2
    
        NEW java/lang/StringBuilder
        DUP
        ALOAD 1
        INVOKESTATIC java/lang/String.valueOf(Ljava/lang/Object;)Ljava/lang/String;
        INVOKESPECIAL java/lang/StringBuilder.<init>(Ljava/lang/String;)V
        ALOAD 2
        INVOKEVIRTUAL java/lang/StringBuilder.append(Ljava/lang/String;)Ljava/lang/StringBuilder;
        INVOKEVIRTUAL java/lang/StringBuilder.toString()Ljava/lang/String;
    
        ASTORE 3
    
        // new StringBuilder(cip).append(ciop).toString()
    
       L3
        LINENUMBER 26 L3
    
        NEW java/lang/StringBuilder
        DUP
        ALOAD 1
        INVOKESPECIAL java/lang/StringBuilder.<init>(Ljava/lang/String;)V
        ALOAD 2
        INVOKEVIRTUAL java/lang/StringBuilder.append(Ljava/lang/String;)Ljava/lang/StringBuilder;
        INVOKEVIRTUAL java/lang/StringBuilder.toString()Ljava/lang/String;
    
        ASTORE 4
       L4
        LINENUMBER 27 L4
        RETURN
    

    Looking at the example above and how the byte code based on the source code in the given example is generated, you will be able to notice that the compiler has internally transformed the following statement

    cip+ciop; 
    

    into

    new StringBuilder(cip).append(ciop).toString();
    

    In other words, the operator + in string concatenation is effectively a shorthand for the more verbose StringBuilder idiom.

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

Sidebar

Related Questions

Java Code: import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegExpTest { public static void main(String[]
Java Code : DOMConfigurator.configureAndWatch(log4jConfigFile.getAbsolutePath(),100000L); I need to add some appenders programmaticaly whenever Thread created
Java Code : package Package; public class IntArray { private native int sumArray(int[] arr);
Java Code In Java code I have class called IdentificationResult which has 3 members:
// java interface MyInterface { class MyInnerClass { public static final String myInnerStaticMethod() {
Java noob question: Consider the following C array and initializer code: struct { int
Java newbie here, I am trying to find the output of the following chunk
java docs says: A pool of strings, initially empty, is maintained privately by the
Java Code need to write an equivalent in objectiveC. CAn you please help me
Java Concurrency in Practice gives the following example of an unsafe class which due

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.