How to concatenate string with null without explicitly checking?

Asked

Viewed 441 times

2

When I try to concatenate one String which, in fact, is null with a string valid (e.g., "a"), get the value nulla. See the example below:

String a = "a";
String b = null;
System.out.println(b + a);

I wanted the output to be just "a". Is there any way to do this without checking if the String is null?

  • 1

    It makes no sense to concatenate with null, the recommended is always to check if any of the variables is null before trying to concatenate.

  • Do you control the value of String b? It’s not interesting to abuse null . Often when something returns null, it should have returned a standard object (in this case, an empty string) or launched an exception.

  • Did the answer solve what you were looking to know? Do you think you can accept it now? If not, you need something else to be improved?

3 answers

6

You shouldn’t try to concatenate a null with a text. I even think java should either prohibit concatenation in this case, or adopt a string empty when it finds a null (each has pros and cons). As it is not so, the solution is this:

System.out.println((b == null ? "" : b) + a);

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Obviously you can create a utility method to do this in a generic way and use whenever necessary.

  • But java launches a nullpointerexception if it tries to do null operation, this would no longer be a way to "prohibit"?

  • @diegofm not in this case. Just so you can see that it’s not even println(): http://ideone.com/KctKc9 There must be some coercion rule that determines this. I know that in C# h pa a rule that turns into "". https://dotnetfiddle.net/2E1mmK

  • Wow, so jre gets null and, when concatenating, turns it into a string. What a strange thing

4

It is ensured that a has a value and only b can come null, can follow what was answered by Maniero.

If you are unsure and/or need to test a larger number of strings, it may be more interesting to use Optional to check if an object is null. And if this is, return some predefined value that will not disturb concatenation (an empty string maybe?!) :

public static String get(String string){
   return Optional.ofNullable(string).orElse("");
}

Using:

String a = "a", b = null, c = "c";    

get(a) + get(b); // "a"
get(a) + get(b) + get(c); // "ac"
get(b); // ""

IDEONE

  • That’s what I’m talking about :)

3

I believe that instead of setting a variable like String as "null" you should set it as "". For example:

public static void main(String[] args) {
    String a = "a";
    String b = "";
    System.out.println(a+b);
}

So he returns the right one to you;

  • Don’t take this the wrong way, but I don’t see your answer to the problem. I believe that the example of the question was for demonstrative purposes, what if he has no control over what comes in this variable? What if its value is defined by another method? Anyway, your suggestion and several hypotheses as a solution to the doubt.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.