1System.out.println("hey".equals("hey")); //prints true
2
3/*
4 always use .equals() instead of ==,
5 because == does the compare the string content but
6 loosely where the string is stored in.
7*/
1******Java String compareTo()******
2
3 The Java String class compareTo() method compares the given
4 string with the current string lexicographically.
5 It returns a positive number, negative number, or 0.
6 ___________________________________________
7 if s1 > s2, it returns positive number
8 if s1 < s2, it returns negative number
9 if s1 == s2, it returns 0
10 ___________________________________________
11
12
1if (aName.equals(anotherName))
2 {
3 System.out.println(aName + " equals " + anotherName);
4 }
5 else
6 {
7 System.out.println(aName + " does not equal " +anotherName );
8
9 }
1// These two have the same value
2new String("test").equals("test") // --> true
3
4// ... but they are not the same object
5new String("test") == "test" // --> false
6
7// ... neither are these
8new String("test") == new String("test") // --> false
9
1class scratch{
2 public static void main(String[] args) {
3 String str1 = "Nyello";
4 String str2 = "Hello";
5 String str3 = "Hello";
6
7 System.out.println( str1.equals(str2) ); //prints false
8 System.out.println( str2.equals(str3) ); //prints true
9 }
10}
1// compare two strings in java .equals() method
2public class EqualOperatorDemo
3{
4 public static void main(String[] args)
5 {
6 String str1 = new String("helloworld");
7 String str2 = new String("helloworld");
8 System.out.println(str1 == str2);
9 System.out.println(str1.equals(str2));
10 }
11}