我有一个String[]像这样的值:
public static final String[] VALUES = new String[] {"AB","BC","CD","AE"};
Run Code Online (Sandbox Code Playgroud)
鉴于String s,有没有一种好的方法来测试是否VALUES包含s?
我今天换了讲师,他说我用了一个奇怪的代码.(他说最好使用.equals,当我问为什么时,他回答"因为它是!")
所以这是一个例子:
if (o1.equals(o2))
{
System.out.println("Both integer objects are the same");
}
Run Code Online (Sandbox Code Playgroud)
而不是我习惯的:
if (o1 == o2)
{
System.out.println("Both integer objects are the same");
}
Run Code Online (Sandbox Code Playgroud)
这两者之间有什么区别.为什么他的方式(使用.equals)更好?
通过快速搜索找到了这个,但我无法理解这个答案: