Which Operator Can Be Used in String Concatenation?
Strings are defined every bit an array of characters. The departure between a grapheme array and a string is the string is terminated with a special character '\0'. Since arrays are immutable(cannot grow), Strings are immutable as well. Whenever a change to a String is made, an entirely new Cord is created. Concatenation is the procedure of joining end-to-end.
concat() Method
The Java Cord concat() method of String class has been itself present inside java.util package concatenates ane string to the end of some other string. This method returns a string with the value of the cord passed into the method, appended to the cease of the string.
Illustration:
Input: String ane : abc Cord two : def String northward-i : ... String n : xyz Output: abcdef...xyz
Example:
Java
class
GFG {
public
static
void
main(String args[])
{
String s =
"Geeks "
;
s = s.concat(
"for Geeks"
);
Organisation.out.println(due south);
}
}
Now let usa exercise dwell on the next concept where nosotros will exist discussing
'+' Operator
+ operator is used for concatenating strings on either side and is used merely probable we are up to adding two numbers, henceforth providing united states the flexibility to add together on either side.
Instance:
Java
class
GFG {
public
static
void
primary(Cord args[])
{
Cord s1 =
" Geeks "
;
String s2 =
" for Geeks "
;
Cord s3 = s1 + s2;
String s4 = s2 + s1;
System.out.println(s3);
System.out.println(s4);
}
}
Output
Geeks for Geeks for Geeks Geeks
Although concat() method of Strings class and + operator are both used for the concatenation of strings, at that place are some differences between them which are depicted beneath in the tabular format as follows:
Factor 1: Number of arguments the concat() method and + operator takes
- concat() method takes but one argument of string and concatenates information technology with other string.
- + operator takes any number of arguments and concatenates all the strings.
Example
Java
public
course
GFG {
public
static
void
main(String[] args)
{
String southward =
"Geeks"
, t =
"for"
, g =
"geeks"
;
System.out.println(s + t + g);
Organisation.out.println(due south.concat(t));
}
}
Output
Geeksforgeeks Geeksfor
Gene two: Type of arguments
- string concat() method takes only string arguments, if there is any other blazon is given in arguments then it volition raise an mistake.
- + operator takes any type and converts to cord type and and so concatenates the strings.
Factor 3: concat() method raises java.lang.NullPointer Exception
- concat() method throws NullPointer Exception when a string is concatenated with null
- + operator did not enhance whatsoever Exception when the cord is concatenated with cypher.
Example
Java
public
class
GFG {
public
static
void
main(Cord[] args)
{
String south =
"Geeks"
;
String r =
zip
;
System.out.println(s + r);
Arrangement.out.println(s.concat(r));
}
}
Output:
Cistron 4: Creates a new Cord object
- concat() method takes concatenates ii strings and returns a new string object just string length is greater than 0, otherwise, it returns the same object.
- + operator creates a new String object every fourth dimension irrespective of the length of the cord.
Example
Java
public
course
GFG {
public
static
void
main(Cord[] args)
{
String s =
"Geeks"
, g =
""
;
String f = due south.concat(thou);
if
(f == s)
System.out.println(
"Both are same"
);
else
System.out.println(
"non same"
);
String e = s + g;
if
(e == southward)
System.out.println(
"Both are same"
);
else
System.out.println(
"not same"
);
}
}
Output
Both are aforementioned not same
Factor 5: Operation
concat() method is better than the + operator considering information technology creates a new object only when the cord length is greater than zilch(0) but the + operator ever creates a new string irrespective of the length of the string.
Conclusion: From above two programs we describe meet that both are somehow adding up ii strings straight or indirectly. In concat method we are spring to while adding strings as we have to laissez passer the string in the parameter while in other case of '+' operator, it is but calculation up strings likely mathematics, and then there is no bound nosotros can add either side.
Below is the tabular array to describe major differences between both the methods:
Basics | concat() Method | + Operator |
---|---|---|
Number of arguments the concat() method and + operator takes | Takes just one statement of string and concatenates it with another string. | Takes any number of arguments and concatenates all the strings |
Type of Arguments | Takes but string arguments, if there is whatever other blazon is given in arguments then it will raise an fault. | Takes any type and converts to string blazon and and so concatenates the strings. |
NullPointer Exception | Throws NullPointer Exception when the cord is concatenated with null | It does non raise whatever Exception when the cord is concatenated with null |
Creating a new Cord object | Takes concatenates two strings and returns a new cord object merely if the length of the returned string is greater than the length of one of the concatenated strings, otherwise, it returns the aforementioned object. | Creates a new string object every time irrespective of the length of the cord. |
Operation | concat() method is better than the '+' operator considering it creates a new object merely when the string length is greater than zero(0), so it uses less amount of memory. | + operator always creates a new string irrespective of the length of cord therefore it takes more retentivity. |
Source: https://www.geeksforgeeks.org/difference-between-concat-and-operator-in-java/
0 Response to "Which Operator Can Be Used in String Concatenation?"
Post a Comment