In Java, two strings can be compared using several methods:
1. equals() Method: Compares the content of two strings. It returns true if both strings have the same characters in the same order.
string1.equals(string2);
2. compareTo() Method: Compares two strings lexicographically. It returns a negative integer, zero, or a positive integer if the first string is less than, equal to, or greater than the second string, respectively.
string1.compareTo(string2);
3. == Operator: Checks if two string references point to the same object, not their content.
string1 == string2;
The equals() method is preferred for content comparison, while == is used for reference comparison. Use compareTo() for sorting or lexicographical comparison.