Difference between concat and + in java

Difference between + and concat():

Using + operator:

1. Any type of variable can be concatenated, it is not mandatory to be a string variable.

String firstVar = "Java";
int secVar = 11;
String plusConcat = firstVar + secVar;
System.out.println(plusConcat); // prints Java11

2. String can be null and it will not throw null pointer exception.

String myVar = null;
String plus = myVar + "java";
System.out.println(plus); // prints nulljava


Using Concat():

1. Argument must be string for concatenation using concat()

a.concat(11) //it is not possible

2. String must not be null, if it is null then it will throw null pointer exception.

String myVar = null;
String value = "java";
System.out.println(myVar.concat(value)); // throws null pointer exception

803 total views, 2 views today

About Mirthbees 679 Articles
Blogger, Innovator, Developer and Enterpreneur. Founder of GuruRecharge, Readymade online recharge script. Lead Developer in Bookrate.in

Be the first to comment

Leave a Reply

Your email address will not be published.


This site uses Akismet to reduce spam. Learn how your comment data is processed.