为什么我的输出在java的大多数程序中都是假的? [英] Why is my output false in most programs in java?

查看:63
本文介绍了为什么我的输出在java的大多数程序中都是假的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<代码开始> public class Welcome {
public static void main(String args []){
Hello h1 = new Hello(Hello);
Hello h2 = new Hello(Hello);
System.out.println(h1.equals(Hello));
}
}
class Hello {
String name;
Hello(String name){
this.name = name;
}
public boolean equals(Hello h){
return this.name == h.name;
}
}< / code ends>
输出:false

我为我的错误道歉。在查看了一些成员的评论后,我发现我输错了代码。对不起。我纠正了他们。

Hi.'Hello'是一个类,h1和h2是类'Hello'的引用变量。我通过使用HelloString参数调用'Hello'类的构造函数,该参数调用相应的构造函数。我有两个问题。

1> 'this'关键字,它是如何知道它指的是被比较的对象h1或h2。是因为我使用了h1.equals吗?关于this关键字如何工作的一点解释会有所帮助。

2>即使我在equals中传递字符串Hello并进行比较,我也将输出视为false。这怎么可能?

接下来,我想稍微修改上面的程序:

< code starts> public class Welcome {
public static void main(String args []){
String s1 = new String(Hello);
String s2 = new String(Hello);
Hello h1 = new Hello(s1);
Hello h2 = new Hello(s2);
System.out.println(h1.equals(h2));
}
}
class Hello {
String name;
Hello(String name){
this.name = name;
}
public boolean equals(Hello h){
return this.name == h.name;
}
}< / code ends>
输出:false

这里,它又是如何错误的?原因与上述程序相同吗?

当我用.equals而不是==替换重写'equals'方法的程序2时输出如何变化:

<代码开始> public boolean equals(Hello h){
return this.name.equals(h.name);
}< / code ends>
输出:true

如果我在程序2中传递Object类引用有什么问题:

< code starts> public boolean equals(Object h){
return this.name.equals。(h.name);
}< / code ends>
输出:编译错误





我尝试过:



我试过执行上面的代码。

解决方案

这个指的是当前班级的当前实例,与您在英语中使用它时的方式相同:这辆汽车指的是您是司机或乘客的汽车,无论它是哪辆车。



因此,当您在类方法中使用 this 时,它允许您指定您使用的属性或方法是从当前的实例。大多数情况下,您不需要指定它,因为它是由上下文隐含的 - 当您拥有一个由同名的局部变量隐藏的类变量或属性时,它才真正需要。与Hello构造函数中的情况一样,因为构造函数参数name的调用与类级变量name相同,您需要澄清目标:

 this.name = name; 

未指定 .name,您将从参数复制到参数。



另一个问题更简单,更复杂。

==比较引用相等,而不是值相等这就是你得到结果的原因。那是什么意思?这里解释得很好:如何比较Java中的字符串? - 堆栈溢出 [ ^ ]


<code begins>   public class Welcome{
                public static void main(String args[]){
                Hello h1=new Hello("Hello");
                Hello h2=new Hello("Hello");
                System.out.println(h1.equals("Hello"));
                }
                }
                class Hello{
                String name;
                Hello(String name){
                this.name=name;
                }
                public boolean equals(Hello h){
                return this.name==h.name;
                }
                } </code ends>
Output: false 

I apologize for my mistakes. After looking at comments from some members, I found out that I typed the code wrong. I am sorry. I have corrected them.

Hi.'Hello' is a class and h1 and h2 are reference variables of class 'Hello'. I call the constructor of 'Hello' class by using "Hello" String arguments, which invokes the respective constructor. I have two questions.

1> 'this' keyword, how does it know that it is referring to object h1 or h2 that is compared. Is it because I used h1.equals? A little explanation of how 'this' keyword works would be helpful.

2> Even though I pass a String "Hello" in equals and compare, I get the output as false. How is this possible?

Next, I would like to modify the above program a little bit as:

    <code begins> public class Welcome{
                  public static void main(String args[]){
                  String s1=new String("Hello");
                  String s2=new String("Hello");
                  Hello h1=new Hello(s1);
                  Hello h2=new Hello(s2);
                  System.out.println(h1.equals(h2));
                  }
                  }
                  class Hello{
                  String name;
                  Hello(String name){
                  this.name=name;
                  }
                  public boolean equals(Hello h){
                  return this.name==h.name;
                  }
                  } </code ends>
Output: false

Here, how is it false again? Is the reason same as for the above program?

How is output changing when I replace program 2 with the overridden 'equals' method with .equals instead of == as:

<code begins>    public boolean equals(Hello h){
                 return this.name.equals(h.name);
                 } </code ends>
Output: true

What is wrong if I pass Object class reference as this in Program 2:

<code begins>    public boolean equals(Object h){
                 return this.name.equals.(h.name);
                 } </code ends>
Output: Compilation Error



What I have tried:

I have tried executing the above codes.

解决方案

this refers to the current instance of the current class, in the same way as it does when you use it in English: "this car" refers to the car you are the driver or a passenger in, regardless of which actual vehicle it is.

So when you use this inside a class method, it allows you to specify that the property or method you are using is from the current instance. Most of the time you don't need to specify it, as it is implied by the context - it's only really needed when you have a class variable or property which is "hidden" by a local variable of the same name. As is the case inside your Hello constructor, because the constructor parameter "name" is called the same as the class level variable "name" and you need to clarify the destination:

this.name=name;

Without specifying this.name, you are copying from the parameter to the parameter.

The other question is simpler, and more complicated.
== compares for reference equality, not value equality, which is why you get the results you do. What does that mean? It's explained well here: How do I compare strings in Java? - Stack Overflow[^]


这篇关于为什么我的输出在java的大多数程序中都是假的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆