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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:48:29+00:00 2026-05-23T17:48:29+00:00

I can use either concatenation operator (+) or concat() method for string concatenation String

  • 0

I can use either concatenation operator(+) or concat() method for string concatenation

String myData = "a"+"b";            // using concatenation operator
String myData = "a".concat("b");    // using concat() of String

But to concate a string with integer I cannot use concat() directly. SO I have to use either of the following logic

String myData = "a"+5;
String myData = "a".concat(String.valueOf(5));

But I found some thing strange in the following line when I want to use concatenation operator and concat

String myData = "a"+null;              //output =  anull
    String myData = "a".concat(String.valueOf(5)); // output = NullPointerException
or
    String myData = "a".concat(null); // output = NullPointerException

I have below question arised in my mind

1) How concat() method and concatenation operator works what is the difference in their logic of performing any task?
2) Can we really concat a null using (+) if so why concat() method cannot achieve the same

Thanks

  • 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-23T17:48:29+00:00Added an answer on May 23, 2026 at 5:48 pm

    1) The + operator (to produce Strings) always goes through an intermediate StringBuilder (or StringBuffer if targeting old platforms before 1.5 (released 7 (seven) years ago)). For concatenating two or perhaps three Strings concat will generally be faster due to the lack of intermediate. However, for longer concatenations + will win because there will be fewer intermediate allocations.

    2) null generally indicates an error (quite possibly a design error). In general, errors should be reported as early as possible, which String.concat does. However, + or StringBuilder concatenation is often used to produce debug strings, so the null is tolerated and produces a result suitable for debugging (but not UI!).

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

Sidebar

Related Questions

For string concatenation we can use either the concat() or concat operator (+) .
If we want to get a value from a method, we can use either
We have got a setup where users can either use a web-based form or
On Unix, I can either use \r (carriage return) or \b (backspace) to overwrite
Can anyone please tell me is there any special requirement to use either EXTERN
How can I configure an application, or even an entire machine, to use either
How can you use the following sample data either with a foreach or while
You can use a standard dot notation or a method call in Objective-C to
You can use SelectFolder() to get a folder or GetOpenFolderitem(filter as string) to get
in the tutorial i can use either: public function configure() { $this->setWidgets(array( 'type' =>

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.