
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference Between Concat and Plus Operator in Java
Java provides two ways to append strings and make them one. These two methods are namely the concat() method and + operator to join strings together, but there are some important differences between how they work.
The concat() method is a bit more specific and limited, while the + operator is more flexible. So, even though they do the same thing, understanding how and when to use each one can help in writing more efficient or cleaner code in Java.
Concat() Vs + operator
The following are the important differences between the concat method and the + operator.
Key | Concat Method | + Operator |
---|---|---|
Type | As syntax refers concat() is a method and comes under java.lang.String package and works on String objects. | On the other hand + is an operator and not a method it helps in adding numbers as well as concatenating values. |
Number of arguments | The concat() method could take the only argument as input and append this input to the target string on which this method is called. | + operator could append multiple strings and it is not bound to only input as a parameter and it works on different data types. |
Type of input | The concat() method could only take a string as input and would ask for a compile-time error if any other type of input has been provided to it. | + operator could take any type of input and convert it to a string before append to the target string. |
Output as a new string | The concat method would create a new string object as output after appending only if the output string has a length greater than zero otherwise return the same target string as an output object. | + operator always creates a new object as output no matter what length of result string is produced after appending. |
Exception during execution | The concat() method returns a null pointer exception in case this method is called with null as input. | + operator on the other hand does not throw any exception in a case called with null. |
Performance | In the case of the concat() method, no new object is created if the result string is of zero length hence it consumes less memory as compared to the + operator. | The + operator always creates a new object in the memory while appending the strings hence consuming more memory. |
Example
Following is an example to illustrate the concat() method.
ConcatDemo.java
public class ConcatDemo { public static void main(String args[]){ String s = "I am "; s = s.concat("Indian"); System.out.println(s); } }
Output
I am Indian
Example
Following is an example to illustrate the concat() method.OperatorDemo.java
public class OperatorDemo { public static void main(String args[]){ String s1 = "I am "; String s2 = "Indian"; String s3 = s2 + s1; System.out.println(s3); } }
Output
Indian I am
Advertisements