-
/*Java String Concat Example.This Java String concat example shows how to concat String in Java.*/public class JavaStringConcat {public static void main(String args[]){/** String concatenation can be done in several ways in Java.*/
String str1 = “Hello”;String str2 = ” World”;
//1. Using + operatorString str3 = str1 + str2;System.out.println(“String concat using + operator : “ + str3);
/** Internally str1 + str 2 statement would be executed as,* new StringBuffer().append(str1).append(str2)** String concatenation using + operator is not recommended for large number* of concatenation as the performance is not good.*///2. Using String.concat() methodString str4 = str1.concat(str2);System.out.println(“String concat using String concat method : “ + str4);
//3. Using StringBuffer.append methodString str5 = new StringBuffer().append(str1).append(str2).toString();System.out.println(“String concat using StringBuffer append method : “ + str5);}
}
/*Output of Java String concat example would beString concat using + operator : Hello WorldString concat using String concat method : Hello WorldString concat using StringBuffer append method : Hello World*/
-
/*Java Char Array To String ExampleThis Java char array to String example shows how to convert char array toString in Java.*/public class CharArrayToStringExample{public static void main(String args[]){//char arraychar[] charArray = new char[]{‘J’,‘a’,‘v’,‘a’};/** To convert char array to String in Java, use* String(Char[] ch) constructor of Java String class.*/String str = new String(charArray);System.out.println(“Char array converted to String: “ + str);}}/*Output of above given char array to String example would beChar array converted to String: Java*/