Home / Programs / Write a Java program to compare a given string to the specified character sequence.
🚀 Programming Example

Write a Java program to compare a given string to the specified character sequence.

👁 2,777 Views
💻 Practical Program
📘 Step Learning
Compare a given string to the specified character sequence.

💻 Program Code

public class Exercise {
public static void main(String[] args) {
    String str1 = "example.com", str2 = "Example.com";
    CharSequence cs = "example.com";
     System.out.println("Comparing "+str1+" and "+cs+": " + str1.contentEquals(cs));
      System.out.println("Comparing "+str2+" and "+cs+": " + str2.contentEquals(cs));
          }
}

                        

🖥 Program Output

Comparing example.com and example.com: true
Comparing Example.com and example.com: false
Press any key to continue . . .
                            

📘 Explanation

None
📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.