<div dir="ltr" style="text-align: left;" trbidi="on"><b>Description:</b></p>
<p> There are two variant of this method. First method compares this String to another Object and second method compares two strings lexicographically.<br />
<b>Syntax:</b></p>
<p> Here is the syntax of this method:</p>
<pre class="highlight" name="code">int compareTo(Object o)

or

int compareTo(String anotherString)
</pre>
<p>
<b>Parameters:</b></p>
<p>Here is the detail of parameters:</p>
<p></p>
<ul style="text-align: left;">
<li> o &#8212; the Object to be compared.</li>
<li> anotherString &#8212; the String to be compared.</li>
</ul>
<p>
 <b>Return Value :</b></p>
<p>The value 0 if the argument is a string lexicographically equal to this string; a value less than 0 if the argument is a string lexicographically greater than this string; and a value greater than 0 if the argument is a string lexicographically less than this string.</p>
<p><b>Example:</b></p>
<pre class="highlight" name="code">public class Test {

 public static void main(String args[]) {
 String str1 = "Strings are immutable";
 String str2 = "Strings are immutable";
 String str3 = "Integers are not immutable";

 int result = str1.compareTo( str2 );
 System.out.println(result);
 
 result = str2.compareTo( str3 );
 System.out.println(result);
 
 result = str3.compareTo( str1 );
 System.out.println(result);
 }
}
</pre>
<p><b><br />
</b> <b>This produces following result:</b></p>
<p></p>
<div style="background-color: #ff99cc;"><b>output:</b><br />
0<br />
10<br />
-10</div>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/2013/02/string-class-in-java.html">Previous</a> <;<; ; ; || <a href="https://dineshonjava.com/2013/01/core-java-baby-step-to-be-best-java-ian.html">Index </a>||  ; >;>;<a href="https://dineshonjava.com/2013/03/java-string-comparetoignorecase-method.html">Next</a> >;>;</b></div>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/java-string-charat-method/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/java-string-comparetoignorecase-method/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '552'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…