compareTo() Method with empty string in java

Java Programming Language Number Class in java (Article) Number Class in java (Program)

2219

Program:

 public class CompareToMethodExample {
	public static void main(String args[]){

	String s1="world";
	String s2="";
	String s3="my";
	System.out.println(s1.compareTo(s2));
	System.out.println(s2.compareTo(s3));
	}
}

/*
If you compare string with blank or empty string, it returns
length of the string. If second string is empty, result would
be positive. If first string is empty, result would be negative.*/

Output:

5
-2
Press any key to continue . . .

This Particular section is dedicated to Programs only. If you want learn more about Java Programming Language. Then you can visit below links to get more depth on this subject.