What is difference between Compare to and equals method in java?.

What is difference between Compare to and equals method in java?.

ComapareTo () method is provided by java.lang.string package that check the two different String to character by character if both the String are same than return 0 otherwise it return (-value) for each character.

Equals () Method is java. Object package Object Class Method that Check the two Different object and return true or false
 

Example:

public class comptest {

public static void main(String[] args) {

String s = “java”;

String s1 = new String (“java1″);

System.out.println(s.compareTo(s1));

System.out.println(s.equals(s1));

}}


output: -1
            false

Read More →