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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:38:31+00:00 2026-05-14T07:38:31+00:00

Hi i encountered this problem whereby when i initialized my String[], there seems to

  • 0

Hi i encountered this problem whereby when i initialized my String[], there seems to be a null in the String[] before i do anything. How do i initialized the String[] to be completely empty,i.e. without the null at the start?
The output for the following code is:


nullABC

nullABC

nullABC

nullABC

nullABC

public static void main(String[] args){
    String[] inputArr = new String[5];
    for (int i = 0; i< inputArr.length; i++){
        inputArr[i] += "ABC";
    }
    for (int i = 0; i< inputArr.length; i++){
        System.out.println(inputArr[i]);
    }
}

}

  • 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-14T07:38:32+00:00Added an answer on May 14, 2026 at 7:38 am

    A null reference is about as empty as a string array element can be. Note that there’s a big difference between a reference to the empty string and a null reference though. Just change your code to use simple assignment instead of +=.

    for (int i = 0; i< inputArr.length; i++){
        inputArr[i] = "ABC";
    }
    

    If you need to do conditional concatenation elsewhere, use something like this:

    for (int i = 0; i< inputArr.length; i++) {
        String current = inputArr[i];
        String suffix = "ABC";
        String replacement = current == null ? suffix : current + suffix;
        inputArr[i] = replacement;
    }
    

    Alternatively, you could use something like this:

    public static String emptyForNull(String x) {
        return x == null ? "" : x;
    }
    

    and then have:

    for (int i = 0; i< inputArr.length; i++){
        inputArr[i] = emptyForNull(inputArr[i]) + "ABC";
    }
    

    Or (yes, lots of available options):

    public static String nullAwareConcat(String x, String y) {
        return x == null && y == null ? ""
            : x == null ? y
            : y == null ? x
            : x + y;
    }
    
    ...
    
    for (int i = 0; i< inputArr.length; i++){
        inputArr[i] = nullAwareConcat(inputArr[i], "ABC");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Encountered this problem before but forgot how I solved it. I want to use
Uhm I'm not sure if anyone has encountered this problem a brief description is
I have encountered a problem twice now whereby a producer thread produces N work
I recently encountered this problem. I found many instances of people asking the question—
I encountered this problem when doing some enthusiastic programming. The problem can be expressed
I encountered this problem Unable to Copy File *\bin\debug\*.dll to bin\*.dll And when this
I encountered this problem when updating my background to use a 9-patch image. The
Since I've never encountered this kind of problem I thought about making language parser
Wondering if anyone else has encountered this problem when utilizing the new ability to
i am getting started with zend framework.I have encountered this problem with forms. when

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.