How to use equals( ) and equalsIgnoreCase( ) in Java

By: Fazal Emailed: 1676 times Printed: 2157 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

To compare two strings for equality, use equals( ). It has this general form:

boolean equals(Object str)

Here, str is the String object being compared with the invoking String object. It returns true if the strings contain the same characters in the same order, and false otherwise.

The comparison is case-sensitive. To perform a comparison that ignores case differences, call equalsIgnoreCase( ). When it compares two strings, it considers A-Z to be the same as a-z. It has this general form:

boolean equalsIgnoreCase(String str)

Here, str is the String object being compared with the invoking String object. It, too, returns true if the strings contain the same characters in the same order, and false otherwise. Here is an example that demonstrates equals( ) and equalsIgnoreCase( ):

// Demonstrate equals() and equalsIgnoreCase().
class equalsDemo {
public static void main(String args[]) {
String s1 = "Hello";
String s2 = "Hello";
String s3 = "Good-bye";
String s4 = "HELLO";
System.out.println(s1 + " equals " + s2 + " -> " +
s1.equals(s2));
System.out.println(s1 + " equals " + s3 + " -> " +
s1.equals(s3));
System.out.println(s1 + " equals " + s4 + " -> " +
s1.equals(s4));
System.out.println(s1 + " equalsIgnoreCase " + s4 + " -> " +
s1.equalsIgnoreCase(s4));
}
}

The output from the program is shown here:
Hello equals Hello -> true
Hello equals Good-bye -> false
Hello equals HELLO -> false
Hello equalsIgnoreCase HELLO -> true


Java Home | All Java Tutorials | Latest Java Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(3)


1. View Comment

Its awesome

View Tutorial          By: Ranjitha at 2012-08-02 10:21:59
2. View Comment

nice article but can more example

View Tutorial          By: sandi at 2012-10-18 13:26:10
3. View Comment

Nice one , Compailed no Errors ... but when executing it doesnt work ... something wrong with main reply to me if u can

View Tutorial          By: Diamant at 2012-12-04 00:17:11

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Fazal
The Tag Life Cycle with Attributes in JSP
Using Shift Operators in C
IncludeAction in Struts
typedef example program in C
Arithmetic Evaluation Using the Expression Language in JSP
Tutorial on Complicated Declarations in C
Static Variables in C
getPosition() the right strategy in getting GPS
Precedence and Order of Evaluation in C
File Copying in C
JSF Life Cycle
Getting User Input Using cin in C++
UDP Datagram sample in J2ME
GUI components and menu based J2ME Applications.
A Serialization Example in Java

More Tutorials in Java
Update contents of a file within a jar file
Tomcat and httpd configured in port 8080 and 80
Java File
Java String
Count number of vowels, consonants and digits in a String in Java
Reverse a number in Java
Student marks calculation program in Java
Handling Fractions in Java
Calculate gross salary in Java
Calculate average sale of the week in Java
Vector in Java - Sample Program
MultiLevel Inheritance sample in Java
Multiple Inheritance sample in Java
Java program using Method Overriding
Java program to check if user input is an even number

More Latest News
Most Viewed Articles (in Java )
How to Send SMS using Java Program (full code sample included)
Stack example in Java - push(), pop(), empty(), search()
Method Overriding in Java
FileReader and FileWriter example program in Java
The Basic Structure of a Simple Java program
Read from a COM port using Java program
XML and Java - Parsing XML using Java Tutorial
How to use ArrayList in Java
Using StringTokenizer in Java
Recursion in java
Student marks calculation program in Java
How to use Iterator in Java
Abstract classes in Java
Method Overloading (function overloading) in Java
append() in Java
Most Emailed Articles (in Java)
Tomcat and httpd configured in port 8080 and 80
Update contents of a file within a jar file
Vector in Java - Sample Program
Get user input in Java
FilenameFilter - sample program in Java
How to Send SMS using Java Program (full code sample included)
Count number of vowels, consonants and digits in a String in Java
Java program using Method Overriding
Using StringTokenizer in Java
How to use ArrayList in Java
Using substring( ) in Java
Java File
MultiLevel Inheritance sample in Java
Read a file line by line in Java - Sample Program
The clone() Method in Java