Write a java program to compare two strings lexicographically.

twitter logo github logo ・1 min read

Write a java program to compare two strings lexicographically.

Using the compareTo method for comparing the String .
compareTo method is compare the string is based on the unicode value of character .
It return the positive negative and zero value.
if the string are same it return the zero value otherwise it positive or negative value depend on ASCII value of the character .
for example
1st string is s1 = middle
2nd string is s2 = niddle
so it return the value is -1.
bcoz the value of m is 77 and value of n is 78 (ASCII)
so 77 -78 = -1
so the return value is -1 .

twitter logo DISCUSS
Classic DEV Post from Feb 5

If/else or just if?

It's a simple question that cuts deep.

Vikas Tomar profile image
try to make the better career

Customize DEV by logging in

  • Follow users
  • Follow tags
  • Dark mode
  • Font style (like sans serif)
  • Notifications
Get Started