为什么 Integer 的两个引用都等于某个点?
Why are two references of Integer equal to a certain point?
为什么 Integer 对象的行为方式与 String 对象的行为方式不同?
我读到原因是性能,但不明白它如何才能表现得更好?
例如看下面的代码:
public class MyClass{
public static void main(String[] args){
String one = "myString";
String two = "myString";
System.out.println(one == two); // true
String oneLong = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus in leo at massa vehicula rhoncus quis eu mauris. Pellentesque non nulla convallis, tempus augue sed, sollicitudin risus. Aenean posuere nulla ipsum, at faucibus massa dignissim non. Duis felis felis, iaculis eu posuere id, elementum id nulla. Fusce tristique arcu vitae consectetur vehicula. Mauris tincidunt nunc placerat tortor rhoncus, eget venenatis felis dapibus. Sed scelerisque ligula congue ligula elementum hendrerit. Proin et mauris vestibulum, rutrum ante ut, sollicitudin massa. Fusce tempus mattis eleifend. Phasellus ut ante turpis. Suspendisse eu leo nec elit ornare rhoncus sed nec ex. In at tellus mi.";
String twoLong = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus in leo at massa vehicula rhoncus quis eu mauris. Pellentesque non nulla convallis, tempus augue sed, sollicitudin risus. Aenean posuere nulla ipsum, at faucibus massa dignissim non. Duis felis felis, iaculis eu posuere id, elementum id nulla. Fusce tristique arcu vitae consectetur vehicula. Mauris tincidunt nunc placerat tortor rhoncus, eget venenatis felis dapibus. Sed scelerisque ligula congue ligula elementum hendrerit. Proin et mauris vestibulum, rutrum ante ut, sollicitudin massa. Fusce tempus mattis eleifend. Phasellus ut ante turpis. Suspendisse eu leo nec elit ornare rhoncus sed nec ex. In at tellus mi.";
System.out.println(oneLong == twoLong); // true
Integer first = 1;
Integer second = 1;
System.out.println(first == second); // true
Integer third = 500;
Integer fourth = 500;
System.out.println(third == fourth); // false
}
}
以下是我发现的问题,但没有得到答复:
Why Integer In Java is Immutable
Is Integer Immutable?
通常保持对象尽可能不可变是个好主意,因为在多线程环境中可以毫无问题地共享不可变对象。
创建相同 Integer
的 n 个副本是可能的,但副本是不可变的。请注意,对于 String 也可以创建同一 String 的 n 个不同副本。为此,您需要明确使用 new
关键字。
如果与运算符== returns true 进行比较,则两个对象相同。两个对象内容相同如果比较方法等于returns true.
obj1 == obj2 // if true obj1 and obj2 are the same
obj1.equals(obj2) // if true obj1 and obj2 have the same content
// (can be the same or not)
在您的示例中,您有两个 Integer 具有相同的内容(因此等于 returns true)但内存位置不同(因此 == returns false)
为什么 Integer 对象的行为方式与 String 对象的行为方式不同?
我读到原因是性能,但不明白它如何才能表现得更好?
例如看下面的代码:
public class MyClass{
public static void main(String[] args){
String one = "myString";
String two = "myString";
System.out.println(one == two); // true
String oneLong = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus in leo at massa vehicula rhoncus quis eu mauris. Pellentesque non nulla convallis, tempus augue sed, sollicitudin risus. Aenean posuere nulla ipsum, at faucibus massa dignissim non. Duis felis felis, iaculis eu posuere id, elementum id nulla. Fusce tristique arcu vitae consectetur vehicula. Mauris tincidunt nunc placerat tortor rhoncus, eget venenatis felis dapibus. Sed scelerisque ligula congue ligula elementum hendrerit. Proin et mauris vestibulum, rutrum ante ut, sollicitudin massa. Fusce tempus mattis eleifend. Phasellus ut ante turpis. Suspendisse eu leo nec elit ornare rhoncus sed nec ex. In at tellus mi.";
String twoLong = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus in leo at massa vehicula rhoncus quis eu mauris. Pellentesque non nulla convallis, tempus augue sed, sollicitudin risus. Aenean posuere nulla ipsum, at faucibus massa dignissim non. Duis felis felis, iaculis eu posuere id, elementum id nulla. Fusce tristique arcu vitae consectetur vehicula. Mauris tincidunt nunc placerat tortor rhoncus, eget venenatis felis dapibus. Sed scelerisque ligula congue ligula elementum hendrerit. Proin et mauris vestibulum, rutrum ante ut, sollicitudin massa. Fusce tempus mattis eleifend. Phasellus ut ante turpis. Suspendisse eu leo nec elit ornare rhoncus sed nec ex. In at tellus mi.";
System.out.println(oneLong == twoLong); // true
Integer first = 1;
Integer second = 1;
System.out.println(first == second); // true
Integer third = 500;
Integer fourth = 500;
System.out.println(third == fourth); // false
}
}
以下是我发现的问题,但没有得到答复:
Why Integer In Java is Immutable
Is Integer Immutable?
通常保持对象尽可能不可变是个好主意,因为在多线程环境中可以毫无问题地共享不可变对象。
创建相同 Integer
的 n 个副本是可能的,但副本是不可变的。请注意,对于 String 也可以创建同一 String 的 n 个不同副本。为此,您需要明确使用 new
关键字。
如果与运算符== returns true 进行比较,则两个对象相同。两个对象内容相同如果比较方法等于returns true.
obj1 == obj2 // if true obj1 and obj2 are the same
obj1.equals(obj2) // if true obj1 and obj2 have the same content
// (can be the same or not)
在您的示例中,您有两个 Integer 具有相同的内容(因此等于 returns true)但内存位置不同(因此 == returns false)