BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java String equals() and equalsIgnoreCase() Methods example

Last Updated: September 11, 2022 by Chaitanya Singh | Filed Under: java

In this tutorial we will discuss equals() and equalsIgnoreCase() methods. Both of these methods are used for comparing two strings. The only difference between them is that the equals() methods considers the case while equalsIgnoreCase() methods ignores the case during comparison. For e.g. The equals() method would return false if we compare the strings “TEXT” and “text” however equalsIgnoreCase() would return true.

boolean equals(String str): Case sensitive comparison
boolean equalsIgnoreCase(String str): Case in-sensitive comparison

Java String equals() method example

In this example we will see how equals() method works in different scenarios. We can compare two String instances (str1, str2, str3) using equals() method like we did in the following example or we can also compare string instances with the hardcoded strings passed as an argument to the equals() method as shown in the example below.

As you can observe in the output that when we compared the String str1 (value “Hello”) with the string “hello”, the equals() method returned false because this method case sensitive and considers the case while comparing strings. On the other hand the equalsIgnoreCase() method compares strings while ignoring their cases, which we will see in the next section.

public class JavaExample{
   public static void main(String args[]){
	String str1= new String("Hello");
	String str2= new String("Hi");
	String str3= new String("Hello");
	System.out.println("str1 equals to str2:"+str1.equals(str2));
	System.out.println("str1 equals to str3:"+str1.equals(str3));
	System.out.println("str1 equals to Welcome:"+str1.equals("Welcome"));
	System.out.println("str1 equals to Hello:"+str1.equals("Hello"));
	System.out.println("str1 equals to hello:"+str1.equals("hello"));
   }
}

Output:
Java equals method example

Java String equalsIgnoreCase() method example

The method equalsIgnoreCase() ignores the case while comparing two strings. In the following example we compared the string “Apple” with the string “APPLE” and it returned true.

public class JavaExample{
   public static void main(String args[]){
	String str1= new String("Apple");
	String str2= new String("MANGO");
	String str3= new String("APPLE");
	System.out.println("str1 equals to str2:"+str1.equalsIgnoreCase(str2));
	System.out.println("str1 equals to str3:"+str1.equalsIgnoreCase(str3));
	System.out.println("str1 equals to Welcome:"+str1.equalsIgnoreCase("Welcome"));
	System.out.println("str1 equals to Apple:"+str1.equalsIgnoreCase("Apple"));
	System.out.println("str2 equals to mango:"+str2.equalsIgnoreCase("mango"));
   }
}

Output:
Java equalsIgnoreCase() method example

❮ PreviousNext ❯

Top Related Articles:

  1. Java String charAt() Method example
  2. Java OffsetDateTime Class explained with examples
  3. Java String indexOf() Method
  4. Java String format() method
  5. Java String concat() Method example

Tags: Java-Strings

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap