Concatenate Two Strings in Java Using Scanner

Concatenate two string in java. Concatenation of strings with a java program. As input, choose any two strings. We’ve created a function to which we input the two specified strings, and this function concatenates the strings using the concatenation operator, returning the desired result to the main function.

import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
String a, b, c;
Scanner s = new Scanner(System.in);
System.out.print("Enter first string");
a = s.nextLine();
System.out.print("Enter second string");
b = s.nextLine();
Main obj = new Main();
c = obj.concat(a, b);
System.out.println("New String"+c);
}
String concat(String x, String y)
{
String z;
z = x + " " + y;
return z;
}
}

 

Output:

Enter first string inlarn
Enter second string.com
New String inlarn .com

concat() Function to Concatenate two string in java

The concat() function in Java concatenates several strings. The requested string is appended to the end of the given string, and the combined string is returned. To connect several strings, we may use the concat() technique. The concat method creates a new array consisting of the elements in the object on which it is called, followed in order by the elements of that argument  or the argument itself for each argument . It does not recursively search over nested array parameters.

Object Function in Java

A Java object is a member (also referred to as an instance) of a Java class. Fields (variables) store an object’s state, while methods (functions) display the object’s behavior. At runtime, objects are formed from templates, which are also known as classes. The keyword “new” in Java is used to create an object.

nextLine in Java

This scanner is advanced past the current line by the nextLine() function, which returns the input that was skipped. This function returns the remainder of the current line, minus any line separators at the end.