简体   繁体   English

包装类引用和用户在Java中定义类引用之间的区别

[英]Difference between wrapper class reference and user defines class reference in java

Suppose I have a custom class, say Test. 假设我有一个自定义类,例如Test。

Test test = new Test(); // test is the reference.

Now when I print the value of test, it returns the hashcode . 现在,当我打印test的值时,它将返回hashcode。

Now consider, 现在考虑,

Integer i = new Integer(10);

When I print the value of i , it returns 10. 当我打印i的值时,它返回10。

Can someone help me to understand what exactly is the difference here? 有人可以帮助我了解这里的区别是什么吗? I believe both are object references, but for wrapper class reference, it returns the value of the object it is pointing to. 我相信两者都是对象引用,但是对于包装类引用,它返回所指向的对象的值。

When you create a new class, it inherits the method toString() from Object . 创建新类时,它将从Object继承方法toString() Integer class overrides that method to return the inner value. Integer类重写该方法以返回内部值。

When printing, there is an implicit call to toString() method. 打印时,将隐式调用toString()方法。

By default (in for your Test class) it uses the one inside Object class. 默认情况下(在Test类中),它使用一个Object类内部。 For Integer, it convert the Integer to a String in 10-base. 对于Integer,它将Integer转换为以10为基数的字符串。

Your Test class is using Object class's toString() method which prints hashCode. 您的Test类使用Object类的toString()方法,该方法打印hashCode。 But for Integer class, toString method is overrided. 但是对于Integer类,将重写toString方法。 You can see Integer.java here 您可以在这里看到Integer.java

user defined reference is an object,if you print that object means you may get some hash code because every class extends Object class,so your also have the property (method) tostring(). 用户定义的引用是一个对象,如果您打印该对象,则意味着您可能会得到一些哈希码,因为每个类都扩展了Object类,因此您还具有tostring()属性(方法)。

Wrapper class wraps its respective primitive data type Integer i = new Integer(10); 包装器类包装各自的原始数据类型Integer i = new Integer(10); and i=10; 和i = 10; both same in value. 两者的价值相同。

When you call System.out.println(Object) (or, more generally, PrintStream.println(Object) ): 当您调用System.out.println(Object) (或更PrintStream.println(Object) )时:

This method calls at first String.valueOf(x) 此方法首先调用String.valueOf(x)

String.valueOf(Object) returns: String.valueOf(Object)返回:

if the argument is null, then a string equal to "null"; 如果参数为null,则字符串等于“ null”; otherwise, the value of obj.toString() is returned. 否则,返回obj.toString()的值。

Neither of your objects are null , so the toString() method of the instances is called. 您的两个对象都不为null ,因此将调用实例的toString()方法。

In the case of Integer : 对于Integer

The value is converted to signed decimal representation and returned as a string 该值将转换为带符号的十进制表示形式并作为字符串返回

In the case of Test , unless you've explicitly overridden it (or a superclass has overridden it), you will call Object.toString() : 对于Test ,除非您已显式重写它(或超类已重写它),否则将调用Object.toString()

[T]his method returns a string equal to the value of: [T]他的方法返回的字符串等于:

 getClass().getName() + '@' + Integer.toHexString(hashCode()) 

If this isn't the desired behaviour, override toString() in Test : 如果这不是期望的行为,请在Test重写toString()

class Test {
  @Override public String toString() {
    // ... Your implementation.
  }
}

Whenever you print the object Java will invoke the toString() method. 每当您打印对象时,Java都会调用toString()方法。 The default implementation of toString() available in Object Class. 对象类中可用的toString()的默认实现。 Object is the base class for the all the Object in java. Object是Java中所有Object的基类。

 public String toString() {
        return getClass().getName() + "@" + Integer.toHexString(hashCode());
    }

It will print the class Name with full package path @ and HashCode of the object. 它将打印带有完整包路径@和对象的HashCode的类Name。

The test class doesn't override the toString() method. 测试类不会覆盖toString()方法。 But all the wrapper class in java override the toString() .so when You invoke the Integer method it invoke the toString() implemented in Integer class. 但是,java中的所有包装器类都会覆盖toString()当您调用Integer方法时,它将调用在Integer类中实现的toString()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM