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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T12:51:32+00:00 2026-05-19T12:51:32+00:00

why abc + null results abcnull String s1 = abc; String s2 = null;

  • 0

why "abc" + null results abcnull

String s1 = "abc";
String s2 = null; 
String s3 = s1+ s2;

System.out.println(s3);

Result: abcnull

  • 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-19T12:51:33+00:00Added an answer on May 19, 2026 at 12:51 pm

    Because Java will create a StringBuilder to append s1, or "abc", to s2, or null.

    According to the spec for StringBuilder.append(String)–

    If str is null, then the four characters "null" are appended
    

    So it turns into the same as "abc" + "null"

    Let’s take your code example (I placed it inside a method):

     public static void main(String[] args)
     {
      String s1 = "abc";
      String s2 = null; 
      String s3 = s1+ s2;
      System.out.println(s3);
     }
    

    If we take a gander at the bytecode (gotten by invoking javap -c):

    public static void main(java.lang.String[]);
      Code:
       0:   ldc #2; //String abc
       2:   astore_1
       3:   aconst_null
       4:   astore_2
       5:   new #3; //class java/lang/StringBuilder
       8:   dup
       9:   invokespecial   #4; //Method java/lang/StringBuilder."<init>":()V
       12:  aload_1
       13:  invokevirtual   #5; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
       16:  aload_2
       17:  invokevirtual   #5; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
       20:  invokevirtual   #6; //Method java/lang/StringBuilder.toString:()Ljava/lang/String;
       23:  astore_3
       24:  getstatic   #7; //Field java/lang/System.out:Ljava/io/PrintStream;
       27:  aload_3
       28:  invokevirtual   #8; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
       31:  return
    

    Java creates a StringBuilder and appends the values, as Strings, of s1 and s2.

    So you have to be careful then, if you were expecting a NullPointerException, because concatenation in Java will skirt the issue.

    Side Note: As pointed out by Jon Skeet, this is ultimately just implementation details — it’s the Java spec that guarantees that null, when converted to a String, turns into “null”. However, this bytecode at least shows what is actually happening behind the scenes.

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

Sidebar

Related Questions

Can someone explain to me why my code: string messageBody = abc\n + stringFromDatabaseProcedure;
I have a defunct process on my system: abc 22093 19508 0 23:29 pts/4
I am looking for the VB.NET equivalent of var strings = new string[] {abc,
I want to split a string like this: abc//def//ghi into a part before and
I’m trying to split a String . Simple examples work: groovy:000> print abc,def.split(,); [abc,
Antlr3 produces the following output to System.out , while doing everything else correctly: line
XmlNodeList list = null; list = xmlResult.SelectNodes(/sitecore /result /item [scWebsitePath='+sitecoreContextItemPath+' and scTemplateId='+templateId+' and scDateCreated
I am using a string variable in a class Abc , but i am
Have a interface class abc { public: virtual int foo() = 0; ... }
I have an ABC with several derived classes. To create these derived classes I

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.