compareTo() function in Java
compareTo() function :- It is usaully used to compare strings.
Syntax : Str1.compareTo(Str2)
let be assume we have two String which is str1 & str2.
- if str1>str2 then compareTo function will give positive value .
- if str1 < str2 then compareTo function will give negative value .
- if str1 is equal to str2 then compareTo function will give zero value.
In this below code , we have seen that compareTo function give positive value 👇👇
In this below code , we have seen that compareTo function give negative value.👇👇
But here is one question are arised why we use this function we have already a equality function (==) . Its answer is simple equality function doesn’t work in many places let me so you some example here.
Let’s compare equality function & compareTo function here 👇👇
Atlast give your feedback below about this article .
Be Happy 😘😍 & Keep learning 🙂
Thank you..!