-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path079-String-Concatenation.java
More file actions
72 lines (56 loc) · 2.51 KB
/
Copy path079-String-Concatenation.java
File metadata and controls
72 lines (56 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
* ============================================================================
* Program Name : String Concatenation
* File Name : 079-String-Concatenation.java
* Class Name : StringConcatenation
*
* Description:
* This program accepts two Strings from the user and concatenates them
* using the concat() method and the '+' operator.
*
* Objective:
* - Understand how to concatenate Strings in Java.
* - Learn how to use the concat() method.
* - Learn how to use the '+' operator for String concatenation.
*
* Author : Shaik Mahaboob Basha
* Repository : 01-Core-Java
* Folder : 08-Java-Programs
* ============================================================================
*/
import java.util.Scanner;
public class StringConcatenation {
// The main() method is the entry point of every Java application.
public static void main(String[] args) {
// Create a Scanner object to read input from the keyboard.
Scanner scanner = new Scanner(System.in);
// Ask the user to enter the first string.
System.out.print("Enter the First String: ");
// Read the first string.
String firstString = scanner.nextLine();
// Ask the user to enter the second string.
System.out.print("Enter the Second String: ");
// Read the second string.
String secondString = scanner.nextLine();
// Concatenate the strings using the concat() method.
String concatenatedUsingConcat = firstString.concat(secondString);
// Concatenate the strings using the '+' operator.
String concatenatedUsingPlus = firstString + secondString;
// Concatenate the strings with a space using the '+' operator.
String concatenatedWithSpace = firstString + " " + secondString;
// Display the result using the concat() method.
System.out.println("Using concat() Method: " + concatenatedUsingConcat);
// Display the result using the '+' operator.
System.out.println("Using '+' Operator: " + concatenatedUsingPlus);
// Display the result with a space between the strings.
System.out.println("Using '+' Operator with Space: " + concatenatedWithSpace);
// Example Output:
// Enter the First String: Core
// Enter the Second String: Java
// Using concat() Method: CoreJava
// Using '+' Operator: CoreJava
// Using '+' Operator with Space: Core Java
// Close the Scanner object to release system resources.
scanner.close();
}
}