public class CompareStrings { public static void main(String[] args) { String style = "Bold"; String style2 = "Bold"; if(style == style2) System.out.println("Equal"); else System.out.println("Not Equal"); } } Output Show In the above program, we've two strings style and style2. We simply use the equal to operator (==) to compare the two strings, which compares the value Bold to Bold and prints Equal. Example 2: Compare two strings using equals()public class CompareStrings { public static void main(String[] args) { String style = new String("Bold"); String style2 = new String("Bold"); if(style.equals(style2)) System.out.println("Equal"); else System.out.println("Not Equal"); } }Output EqualIn the above program, we have two strings named style and style2 both containing the same world Bold. However, we've used String constructor to create the strings. To compare these strings in Java, we need to use the equals() method of the string. You should not use == (equality operator) to compare these strings because they compare the reference of the string, i.e. whether they are the same object or not. On the other hand, equals() method compares whether the value of the strings is equal, and not the object itself. If you instead change the program to use equality operator, you'll get Not Equal as shown in the program below. Example 3: Compare two string objects using == (Doesn't work)public class CompareStrings { public static void main(String[] args) { String style = new String("Bold"); String style2 = new String("Bold"); if(style == style2) System.out.println("Equal"); else System.out.println("Not Equal"); } }Output Not EqualExample 4: Different ways to compare two stringsHere is the string comparison which is possible in Java. public class CompareStrings { public static void main(String[] args) { String style = new String("Bold"); String style2 = new String("Bold"); boolean result = style.equals("Bold"); // true System.out.println(result); result = style2 == "Bold"; // false System.out.println(result); result = style == style2; // false System.out.println(result); result = "Bold" == "Bold"; // true System.out.println(result); } }Output true false false true
In Go language, the string is an immutable chain of arbitrary bytes encoded with UTF-8 encoding. You are allowed to compare strings with each other using two different ways: 1. Using comparison operators: Go strings support comparison operators, i.e, ==, !=, >=, <=, <, >. Here, the == and != operator are used to check if the given strings are equal or not, and >=, <=, <, > operators are used to find the lexical order. The results of these operators are of Boolean type, meaning if the condition is satisfied it will return true, otherwise, return false. Example 1:
Output: Result 1: false Result 2: false Result 3: false Result 4: true Result 5: true Result 6: true Result 7: true Result 8: false Example 2:
Output: Slice: [Geeks Geeks gfg GFG for] Result 1: false Result 2: true Result 3: false Result 4: true Result 5: true Result 6: true2. Using Compare() method: You can also compare two strings using the built-in function Compare() provided by the strings package. This function returns an integer value after comparing two strings lexicographically. The return values are:
Syntax: func Compare(str1, str2 string) intExample:
Output: 1 0 1 -1Article Tags :
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:
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:
In simple words, == checks if both objects point to the same memory location whereas .equals() evaluates to the comparison of values in the objects. Example:
Explanation: Here two String objects are being created namely s1 and s2. Article Tags : Practice Tags : |