- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
How to compare String equality in Java?
You can check the equality of two Strings in Java using the equals() method. This method compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.
Example
import java.lang.* public class StringDemo { public static void main(String[] args) { String str1 = "Tutorialspoint"; String str2 = "Tutorialspoint"; String str3 = "Hi"; // checking for equality boolean retval1 = str2.equals(str1); boolean retval2 = str2.equals(str3); // prints the return value System.out.println("str2 is equal to str1 = " + retval1); System.out.println("str2 is equal to str3 = " + retval2); } }
Output
str2 is equal to str1 = true str2 is equal to str3 = false
- Related Articles
- How to compare two ArrayList for equality in Java?
- Java Program to compare strings for equality
- How to compare two lists for equality in C#?
- How to compare two arrays for equality in Perl?
- How to Compare two String in Java?
- Compare array elements to equality - JavaScript
- How do we compare String in Java
- How to compare two dates in String format in Java?
- String compare by == operator in Java
- String compare by equals() method in Java
- String compare by compareTo() method in Java
- How to use compare string in android?
- Java Program to compare string using compareTo() method
- How to compare arrays in Java
- How to compare strings in Java?

Advertisements