String Comparison Styles in Java

ZoneTechPro
By -
0


String Comparison Styles


String Comparison Styles
java - strings comparisons



Preface

In the former units, we've learned about working with strings, such as creating strings, comparing strings using the equivalency driver (==), etc.

In this unit, we will explore further ways to produce and compare strings in Java. To get started, let's learn how strings are actually created in Java, and also we will learn how to compare the strings using different string styles.


1. Creating Strings

In Java, strings can be created in two ways:

  • Using string literals
  • Using new keyword


Using String Literals

A string literal is a value enclosed in double quotations (""). In Java, a string literal is created using double quotations.


Illustration:

String str = "ABC";

Then, "ABC" is a string literal assigned to the variable str.

In Java, when strings are created using string literals with the same value, both the strings point to the same reference. As a result, memory can be saved during execution.


Using New Keyword

A string can be created by initializing the String class with the new keyword.

Syntax:

String str1 = new String("ABC");

Then, str is a string literal ("ABC").

When a string is created using the new keyword, the string is stored in a new memory location.


2. Comparing Strings


In Java, we can compare two strings in different ways. Some of them are listed below:

  • Using Equality (==) Operator
  • Using equals() Method
  • Using equalsIgnoreCase() Method
  • Using compareTo() Method
  • Using compareToIgnoreCase() Method

Using Equality (==) Operator

The == operator compares the strings based on their memory locations.


Illustration 1

Code:

class CompareStrings {
    public static void main(String[] args) {
        String str1 = "ABC";
        String str2 = "ABC";
        System.out.println(str1 == str2);
    }
}

Affair:

true

In the above example, str1 and str2 are created using a string literal, and they have the same reference. So, the output is true.


Illustration 2

Code:

class CompareStrings {
    public static void main(String[] args) {
        String str1 = new String("ABC");
        String str2 = new String("ABC");
        System.out.println(str1 == str2);
    }
}

Affair:

false

Here, str1 and str2 are created using the new keyword, meaning they point to different memory locations. So, == returns false even though they have the same value.

Thus, the == operator can only be used when comparing strings created using string literals, and it cannot be used for strings created with the new keyword.

To compare strings created with the new keyword, we use string comparison methods.


3. String Comparison Methods


equals() Method

The equals() method checks if a given string is equal to another string. It is case-sensitive.

Syntax:

str1.equals(str2);

Code:

class CompareStrings {
    public static void main(String[] args) {
        String str1 = "ABC";
        String str2 = "ABC";
        String str3 = new String("ABC");
        String str4 = new String("ABC");
        System.out.println(str1.equals(str2));
        System.out.println(str3.equals(str4));
        System.out.println(str1.equals(str3));
    }
}

Affair:

true  
true  
true

equalsIgnoreCase() Method

The equalsIgnoreCase() method works similarly to equals() but ignores case differences.

Syntax:

str1.equalsIgnoreCase(str2);

Code:

class CompareStrings {
    public static void main(String[] args) {
        String str1 = "ABC";
        String str2 = "abc";
        String str3 = new String("aBc");
        System.out.println(str1.equalsIgnoreCase(str2));
        System.out.println(str1.equalsIgnoreCase(str3));
    }
}

Affair:

true  
true

compareTo() Method

The compareTo() method compares two strings based on Unicode values.

Syntax:

str1.compareTo(str2);

Illustration 1

Code:

class CompareStrings {
    public static void main(String[] args) {
        String str1 = "ABC";
        String str2 = "ABC";
        System.out.println(str1.compareTo(str2));
    }
}

Affair:

0

Illustration 2

Code:

class CompareStrings {
    public static void main(String[] args) {
        String str1 = "A";
        String str2 = "B";
        System.out.println(str1.compareTo(str2));
    }
}

Affair:

-1

compareToIgnoreCase() Method


The compareToIgnoreCase() method works like compareTo(), but ignores case differences.

Syntax:

str1.compareToIgnoreCase(str2);

Code:

class CompareStrings {
    public static void main(String[] args) {
        String str1 = "AbC";
        String str2 = "aBc";
        System.out.println(str1.compareToIgnoreCase(str2));
    }
}

Affair:

0

4. Comparing Characters

We can compare characters using relational operators such as ==, >, and <.


Illustration 1

Code:

class CompareCharacters {
    public static void main(String[] args) {
        System.out.println('A' < 'B');
    }
}

Affair:

true

Illustration 2

Code:

class CompareCharacters {
    public static void main(String[] args) {
        System.out.println('P' >= 'Q');
        System.out.println('H' == 'H');
    }
}

Affair:

false  
true



Related Posts :

        
Java Introduction & Basics

         Sequence of Instructions in Java

         Java Data Types

         Java Input and Output Basics

         Java Type Conversions

         Java String Methods

         Java Logical Operators

         Conditional Statements in Java

         Java Ternary Operator

         Java Switch Statement

         Loops in Java

      Java String Method

          Chracters of Methods

Tags:

Post a Comment

0Comments

Post a Comment (0)

#buttons=(Ok, Go it!) #days=(20)

Our website uses cookies to enhance your experience. Learn more
Ok, Go it!