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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:29:43+00:00 2026-06-11T15:29:43+00:00

When you concatenate a String with a primitive such as int, does it autobox

  • 0

When you concatenate a String with a primitive such as int, does it autobox the value first.

ex.

String string = "Four" + 4;

How does it convert the value to a string in Java?

  • 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-11T15:29:44+00:00Added an answer on June 11, 2026 at 3:29 pm

    To see what the Java compiler produces it is always useful to use javap -c to show the actual bytecode produced:

    For example the following Java code:

    String s1 = "Four" + 4;
    int i = 4;
    String s2 = "Four" + i;
    

    would produce the following bytecode:

       0:   ldc     #2; //String Four4
       2:   astore_1
       3:   iconst_4
       4:   istore_2
       5:   new     #3; //class java/lang/StringBuilder
       8:   dup
       9:   invokespecial   #4; //Method java/lang/StringBuilder."<init>":()V
       12:  ldc     #5; //String Four
       14:  invokevirtual   #6; //Method java/lang/StringBuilder.append:(Ljava/lang/
    String;)Ljava/lang/StringBuilder;
       17:  iload_2
       18:  invokevirtual   #7; //Method java/lang/StringBuilder.append:(I)Ljava/lan
    g/StringBuilder;
       21:  invokevirtual   #8; //Method java/lang/StringBuilder.toString:()Ljava/la
    ng/String;
       24:  astore_3
       25:  return
    

    From this we can see:

    • In the case of "Four" + 4, the Java compiler (I was using JDK 6) was clever enough to deduce that this is a constant, so there is no computational effort at runtime, as the string is concatenated at compile time
    • In the case of "Four" + i, the equivalent code is new StringBuilder().append("Four").append(i).toString()
    • Autoboxing is not involved here as there is an StringBuilder.append(int) method which according to the docs is using String.valueOf(int) to create the string representation of the integer.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is the reason you can concatenate a primitive type to a String due to
I want to convert (USERID=XYZ USERPWD=123) to USERID=XYZ&USERPWD=123 . I tried (apply #'concatenate 'string
onclick=timeTracker._recordEndTime(); timeTracker._track(pageTracker, 'CatagoryName', document.getElementById('FirstName').value); I want to concatenate a string(for example 'User name') to
I would like concatenate string literals and ints, like this: string message(That value should
I need to concatenate the string value of a spring bean, to an existing
Possible Duplicate: C++ concatenate string and int Hi, In C# I can write like
I would like to concatenate a string using lambda to compare that concatenated value
Possible Duplicate: Easiest way to convert int to string in C++ I have a
What would be the best way to properly concatenate string with an url inside?
How to aggregate string( concatenate) with Oracle 10g SQL?

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.