Apr 18, 2012

String concatenation in System.out.println(),Concatenate Strings

String concatenation in System.out.println()


The + operator is overloaded for performing the concatenation operation in Java. When used in System.out.println() then it automatically invokes the toString method and then uses the + operator to concatenate the strings being used with + operator.


package com.example;

/** This class is a Java tutorial for concatenation of Strings in Java
*
* @author Extreme Java
*/
public class Test{

/** This method shows how to concatenate strings in Java
*
* @param args
*/
public static void main(String[] args) {
System.out.println("abc" + " def");

System.out.println(1 + "abc");

System.out.println(1 + 2 + "abc");

System.out.println("abc" + 1 + 2);
}
}


output:
abc def
1abc
3abc
abc12

The output of the above program shows how the addition and concatenation operations are overloaded and how they are infrenced by JDK when used in conjunction in a single expression.

1 comment:

  1. StringBuffer is good in terms of performance. + operative an additional string and keep in memory.

    ReplyDelete