Monday, 30 January 2017

What is String Concatenation In Java ?

String concatenation means joining two or more strings. For example full name is a combination of first name and last name. Java provides multiple ways to concatenate string 
  1. + operator
  2. concat() method
  3. StringBuffer class
  4. StringBuilder class
+ operator:
This is the the most easy way to concatenate string. For example suppose we have two string i.e. "java" and "identifiers", the concatenation of "java"+"identifiers" will produce the output "javaidentifiers".

concat() method:
This is a string class method which is used to concat two or more strings. For example suppose we have two strings i.e. "java" and "identifiers" ,we can concate these strings as "java".concat("identifiers"); and this will produce the output "javaidentifiers".

StringBuffer class:
This class belongs to java.lang; package and this class provide append() method to concate strings. If we want to concate strings using this class then we have to create object of this class and then using append() method we can concate string as shown below.
StringBuffer sb = new StringBuffer();
sb.append("java").append("identifiers");
this will produce the output "javaidentifiers".

StringBuilder class:
This class is same as StringBuffer class click to know differences between StringBuffer and StringBuilder. This class also belongs to java.lang; package and this class also provide append() method to concate strings. If we want to concate strings using this class then we have to create object of this class and then using append() method we can concate string as shown below.
StringBuilder sb = new StringBuilder();
sb.append("java").append("identifiers");
this will produce the output "javaidentifiers".

Example of String Concatenation:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class StringConcatenation {

 public static void main(String[] args) {
  String s1 = "Java ";
  String s2 = "Identifier";
  
  //String Concatenation using + operator
  System.out.println(s1+s2);
  
  //String Concatenation using concat() method
  System.out.println(s1.concat(s2));
  
  //String Concatenation using StringBuffer Class
  StringBuffer sb = new StringBuffer();
  sb.append(s1).append(s2);
  System.out.println(sb);
  
  //String Concatenation using StringBuilder Class
  StringBuilder sb1 = new StringBuilder();
  sb1.append(s1).append(s2);
  System.out.println(sb1);
 }
}

Output:

Java Identifier
Java Identifier
Java Identifier
Java Identifier

      
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



No comments:

Post a Comment