Java Program to Concatenate Strings in Java

Write a program to Concatenate Two Strings in Java.

There are many ways to Concat Strings in java. We will see 2 approach to concatenate Strings in Java by using + operator and by using concat() method.

Approach 1: By using + operator.

You can concat two or more strings in Java using + Operator. Let's see below program and things will be more clear.
package javabypatel;

public class Test {  
 public static void main(String args[]){  
  String str = "Java" + "By" + "Patel";  
  System.out.println(str);  
 }  
} 

Output: JavaByPatel


Approach 2: By using String API's concat() method.

We can also using String class concat() method to concat 2 Strings in Java.

Let's see one example below on how to use concat() method in Java.

package javabypatel;

public class Test {  
 public static void main(String args[]){
  String str1 = "Java";
  String str2 = "By";
  String str3 = "Patel";
  
  String str1str2ConcatResult = str1.concat(str2);
  System.out.println(str1str2ConcatResult);
  
  String str1str2str3ConcatResult = str1str2ConcatResult.concat(str3);
  System.out.println(str1str2str3ConcatResult);
 }  
} 


Output: 
JavaBy 
JavaByPatel

You may also like to see


Exception Handling Interview Question-Answer

Method Overloading - Method Hiding Interview Question-Answer

Advanced Multithreading Interview Questions-Answers In Java

Type Casting Interview Questions-Answers In Java

How Thread.join() in Java works internally

How is ambiguous overloaded method call resolved in java

 

Enjoy !!!! 

If you find any issue in post or face any error while implementing, Please comment.

Post a Comment