English 中文(简体)
关于目标认同和目的平等及特殊地位的问题
原标题:Question about Object Identity and Object Equality and String class exception

This is a Java and C# question. We all know that, Object Identity(==) tests whether two objects refer to the same location and Obejct Equality(Equals method) tests whether two different (non identical)objects have the same value .But In case of string object Object Identity and Object Equality are same. For e.g Below two boolean expressions in if statements return true

string a="123";
string b="123";
if(a==b)
if(a.Equals(b))

Why is it so?? What is the rational behind this design decision?

最佳回答
问题回答

实际上,至少在贾瓦,有一个缝.机制。 陷阱是,在申请身份操作者时,两处平方有时会回来,但并不总是能够回去。 下面的密码是伪造的:

String a="123";
String b="12";
b=b+"3";
System.out.println(a==b);

如果你真的想要确定,a.equis(b)=真实,但(a=b)=虚假对两个Sting a和 b的错误评价,那么你可以使用完全低估的(^)建筑商:

String a = new String("abc");
String b = new String("abc");
if (a.equals(b)) {
   doTheyAreEqual();
   if (a != b) {
     doButNotTheSame();
   }
}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签