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

By: Fazal Emailed: 1641 times Printed: 2108 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)
InetAddress Example program in Java
XML and Java - Parsing XML using Java Tutorial
Read from a COM port using Java program
How to use Iterator in Java
wait(), notify() and notifyAll() in Java - A tutorial
Recursion in java
Student marks calculation program in Java
TCP Server and TCP Client in Java
Count number of vowels, consonants and digits in a String in Java
Vector example in Java
Using StringTokenizer in Java
Integer: byte, short, int, and long data types in Java
FileReader and FileWriter example program in Java
Stack example in Java - push(), pop(), empty(), search()
Most Emailed Articles (in Java)
What is Multithreaded programming?
TCP Server and TCP Client in Java
setPriority() and getPriority() in Java
Using One-Dimensional Arrays in Java
Read from a COM port using Java program
Type Casting in Java
Simple Port Scanner application using Java
constants and variables
Update contents of a file within a jar file
HTTPS Java - URL Connection program. Using HttpsURLConnection
Major components of FxObjects in JavaFX
Java program for Associate keys with values
How to use set, get basic and nested properties for Spring framework
How to reverse the elements in a Single dimension array and display the output
Of Object, equals (), == and hashCode ()