The explanation is simple; Java wasn’t built to work with == as an option to compare strings. The developers of Java had the vision to keep things simple in programming thus did away with the == operator and in its place introduced three unique ways to test strings. Below are the 3 ways to compare your strings and not get any reference errors.
Using the ‘equals’ method
This is a frequently used, and preferred, option among experienced programmers. The equals method compares two strings and determines whether they contain similar characters. If all characters in the two strings are identical, then the operator gives a true or equal answer.
Here is a quick example of how two strings that don’t contain the same characters give a false result false
Now below is a second example on how two strings are compared to give an equal result
Thus the lines in the second example will print.
Comparing strings with the EqualsIgnoreCase method
his method is used in special cases where the programmer wants to compare strings that are case sensitive. The operator will ignore whether the strings have uppercase or lowercase characters and proceed to compare them as shown in the diagram below.
Comparing Strings with ‘CompareTo’ Operator
This is the third option for comparing strings. Though it is not a frequently used method, it gives more accurate results than using == operator. The CompareTo operator puts two strings side by side, and if they all have identical characters it gives a result of zero. Below is an example of this’
For more tutorils on WordPress, SEO, Making money online, Google toools etc, just have a look at out at our latest articles