- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to compare strings for equality
To compare string for equality in Java, use the equals() method. Let us see some examples wherein we have checked for same as well as different string values.
Example
public class Demo { public static void main(String[] args) { String one = "x"; String two = "x"; if(one.equals(two)) { System.out.println("String one is equal to two i.e. one==two"); }else{ System.out.println("String one is not equal to String two i.e. one!=two"); } } }
Output
String one is equal to two i.e. one==two
Let us see another example.
Example
public class Demo { public static void main(String[] args) { String one = "x"; String two = "y"; if(one.equals(two)) { System.out.println("String one is equal to two i.e. one==two"); }else{ System.out.println("String one is not equal to String two i.e. one!=two"); } } }
Output
String one is not equal to String two i.e. one!=two
- Related Articles
- Java Program to Compare Strings
- Java Program to check for equality between two strings ignoring the case
- How to compare two ArrayList for equality in Java?
- Java Program to Compare Two Strings
- Program to Compare two strings in Java
- Java Program to Compare two strings lexicographically
- How to compare String equality in Java?
- How to compare two lists for equality in C#?
- Compare array elements to equality - JavaScript
- How to compare strings in Java?
- Compare two Strings in Java
- How to Initialize and Compare Strings in Java?
- Compare two strings lexicographically in Java.
- Compare the two Strings lexicographically in Java
- How do I compare strings in Java?

Advertisements