Palindrome checker program I've been studying Java for some days and I'm just now starting to make some simple programs, I'd like to know if there's any 'noob' mistakes or habits that I could avoid.

In other words: how could my code be more streamlined to Java?

My code:

import java.util.Scanner;

public class Main
{
    private static String reverse_string(String my_string)
    {
        String reversed_string = "";
        for(int j = my_string.length() - 1; j &gt;= 0; j--)
        {
            reversed_string = reversed_string + my_string.charAt(j);
        }
        return reversed_string;
    }

    public static void main(String[] args)
    {
        System.out.print("Insert a 'String': ");

        Scanner input = new Scanner(System.in);
        String user_string = input.nextLine().toLowerCase().replace(" ", "");

        if (user_string.equals(reverse_string(user_string)))
            {
                System.out.println("It is a palindrome.");
            } else
            {
                System.out.println("It is not a palindrome.");
            }
    }
}


I believe there are already other alternatives to my  reverse_string function, hence the 'reinventing the wheel' tag.
To answer your comment, yes there are equivalent of PEP8 in java. I suggest checkstyle, this plugin works in lots of IDE / text editors and can be configured with your code style.

Code review

reverse_string method


In java, we try to use the upper-case version of Snake case only on constants / enums. For the methods and variables, I suggest that you use the Camel case style.


Before

private static String reverse_string(String my_string) 
{
   //[..]
}


After

private static String reverseString(String myString) 
{
   //[..]
}



When concatenating String in a loop, it's generally recommended to use a java.lang.StringBuilder to gain performance and take fewer operations.


private static String reverseString(String myString) 
{
   StringBuilder reversedString = new StringBuilder();

   for (int j = myString.length() - 1; j &gt;= 0; j--) 
   {
      reversedString.append(myString.charAt(j));
   }

   return reversedString.toString();
}


If you want not to use it, then I suggest the += operator instead of the +; it will give the same result and make the code shorter.

Other observations

The code was missing a bit of formatting, but nothing too important, I sugest that you pick a formatter for your style (Horstmann style) depending on your IDE / text editor.

Refactored code

private static String reverseString(String myString)
{
   String reversedString = "";

   for (int j = myString.length() - 1; j &gt;= 0; j--) 
   {
      reversedString += myString.charAt(j);
   }

   return reversedString;
}

public static void main(String[] args) 
{
   System.out.print("Insert a 'String': ");

   Scanner input = new Scanner(System.in);
   String userString = input.nextLine().toLowerCase().replace(" ", "");

   if (userString.equals(reverseString(userString))) 
   {
      System.out.println("It is a palindrome.");
   } 
   else 
   {
      System.out.println("It is not a palindrome.");
   }
}