1. String Concatenation operator +


« String Concatenation                                                        2. Using concat() method of String class »
 
In Java + operator has been overloaded so that it can also concatenate strings in addition to the normal plus operation (on integers etc.). Strings can be concatenated by using + operator as:

Example1:

String s= “Charlie” +”Chaplin”;
System.out.println(s);   // will print Charlie Chaplin.

Example 2:

String s=”Ranga”;
String s1=”nathan”;

String s3=s+s1;
System.out.println(s3);  // will print Ranganathan.


Example3:

System.out.println(“abc”+”def”);  // will print abcdef.

System.out.println(s+s1);    // will print Ranganathan.

Example 4:

String s=null;
String s1=”hello”

System.out.println(s+s1);  // will print nullhello

Note the above example. Here s is a null reference variable pointing to null. Whenever you print a null reference variable, it converts to “null” (i.e a string having value null).

Therefore above statement prints nullhello.

Example 5:

Concatenating more than two strings at the same time:

String s=”Stop”+ “beating”+”around”+ “the”+ “bush.”;
System.out.println(s);     // will print Stop beating around the bush

String s= “Big”
Styring s1=”fish”
String s2=”in”
String s3=”a little sea”;
String s4= s+s1+s2+s3;
System.out.println(s4);  // will print Big fish in a little sea

Example 6:

You can also use compound additive operator (+=) to add Strings:


String s=”Old”;
s += ” man”;
System.out.println(s);  // will print Old man.

s += ” man” is equivalent to s = s+” man”.

Example 7:

String s=”12”;
String s1=”12”;

String s2=s+s1;

System.out.println(s2);  // will print 1212, NOT 24

Note that in above example 12 +12 has not get added to give 24 (normal plus operation) but instead it prints 1212. That’s why + is said to be overloaded for strings.

Similarly,

System.out.println(“24”+”3”);  //will print 243 Not 27.

Working of String concatenation operator:

Consider,

String s=”3”;
int i=10;

System.out.println( s+i );

What will be the output 13 or 310? Output is 310.

Why so? This is because if any one of the arguments of + is String then + behaves like string concatenation operator instead of doing mathematical addition. Therefore instead of adding 3 and 10 (i.e 3+10=13) it concatenates them (310).

Similarly,

String s=”3”;
int i=10;
int i=11;
System.out.println(s+i+i1); 

Above statement will NEITHER print 24 (i.e 3+10+11=24), NOR 321 (i.e “3” +10+11=321), it will print 31011.

String concatenation operator works from left to right. Here s+i (i.e “3”+10) is evaluated first giving “310”( a string). Then “310”+11 evaluated to give 31011(also a string.)

Now consider,

String s=”3”;
int i=10;
int i1=11;

System.out.println(s+(i+i1));

Above statement will 321. This is because you have enclosed i+i1 in (). So (i+i1) will be evaluated first. Since both operands are of int type, + will do mathematical addition instead of string concatenation.

After adding i and i1 it will then concatenate 3 (a string) and 21 (an int) as “3”+21. Now since one of the operands is string therefore + will do string concatenation and result will be 321.

Similarly,

String s=”3”;
int i=10;
int i1=11;

System.out.println(i+i1+s);

Will print 213.

In above example i+i1 evaluated first to give 21 because + operates from left to right. Here + has done numerical addition instead of string concatenation. This is because both arguments are of int type, neither of the arguments is of String type.

Therefore,
10+11=21   (integer addition)
21+”3”=213 (String concatenation)

More Examples:

Example 1:

String s=”3”;
int i=10;
int i1=11;

System.out.println(i+s+i1);

Will print 10311

Example 2:

System.out.println(2+someObj.getValue());

Here what will be the output depends on the return type of getValue(). If getValue() returns String, only then above statement will be a String concatenation.

Suppose getValue() returns a String “10”, then output will be 210.

If getValue() returns an int 10, then output will be 12 (i.e 2+10=12).


Example 3:

System.out.print("" +14 + 2 );

 will print 142 NOT  16  because first argument  “” is a string (an empty string). Therefore  it will be String concatenation instead of mathematical addition.

Example 4:

System.out.println("The square root of 3 is " + Math.sqrt(3));

Will print : The square root of 3 is 1.7320508075688772.

Here value returned by Math.sqrt(3) is concatenated with The square root of 3 is.

« String Concatenation                                                        2. Using concat() method of String class »



I would like to know your comments and if you liked the article then please share it on social networking buttons.


1 comment:

  1. Nice informative site.
    Please do visit : How System.out.println in java works.

    ReplyDelete