Difference between String concatenation operator (+) and concat() Method

In Java, String concat() method is used to concat the specified string to the end of current string. Concatenation operator (+) is also used to concat the specified string to the end of current string.

But there are some differences between concatenation operator (+) and method concat() in terms of their behaviour.

Difference between String concatenation operator (+) and concat() Method

(1.) Concatenation operator (+) does not throw NullPointerException when null String is concat to current string where as concat() method throws NullPointerException when null String is concat to Current String.
e.g.

String st =”Hello”+ null;
System.out.println(st); // Output : Hellonull
String str = “Hello”;
System.out.println(str.concat(null)); // output : NullPointerException will be thrown

(2.)Concatenation operator (+) does not require both operands to be of type String but in concat() method both must be of type String as the argument of method concat() is of type String.

e.g.

String st =”Hell”+ 50;
System.out.println(st); // Output : Hell50
String str = “Hello”;
System.out.println(str.concat(“World”)); // output : HelloWorld