gpt4 book ai didi

java - 数据结构课上的测验

转载 作者:行者123 更新时间:2023-12-01 06:41:16 24 4
gpt4 key购买 nike

想要对问题 1 的结果进行解释。

***1。以下方法的输出是什么?

public static void main(String[] args) {
Integer i1=new Integer(1);
Integer i2=new Integer(1);
String s1=new String("Today");
String s2=new String("Today");

System.out.println(i1==i2);
System.out.println(s1==s2);
System.out.println(s1.equals(s2));
System.out.println(s1!=s2);
System.out.println( (s1!=s2) || s1.equals(s2));
System.out.println( (s1==s2) && s1.equals(s2));
System.out.println( ! (s1.equals(s2)));
}

答案:

false
false
true
true
true
false
false
<小时/>

最佳答案

Integer i1=new Integer(1);
Integer i2=new Integer(1);
String s1=new String("Today");
String s2=new String("Today");

// do i1 and 12 point at the same location in memory? No - they used "new"
System.out.println(i1==i2);

// do s1 and s2 point at the same location in memory? No - the used "new"
System.out.println(s1==s2);

// do s1 and s2 contain the same sequence of characters ("Today")? Yes.
System.out.println(s1.equals(s2));

// do s1 and s2 point at different locations in memory? Yes - they used "new"
System.out.println(s1!=s2);

// do s1 and s2 point to different locations in memory? Yes - they used "new".
// Do not check s1.equals(s2) because the first part of the || was true.
System.out.println( (s1!=s2) || s1.equals(s2));

// do s1 and s2 point at the same location in memory? No - they used "new".
// do not check s1.equals(s2) because the first part of the && was false.
System.out.println( (s1==s2) && s1.equals(s2));

// do s1 and s2 not contain the same sequence of characters ("Today")? No.
System.out.println( ! (s1.equals(s2)));

关于java - 数据结构课上的测验,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7458288/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com