Join super friendly code & developer communities
A place for coders and developers to share, learn and grow together.
This is a tutorial for using string and concatenation of strings in java. The program is given below that takes name and surname of the user as input and makes full name and prints hello message. The program is extendable. Go enjoy the program. Lets begin…
Program for string and concatenation of string in java.
//import Scanner as we require it.
import java.util.Scanner;
// the name of our class its public
public class Strings {
//void main
public static void main (String[] args)
{
//declare variables
String name,surname,fullname;
//print message
System.out.println("Enter your name:");
//Take input
Scanner input = new Scanner(System.in);
name = input.next();
System.out.println("Enter your surname:");
surname = input.next();
fullname = name;
fullname = fullname.concat(" "+surname);
//print the message
System.out.println("Hello ,"+fullname);
}
}
Output
Enter your name:
abc
Enter your surname:
xyz
Hello ,abc xyz
How it works
1. You enter your name and surname
2. Program does concatenation of your name and surname.
3. Program says hello to you.
Extending it
The program can be extended by using the concept in any program.
Remember this is a basic part of java which can be used in any program.
Explanation.
1. Import the Scanner.
2. Declare the class as public
3. Add the void main function.
4. Declare Strings.
5. Declare input as Scanner.
6. Add system.out.println() function with the message to enter necessary inputs .
7. Take the inputs and save it in variables.
8. Call Concat() function.
9. Add system.out.print() function to hello message.
At the end.
You learnt creating the Java program for Using Strings and concatenation of strings. So now enjoy the program.
P.S. If you have any questions, click here to join our community and feel free to ask any questions.