Home »
Java Programs »
Java String Programs
Java program to concatenate two strings without using library function
In this java program, we are going to learn how to concatenate two strings without using library function?
Submitted by Preeti Jain, on March 13, 2018
Java - Concatenate two strings
There are two common ways to concatenate two strings in Java:
1. Concatenate two strings using library method
First way is to write a program with the help of library function (concat) in java, read here: Java program to concatenate two strings
1. Concatenate two strings without using library method
Second way is to write a program without the help of library function means we can make a program manually or our programming logic there is no need to use any readymade function.
Program to concatenate two strings without using library function in java
import java.util.Scanner;
class ConcatenateString{
public static void main(String[] args){
/* create Scanner class object */
Scanner sc = new Scanner(System.in);
/* Display message for user to take first
string input from keyboard */
System.out.println("Enter First String :");
String firstStr = sc.next();
/* Display message for user to take first
string input from keyboard */
System.out.println("Enter Second String :");
String secondStr = sc.next();
/* Display message for displaying result */
System.out.println("Result after concatenation:");
/* '+' operator concatenate string */
System.out.println(firstStr+ " " + secondStr);
}
}
Output
D:\Java Articles>java ConcatenateString
Enter First String :
preeti
Enter Second String :
jain
Result after concatenation:
preeti jain
Java String Programs »