Which of these methods of class string is used to compare two string objects?

String is a sequence of characters. In Java, objects of String are immutable which means they are constant and cannot be changed once created.

Below are 5 ways to compare two Strings in Java:

  1. Using user-defined function : Define a function to compare values with following conditions :
    1. if (string1 > string2) it returns a positive value.
    2. if both the strings are equal lexicographically
      i.e.(string1 == string2) it returns 0.
    3. if (string1 < string2) it returns a negative value.

    The value is calculated as (int)str1.charAt(i) – (int)str2.charAt(i)

    Examples:

    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: -9
    
    Input 1: Geeks
    Input 2: Geeks
    Output: 0
    
    Input 1: GeeksforGeeks
    Input 2: Geeks
    Output: 8
    

    Program:




    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    5

    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    6

    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    7
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    8
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    9

    str1.equals(str2);

    str1.equals(str2);
    1
    str1.equals(str2);
    2

    str1.equals(str2);
    1
    str1.equals(str2);
    4

    str1.equals(str2);
    1
    str1.equals(str2);
    6

    str1.equals(str2);
    1
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    7
    str1.equals(str2);
    9
    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    0
    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    1

    str1.equals(str2);
    1
    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    3

    str1.equals(str2);

    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    5
    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    0
    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    7

    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    5
    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    0
    Comparing Geeksforgeeks and Practice : false
    Comparing Geeks and Geeks : true
    Comparing Geeks and geeks : false
    Comparing Geeksforgeeks and Geeks : false
    
    0

    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    5
    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    0
    Comparing Geeksforgeeks and Practice : false
    Comparing Geeks and Geeks : true
    Comparing Geeks and geeks : false
    Comparing Geeksforgeeks and Geeks : false
    
    3

    str1.equals(str2);

    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    5
    Comparing Geeksforgeeks and Practice : false
    Comparing Geeks and Geeks : true
    Comparing Geeks and geeks : false
    Comparing Geeksforgeeks and Geeks : false
    
    6
    Comparing Geeksforgeeks and Practice : false
    Comparing Geeks and Geeks : true
    Comparing Geeks and geeks : false
    Comparing Geeksforgeeks and Geeks : false
    
    7
    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    0
    Comparing Geeksforgeeks and Practice : false
    Comparing Geeks and Geeks : true
    Comparing Geeks and geeks : false
    Comparing Geeksforgeeks and Geeks : false
    
    9
    str2.equalsIgnoreCase(str1);
    0
    str2.equalsIgnoreCase(str1);
    1

    str2.equalsIgnoreCase(str1);
    2
    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    0
    str2.equalsIgnoreCase(str1);
    4
    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    0
    str2.equalsIgnoreCase(str1);
    6

    str2.equalsIgnoreCase(str1);
    2
    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    0
    str2.equalsIgnoreCase(str1);
    9
    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    0
    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: true
    
    1

    str1.equals(str2);

    str2.equalsIgnoreCase(str1);
    2
    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: true
    
    4
    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: true
    
    5

    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: true
    
    6
    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: true
    
    7
    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: true
    
    8

    str2.equalsIgnoreCase(str1);
    2
    Comparing Geeksforgeeks and Practice : false
    Comparing Geeks and Geeks : true
    Comparing Geeks and geeks : true
    Comparing Geeksforgeeks and Geeks : false
    
    0

    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    5
    Comparing Geeksforgeeks and Practice : false
    Comparing Geeks and Geeks : true
    Comparing Geeks and geeks : true
    Comparing Geeksforgeeks and Geeks : false
    
    0

    str1.equals(str2);

    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    5
    Comparing Geeksforgeeks and Practice : false
    Comparing Geeks and Geeks : true
    Comparing Geeks and geeks : true
    Comparing Geeksforgeeks and Geeks : false
    
    5

    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    5
    Comparing Geeksforgeeks and Practice : false
    Comparing Geeks and Geeks : true
    Comparing Geeks and geeks : true
    Comparing Geeksforgeeks and Geeks : false
    
    7

    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    5
    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: true
    
    4
    public static boolean equals(Object a, Object b)
    0

    str2.equalsIgnoreCase(str1);
    2
    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: true
    
    7
    public static boolean equals(Object a, Object b)
    3

    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    5
    Comparing Geeksforgeeks and Practice : false
    Comparing Geeks and Geeks : true
    Comparing Geeks and geeks : true
    Comparing Geeksforgeeks and Geeks : false
    
    0

    str1.equals(str2);

    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    5
    public static boolean equals(Object a, Object b)
    8

    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    5
    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: null
    Input 2: null
    Output: true
    
    0

    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    5
    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: null
    Input 2: null
    Output: true
    
    2
    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    3

    str2.equalsIgnoreCase(str1);
    2
    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: true
    
    7
    str2.equalsIgnoreCase(str1);
    0
    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: null
    Input 2: null
    Output: true
    
    7

    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    5
    Comparing Geeksforgeeks and Practice : false
    Comparing Geeks and Geeks : true
    Comparing Geeks and geeks : true
    Comparing Geeksforgeeks and Geeks : false
    
    0

    str1.equals(str2);
    1
    Comparing Geeksforgeeks and Practice : false
    Comparing Geeks and Geeks : true
    Comparing Geeks and geeks : true
    Comparing Geeksforgeeks and Geeks : false
    
    0

    str1.equals(str2);

    str1.equals(str2);
    1
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    04

    str1.equals(str2);
    1
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    7
    str1.equals(str2);
    9
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    08
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    09

    str1.equals(str2);
    1
    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    3

    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    5
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    13
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    14
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    15
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    16
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    17

    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    5
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    19
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    14
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    15
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    22
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    17

    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    5
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    25
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    14
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    15
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    28
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    17

    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    5
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    31
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    14
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    15
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    28
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    17

    str1.equals(str2);

    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    5
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    38

    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    5
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    40
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    41
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    42
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    43
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    44

    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    45
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    46
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    47
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    48

    str1.equals(str2);

    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    5
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    51

    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    5
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    40
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    41
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    55
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    43
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    57

    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    45
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    46
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    47
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    61

    str1.equals(str2);

    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    5
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    64

    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    5
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    40
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    41
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    42
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    43
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    57

    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    45
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    46
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    47
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    74

    str1.equals(str2);
    1
    Comparing Geeksforgeeks and Practice : false
    Comparing Geeks and Geeks : true
    Comparing Geeks and geeks : true
    Comparing Geeksforgeeks and Geeks : false
    
    0

    Comparing Geeksforgeeks and Practice : false
    Comparing Geeks and Geeks : true
    Comparing Geeks and geeks : true
    Comparing Geeksforgeeks and Geeks : false
    
    0

    Output:

    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    

  2. Using String.equals() :In Java, string equals() method compares the two given strings based on the data/content of the string. If all the contents of both the strings are same then it returns true. If any character does not match, then it returns false.

    Syntax:

    str1.equals(str2);

    Here str1 and str2 both are the strings which are to be compared.

    Examples:

    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    

    Program:




    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    5

    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    6

    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    7
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    8
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    9

    str1.equals(str2);
    1
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    7
    str1.equals(str2);
    9
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    08
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    09

    str1.equals(str2);
    1
    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    3

    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    5
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    13
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    14
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    15
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    16
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    17

    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    5
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    19
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    14
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    15
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    22
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    17

    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    5
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    25
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    14
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    15
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    28
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    17

    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    5
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    31
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    14
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    15
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    28
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    17

    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    5
    str1.equals(str2);
    15
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    14
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    15
    str1.equals(str2);
    18
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    17

    str1.equals(str2);

    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    5
    str1.equals(str2);
    22

    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    5
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    40
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    41
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    42
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    43
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    44

    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    45
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    46
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    47
    str1.equals(str2);
    32

    str1.equals(str2);

    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    5
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    51

    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    5
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    40
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    41
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    55
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    43
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    57

    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    45
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    46
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    47
    str1.equals(str2);
    45

    str1.equals(str2);

    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    5
    str1.equals(str2);
    48

    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    5
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    40
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    41
    str1.equals(str2);
    52
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    43
    str1.equals(str2);
    54

    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    45
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    46
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    47
    str1.equals(str2);
    58

    str1.equals(str2);

    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    5
    str1.equals(str2);
    61

    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    5
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    40
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    41
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    42
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    43
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    57

    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    45
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    46
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    47
    str1.equals(str2);
    71

    str1.equals(str2);
    1
    Comparing Geeksforgeeks and Practice : false
    Comparing Geeks and Geeks : true
    Comparing Geeks and geeks : true
    Comparing Geeksforgeeks and Geeks : false
    
    0

    Comparing Geeksforgeeks and Practice : false
    Comparing Geeks and Geeks : true
    Comparing Geeks and geeks : true
    Comparing Geeksforgeeks and Geeks : false
    
    0

    Output:

    Comparing Geeksforgeeks and Practice : false
    Comparing Geeks and Geeks : true
    Comparing Geeks and geeks : false
    Comparing Geeksforgeeks and Geeks : false
    

  3. Using String.equalsIgnoreCase() : The String.equalsIgnoreCase() method compares two strings irrespective of the case (lower or upper) of the string. This method returns true if the argument is not null and the contents of both the Strings are same ignoring case, else false.

    Syntax:

    str2.equalsIgnoreCase(str1);

    Here str1 and str2 both are the strings which are to be compared.

    Examples:

    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: true
    

    Program:




    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    5

    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    6

    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    7
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    8
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    9

    str1.equals(str2);
    1
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    7
    str1.equals(str2);
    9
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    08
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    09

    str1.equals(str2);
    1
    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    3

    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    5
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    13
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    14
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    15
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    16
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    17

    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    5
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    19
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    14
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    15
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    22
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    17

    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    5
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    25
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    14
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    15
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    28
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    17

    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    5
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    31
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    14
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    15
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    28
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    17

    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    5
    str1.equals(str2);
    15
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    14
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    15
    str1.equals(str2);
    18
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    17

    str1.equals(str2);

    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    5
    str1.equals(str2);
    22

    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    5
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    40
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    41
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    42
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    43
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    44

    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    45
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    46
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    47
    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    29

    str1.equals(str2);

    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    5
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    51

    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    5
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    40
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    41
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    55
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    43
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    57

    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    45
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    46
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    47
    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    42

    str1.equals(str2);

    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    5
    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    45

    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    5
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    40
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    41
    str1.equals(str2);
    52
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    43
    str1.equals(str2);
    54

    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    45
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    46
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    47
    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    55

    str1.equals(str2);

    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    5
    str1.equals(str2);
    61

    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    5
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    40
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    41
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    42
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    43
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    57

    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    45
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    46
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    47
    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    68

    str1.equals(str2);
    1
    Comparing Geeksforgeeks and Practice : false
    Comparing Geeks and Geeks : true
    Comparing Geeks and geeks : true
    Comparing Geeksforgeeks and Geeks : false
    
    0

    Comparing Geeksforgeeks and Practice : false
    Comparing Geeks and Geeks : true
    Comparing Geeks and geeks : true
    Comparing Geeksforgeeks and Geeks : false
    
    0

    Output:

    Comparing Geeksforgeeks and Practice : false
    Comparing Geeks and Geeks : true
    Comparing Geeks and geeks : true
    Comparing Geeksforgeeks and Geeks : false
    

  4. Using Objects.equals() : Object.equals(Object a, Object b) method returns true if the arguments are equal to each other and false otherwise. Consequently, if both arguments are null, true is returned and if exactly one argument is null, false is returned. Otherwise, equality is determined by using the equals() method of the first argument.

    Syntax:

    public static boolean equals(Object a, Object b)

    Here a and b both are the string objects which are to be compared.

    Examples:

    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: null
    Input 2: null
    Output: true
    

    Program:




    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    5

    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    6

    str1.equals(str2);

    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    75
    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    76

    str1.equals(str2);

    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    7
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    8
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    9

    str1.equals(str2);
    1
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    7
    str1.equals(str2);
    9
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    08
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    09

    str1.equals(str2);
    1
    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    3

    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    5
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    13
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    14
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    15
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    16
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    17

    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    5
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    19
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    14
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    15
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    28
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    17

    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    5
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    25
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    14
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    15
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    28
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    17

    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    5
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    31
    Comparing Geeksforgeeks and Practice : false
    Comparing Geeks and Geeks : true
    Comparing Geeks and geeks : false
    Comparing Geeksforgeeks and Geeks : false
    
    08
    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: null
    Input 2: null
    Output: true
    
    7

    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    5
    str1.equals(str2);
    15
    Comparing Geeksforgeeks and Practice : false
    Comparing Geeks and Geeks : true
    Comparing Geeks and geeks : false
    Comparing Geeksforgeeks and Geeks : false
    
    08
    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: null
    Input 2: null
    Output: true
    
    7

    str1.equals(str2);

    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    5
    str1.equals(str2);
    22

    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    5
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    40
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    41
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    42
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    43
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    44

    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    45
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    46
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    47
    Comparing Geeksforgeeks and Practice : false
    Comparing Geeks and Geeks : true
    Comparing Geeks and geeks : false
    Comparing Geeksforgeeks and Geeks : false
    
    26

    str1.equals(str2);

    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    5
    Comparing Geeksforgeeks and Practice : false
    Comparing Geeks and Geeks : true
    Comparing Geeks and geeks : false
    Comparing Geeksforgeeks and Geeks : false
    
    29

    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    5
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    40
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    41
    Comparing Geeksforgeeks and Practice : false
    Comparing Geeks and Geeks : true
    Comparing Geeks and geeks : false
    Comparing Geeksforgeeks and Geeks : false
    
    33
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    43
    Comparing Geeksforgeeks and Practice : false
    Comparing Geeks and Geeks : true
    Comparing Geeks and geeks : false
    Comparing Geeksforgeeks and Geeks : false
    
    35

    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    45
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    46
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    47
    Comparing Geeksforgeeks and Practice : false
    Comparing Geeks and Geeks : true
    Comparing Geeks and geeks : false
    Comparing Geeksforgeeks and Geeks : false
    
    39

    str1.equals(str2);

    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    5
    str1.equals(str2);
    61

    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    5
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    40
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    41
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    42
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    43
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    57

    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    45
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    46
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    47
    Comparing Geeksforgeeks and Practice : false
    Comparing Geeks and Geeks : true
    Comparing Geeks and geeks : false
    Comparing Geeksforgeeks and Geeks : false
    
    52

    str1.equals(str2);

    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    5
    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    45

    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    5
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    40
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    41
    str1.equals(str2);
    52
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    43
    str1.equals(str2);
    54

    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    45
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    46
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    47
    Comparing Geeksforgeeks and Practice : false
    Comparing Geeks and Geeks : true
    Comparing Geeks and geeks : false
    Comparing Geeksforgeeks and Geeks : false
    
    65

    str1.equals(str2);
    1
    Comparing Geeksforgeeks and Practice : false
    Comparing Geeks and Geeks : true
    Comparing Geeks and geeks : true
    Comparing Geeksforgeeks and Geeks : false
    
    0

    Comparing Geeksforgeeks and Practice : false
    Comparing Geeks and Geeks : true
    Comparing Geeks and geeks : true
    Comparing Geeksforgeeks and Geeks : false
    
    0

    Output:

    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    0

  5. Using String.compareTo() :

    Syntax:

    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    1

    Working:
    It compares and returns the following values as follows:

    1. if (string1 > string2) it returns a positive value.
    2. if both the strings are equal lexicographically
      i.e.(string1 == string2) it returns 0.
    3. if (string1 < string2) it returns a negative value.

    Examples:

    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: -9
    
    Input 1: Geeks
    Input 2: Geeks
    Output: 0
    
    Input 1: GeeksforGeeks
    Input 2: Geeks
    Output: 8
    

    Program:




    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    5

    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    6

    str1.equals(str2);

    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    75
    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    76

    str1.equals(str2);

    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    7
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    8
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    9

    str1.equals(str2);
    1
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    7
    str1.equals(str2);
    9
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    08
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    09

    str1.equals(str2);
    1
    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    3

    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    5
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    13
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    14
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    15
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    16
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    17

    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    5
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    19
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    14
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    15
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    22
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    17

    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    5
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    25
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    14
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    15
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    28
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    17

    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    5
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    31
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    14
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    15
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    28
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    17

    str1.equals(str2);

    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    5
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    38

    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    5
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    40
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    41
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    42
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    43
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    44

    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    45
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    46
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    47
    str2.equalsIgnoreCase(str1);
    21

    str1.equals(str2);

    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    5
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    51

    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    5
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    40
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    41
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    55
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    43
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    57

    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    45
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    46
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    47
    str2.equalsIgnoreCase(str1);
    34

    str1.equals(str2);

    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    5
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    64

    Input 1: GeeksforGeeks
    Input 2: Practice
    Output: false
    
    Input 1: Geeks
    Input 2: Geeks
    Output: true
    
    Input 1: geeks
    Input 2: Geeks
    Output: false
    
    5
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    40
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    41
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    42
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    43
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    57

    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    45
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    46
    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    
    47
    str2.equalsIgnoreCase(str1);
    47

    str1.equals(str2);
    1
    Comparing Geeksforgeeks and Practice : false
    Comparing Geeks and Geeks : true
    Comparing Geeks and geeks : true
    Comparing Geeksforgeeks and Geeks : false
    
    0

    Comparing Geeksforgeeks and Practice : false
    Comparing Geeks and Geeks : true
    Comparing Geeks and geeks : true
    Comparing Geeksforgeeks and Geeks : false
    
    0

    Output:

    Comparing Geeksforgeeks and Practice : -9
    Comparing Geeks and Geeks : 0
    Comparing Geeksforgeeks and Geeks : 8
    

Why not to use == for comparison of Strings?

In general both equals() and “==” operator in Java are used to compare objects to check equality but here are some of the differences between the two:

  • Main difference between .equals() method and == operator is that one is method and other is operator.
  • One can use == operators for reference comparison (address comparison) and .equals() method for content comparison.
  • In simple words, == checks if both objects point to the same memory location whereas .equals() evaluates to the comparison of values in the objects.

    Which method of class string is used to compare two strings?

    Using String. equals() :In Java, string equals() method compares the two given strings based on the data/content of the string. If all the contents of both the strings are same then it returns true.

    Which of these method of class String is used to compare two string objects for their equality * equals () equals () Isequal () Isequal ()?

    Explanation: equals() method of string class returns boolean value true if both the string are equal and false if they are unequal.

    Which method is used to compare two strings objects for their equality?

    The String. Equals method can easily determine if two strings are the same. This case-sensitive method returns a true or false Boolean value.

    Which of the following is used to compare two strings?

    Comparing Two Strings Using strcmp() Function in C++ strcmp() is a C library function that compares two strings lexicographically.