Spaceship Operation in for Lexicographic Comparison I have written a program in Java 8 using BlueJ. There is just one function of type byte that accepts two String values and return 1 if the first word comes before the second in alphabetical order, and return -1 if the first word comes after the second in alphabetical order,  and 0 if both are equivalent strings. I would welcome any suggestions on how to make my code faster and more readable.

public class Test
{
    public static byte spaceshipAlphabetical (String str1, String str2)
    {
        int str1Length = str1.length();
        int str2Length = str2.length();
        int strLength;
        if 
        (str1Length &lt; str2Length)
        {
            str2 = str2.substring(0, str1Length);
            strLength = str1.length();
        }
        else
        {
            str1 = str1.substring(0, str2Length);
            strLength = str2.length();
        }
        str1 = str1.toUpperCase();
        str2 = str2.toUpperCase();
        char charStr1;
        char charStr2;
        int intChar1 = 0;
        int intChar2 = 0;
        for 
        (int i = 0; i &lt; strLength; i++)
        {
            charStr1 = str1.charAt(i);
            charStr2 = str2.charAt(i);
            intChar1 = (int) charStr1;
            intChar2 = (int) charStr2;
            if
            ((intChar1 &lt; intChar2) || (intChar2 &lt; intChar1) || ((intChar2 == intChar1) &amp;&amp; (i == (strLength - 1))))
            { 
                break;
            }
        }
        if
        ((intChar1 &lt; intChar2) || ((intChar2 == intChar1) &amp;&amp; (str1Length &lt; str2Length)))
        { 
            return 1;
        }
        else if
        ((intChar2 &lt; intChar1) || ((intChar2 == intChar1) &amp;&amp; (str2Length &lt; str1Length)))
        {
            return -1;
        }
        else 
        {
            return 0;
        }
    }
[1] By Java convention curly brackets belong in the same line as the preceding statement/expression.

Instead of this:

if
()
{}
else
{}

for
()
{}


do this:

if () {
    ...
} else {
    ...
}

for () {
    ...
}


I suggest you read the Google Java style guide for more tips: https://google.github.io/styleguide/javaguide.html

[2] You don't have to introduce an intermediate variable for holding chars, you can directly assign the result of charAt() method to an int variable: intChar1 = (int) str1.charAt(i);

[3] You don't have to do all those substring operations to make the strings the same length. Rewrite the main for loop to do as many iterations as there are characters in the shorter string. Written more succinctly: min(length(A), length(B) iterations

[4] To lexicographically compare two strings, you have to find the first different character in both strings and handle the remaining edge cases after the main for loop. Some pseudocode:

for i = 0; i &lt; (min(length(A), length(B)); i++
    if A[i] != B[i]
        if A[i] &lt; B[i] return -1
        else return 1

// If you didn't return from the method at this point, there are only three options left
String A is shorter than string B - return -1
String B is shorter than string A - return 1
String A and string B are equal - return 0