The program is designed to merge three strings ("Dog"
, "Cat"
, and "Mouse"
) into a single string, with each value separated by commas.
link:https://coderspacket.com/posts/?p=6577&preview=true
public class StringMerger { public static void main(String[] args) { String str1 = "Dog"; String str2 = "Cat"; String str3 = "Mouse"; String mergedString = str1 + "," + str2 + "," + str3; System.out.println("Merged string: " + mergedString); } }
Output:
Merged string: Dog,Cat,Mouse
- Class Definition (
StringMerger
):- We define a class called
StringMerger
. - This class contains a
main
method, which serves as the entry point for our program.
- We define a class called
- String Variables (
str1
,str2
,str3
):- We declare three string variables:
str1
,str2
, andstr3
. - Each variable holds a specific string value (“Dog,” “Cat,” and “Mouse”).
- We declare three string variables:
- Concatenation:
- Using the
+
operator, we concatenate these strings along with commas. - The result is stored in the
mergedString
variable.
- Using the
- Printing the Result:
- Finally, we print the merged string to the console.