我知道Java枚举被编译为具有私有构造函数和一堆公共静态成员的类.当比较给定枚举的两个成员时,我总是使用.equals(),例如
public useEnums(SomeEnum a)
{
if(a.equals(SomeEnum.SOME_ENUM_VALUE))
{
...
}
...
}
Run Code Online (Sandbox Code Playgroud)
但是,我刚刚遇到一些使用equals运算符==而不是.equals()的代码:
public useEnums2(SomeEnum a)
{
if(a == SomeEnum.SOME_ENUM_VALUE)
{
...
}
...
}
Run Code Online (Sandbox Code Playgroud)
我应该使用哪个运营商?
==到目前为止,我一直在我的程序中使用运算符来比较我的所有字符串.但是,我遇到了一个错误,将其中一个更改为了.equals(),并修复了该错误.
是==坏?什么时候应该不应该使用它?有什么不同?
什么是Java中的String Interning,何时应该使用它,为什么?
I have no idea why these lines of code return different values:
System.out.println(Integer.valueOf("127")==Integer.valueOf("127"));
System.out.println(Integer.valueOf("128")==Integer.valueOf("128"));
System.out.println(Integer.parseInt("128")==Integer.valueOf("128"));
Run Code Online (Sandbox Code Playgroud)
The output is:
true
false
true
Run Code Online (Sandbox Code Playgroud)
Why does the first one return true and the second one return false? Is there something different that I don't know between 127 and 128? (Of course I know that 127 < 128.)
Also, why does the third one return true?
I have read the answer of this question, but I still didn't get …
String Pool是什么意思?以下声明之间有什么区别:
String s = "hello";
String s = new String("hello");
Run Code Online (Sandbox Code Playgroud)
JVM存储这两个字符串有什么区别吗?
我今天换了讲师,他说我用了一个奇怪的代码.(他说最好使用.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)更好?
通过快速搜索找到了这个,但我无法理解这个答案:
我是Java的新手.现在我正在研究equals和==以及重新定义equals和toString.
我想使用我已经重新定义的toString方法和从Object类继承的默认方法.
我没有使用那个超级修饰符来达到那个方法.
这仅用于教育目的.如果您要查看我的代码中的注释,我希望得到的更清楚.
你能帮帮我吗?
我的代码是:
public class EqualTest{
public static void main(String[] args){
Employee alice1 = new Employee("Alice Adams", 75000, 1987, 12, 15);
//System.out.super.println(alice1);
Employee alice2 = alice1;
//System.out.super.println(alice2);
Employee alice3 = new Employee("Alice Adams", 75000, 1987, 12, 15);
//System.out.super.println(alice3);
System.out.println("alice1==alice2: " + (alice1==alice2));
System.out.println("alice1 == alice3: " + (alice1==alice3));
System.out.println("alice1.equals(alice3): " + alice1.equals(alice3));
}
}
class Employee{
...
public String toString(){
return getClass().getName() + "[name = " + name +
", salary=" + salary + ", hireDay=" + …Run Code Online (Sandbox Code Playgroud) 我从编译器得到的错误是"赋值的左侧必须是变量".我的用例是深度复制,但并不真正相关.
在C++中,可以分配给*this.
问题不在于如何规避任务this.这很简单,但是决定不做this变量的背后有什么理由.
原因是技术性的还是概念性的?
我的猜测到目前为止 - 在随机方法中重建Object的可能性容易出错(概念性),但技术上可行.
编辑请限制"因为Java规范如此说"的变化.我想知道决定的原因
请考虑以下Java代码:
Object a = new Integer(2);
Object b = new Integer(2);
System.out.println(a.equals(b));
Object x = new Object();
Object y = new Object();
System.out.println(x.equals(y));
Run Code Online (Sandbox Code Playgroud)
第一个打印语句打印true,第二个打印false.
如果这是故意的行为,这有助于如何用Java编程?
如果这不是故意行为,这是Java中的缺陷吗?