Merge 3 strings in Java

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
  1. Class Definition (StringMerger):
    • We define a class called StringMerger.
    • This class contains a main method, which serves as the entry point for our program.
  2. String Variables (str1str2str3):
    • We declare three string variables: str1str2, and str3.
    • Each variable holds a specific string value (“Dog,” “Cat,” and “Mouse”).
  3. Concatenation:
    • Using the + operator, we concatenate these strings along with commas.
    • The result is stored in the mergedString variable.
  4. Printing the Result:
    • Finally, we print the merged string to the console.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top