有人可以向我解释一下Java中的.getClass()方法吗? [英] Could someone explain me the .getClass() method in java

查看:71
本文介绍了有人可以向我解释一下Java中的.getClass()方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在大学攻读Java课.这是我的第一门编程课,我偶然发现了一些我无法理解的东西.据我了解,有两种比较变量的方法.第一种是将==!=<>=<>=符号用于PRIMITIVE变量,例如int,double等.第二种方法是将.equals()方法用于引用类型.现在这是我的问题:

I am currently taking a java class in university. This is my first programming class and I've stumbled on something that I just cannot understand. As i learned, there are two ways of comparing variables. The first is using the ==, !=, <, > , =< , >= signs for PRIMITIVE variables such as int,double,etc. and the second way is to use the .equals() method for reference type. Now here is my question:

使用.getClass()方法时,可以将两个类与.equals()方法和==/!=方法进行比较.由于可以使用==/!=符号,因此我假设返回对象类的.getClass()方法必须返回原始类型.但是在Google上搜索时,我在Java API中发现的关于此方法的唯一一件事就是它返回了对象的类.它没有告诉我它返回的变量类型.这种方法到底是如何工作的.它返回什么?我试图问我的老师,但她不知道.谢谢!

When I use the .getClass() method, I can compare two classes with the .equals() method and the == / != method. Since I can use the ==/!= signs, I'd suppose that the .getClass() method which returns the class of an object must return a primitive type. But searching on google the only thing I found out about this method in the java API is that it returns the class of an object. It doesn't tell me the variable type it returns. How exactly does this method work. What does it return? I tried to ask my teacher but she didn't know. Thank you!

推荐答案

您首先需要知道==!=如何比较两个操作数.之所以不能使用==!=来比较引用类型,是因为它们实际上比较了两个引用类型变量的内存地址.

You first need to know how == and != compare the two operands. The reason why == and != cannot be used to compare reference types is that they actually compare the memory addresses of the two reference type variables.

所以,如果我有两个字符串:

So if I have two strings:

String x = "Hello";
String y = x;

由于第二行执行后xy共享相同的内存地址,所以x == y的计算结果为true.

Since x and y share the same memory address after the second line is executed, x == y evaluates to true.

getClass()方法也是如此. getClass()方法将对象的类作为Class<T>对象返回.问题是,为什么它的计算结果为true:

The same goes for the getClass() method. The getClass() method returns the class of the object as a Class<T> object. The question is, why this evaluates to true:

x.getClass() == y.getClass()

答案很简单.因为xy都是String类型.因此,调用getClass将返回相同的实例.这意味着返回的两个对象共享相同的内存地址.

The answer is simple. Because x and y are both of type String. So calling getClass will return the same instance. This means the two returned the objects share the same memory address.

但是当我用==运算符比较具有相同字符的字符串时,它的计算结果为false!"你大喊.

"But when I compare strings with the same characters with the == operator, it evaluates to false!" you shouted.

这是因为字符串位于不同的内存地址.但是,如果getClass返回的类表示的类相同,则它们始终位于相同的内存地址.这是由于ClassLoader的工作方式.但是我不是那个专家.

This is because the strings are located at different memory addresses. But the classes that getClass will return is always at the same memory address if the class that they represent is the same. This is due to the way ClassLoader works. But I'm not an expert that.

如果它们代表的类相同,则只需要知道getClass返回的对象位于相同的内存地址即可.

这篇关于有人可以向我解释一下Java中的.getClass()方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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