Java初学者计划帮助(如果,否则问题)

And*_*ian 2 java if-statement

我正在练习制作自己的String逆转工具.我在一个简单的应用程序中使用该方法的学习过程.但是..
当我在Eclipse中运行我的代码时,即使word = = racecar并且反转变为= racecar(单词反转).显示了其他的东西..告诉我这个词和反向永远不会相等......但是......那个案子是什么?对?请给我一些建议,这真是太无聊了.谢谢.

import java.util.Scanner;

public class Main {

    static Scanner sc = new Scanner(System.in);

    public static void main(String[] args){

    System.out.println("Please enter a word, I'll show it reversed ");
    System.out.println("and tell you if it is read the same forward and backward(A Palindrome)!: ");
        String word = sc.nextLine();
        String reverse = reverse(word);

        if (reverse == word){
            System.out.println("Your word: " + word + " backwards is " + reverse + ".");
            System.out.println("Your word is read the same forward and backward!");
            System.out.println("Its a Palindrome!");
        }

        else {
            System.out.println("Your word: " + word + " backwards is " + reverse + ".");
            System.out.println("Your word is not read the same forward and backward!");
            System.out.println("Its not a Palindrome!");
        }



    }

    public static String reverse(String source){

        if (source == null || source.isEmpty()){
            return source;
        }

        String reverse = "";

        for(int i = source.length() -1; i >= 0; i--){
            reverse = reverse + source.charAt(i);
        }

        return reverse;
    }

}
Run Code Online (Sandbox Code Playgroud)

Mar*_*k M 10

始终使用String#equals来比较字符串.==将比较引用是否相等,由于它们的存储方式而不能真正用于比较相等的字符串.

if (reverse.equals(word))
Run Code Online (Sandbox Code Playgroud)