Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中,通过new operator可以创建具有相同字符串内容的不同对象吗?_Java - Fatal编程技术网

在Java中,通过new operator可以创建具有相同字符串内容的不同对象吗?

在Java中,通过new operator可以创建具有相同字符串内容的不同对象吗?,java,Java,在下面的代码中,通过hashCode()似乎创建了两个对象。那么,尽管s1==s3给出的是true,但为什么s1==s4给出的是false呢 公共班机{ public static void main(String[] args) { String s1 = new String("jordi") ; String s2 = s1.toUpperCase() ; String s3 = s1.toLowerCase() ; String s4 = new Stri

在下面的代码中,通过
hashCode()
似乎创建了两个对象。那么,尽管
s1==s3
给出的是true,但为什么
s1==s4
给出的是false呢

公共班机{

public static void main(String[] args) {
    String s1 = new String("jordi") ;
    String s2 = s1.toUpperCase() ;
    String s3 = s1.toLowerCase() ;
    String s4 = new String("jordi") ;
    System.out.println(s1.hashCode());
    System.out.println(s2.hashCode());
    System.out.println(s3.hashCode());
    System.out.println(s4.hashCode());
    System.out.println(s1==s2);
    System.out.println(s1==s3);
    System.out.println(s1==s4);
}
}

这将产生如下输出:

101312786

70775026

101312786

101312786

假的

真的

假的


这将执行引用比较。因此,您正在比较s1和s4是否都指向同一个对象(它们不指向同一个对象-因为您显式创建了两个不同的对象)

当我们使用==运算符进行s1和s4比较时,则为false,因为它们在内存中具有不同的地址。它比较内存地址

对于字符串,使用.equals方法而不是==方法。当您试图用==而不是.equals()比较两个字符串时,Java会变得有点不正常

这是您的代码,但已修复

public static void main(String[] args) {

        String s1 = new String("jordi");
        String s2 = s1.toUpperCase();
        String s3 = s1.toLowerCase();
        String s4 = new String("jordi");
        System.out.println(s1.hashCode());
        System.out.println(s2.hashCode());
        System.out.println(s3.hashCode());
        System.out.println(s4.hashCode());
        System.out.println(s1.equals(s2));
        System.out.println(s1.equals(s3));
        System.out.println(s1.equals(s4));
    }
值对象(其标识不重要且其值是唯一重要的对象,例如
String
Integer
)重写
equals
hashCode
,以便例如,它们可以用作
HashMap
中的键。
String
hashCode
值,以便具有相同内容的任意两个字符串将散列为相同的值


您的
s1
s3
是同一个对象,因为它返回
this
,并避免在整个字符串已经是小写的情况下创建新对象。

因为
new string
将始终创建不同的
string
对象
s1
s4
保证是唯一的对象。请不要依赖
hashCode
来告诉您对象标识。这不是它的目的,也不是它的作用。
s1
s4
的值是对具有相同内容的不同字符串对象的引用。您展示的代码中有四个对象:三个对象的内容为“jordi”,另一个对象的内容为“jordi”。“新建”操作符始终创建新对象。(有三个包含“jordi”的内容,因为字符串文字本身引用了一个-两次传递给
string
构造函数的引用。)感谢您的回复Jon Skeet先生。你完美地表达了我的观点。hashCode()不说明是否创建了新对象。每当我们使用新操作符时,总是会创建、理解新对象。但是,当s1==s3为真时,4个对象怎么会在这里呢?这两个引用变量不是都指向同一个对象吗?正如我所说,有三个对象的内容为“jordi”-其中一个没有分配给任何变量,但可以使用
String s0=“jordi”来分配。这种添加不会创建更多的对象,但是很容易说这四个对象可以被称为:{s0;s1和s3;s2;s4}“Java变得有点疯狂”-不,它不会,它的行为完全一致。当OP试图检测两个值是否引用同一对象,以及创建了多少对象时,
==
正是所需的。
public static void main(String[] args) {

        String s1 = new String("jordi");
        String s2 = s1.toUpperCase();
        String s3 = s1.toLowerCase();
        String s4 = new String("jordi");
        System.out.println(s1.hashCode());
        System.out.println(s2.hashCode());
        System.out.println(s3.hashCode());
        System.out.println(s4.hashCode());
        System.out.println(s1.equals(s2));
        System.out.println(s1.equals(s3));
        System.out.println(s1.equals(s4));
    }