What is the number it shows when I print ** this ** pointer in java?

This program

public class HelloWorld{ public void testFunc(){ System.out.println("Class = "+this); } public static void main(String[] args){ HelloWorld hw = new HelloWorld(); System.out.println("Hello, World"); hw.testFunc(); } } 

gives me this result:

 Hello, World Class = HelloWorld@7c6768 

What does @7c6768 after HelloWorld in the second line?

+6
source share
9 answers

According to docs of the toString () method in the object class

The toString method for the Object class returns a string consisting of the name of the class whose object is the instance, the at-sign `@ 'character, and the hexadecimal representation of the object’s hash code. In other words, this method returns a string equal to the value:

At

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

When you call toString () on an object, if you ovveride as shown below, you get your own implementation

  @Override public String toString() { //return something } 

Otherwise, you will get the default implementation that you see right now

From the source class object class

Returns a string representation of an object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative presentation that is easy for humans to read. It is recommended that all subclasses override this method.

The toString method for the Object class returns a string consisting of the name of the class whose object is the instance, the at-sign `@ 'character, and the hexadecimal representation of the object’s hash code. In other words, this method returns a string equal to the value: getClass (). getName () + '@' + Integer.toHexString (hashCode ())

 Returns: a string representation of the object. public String toString() { return getClass().getName() + "@" + Integer.toHexString(hashCode()); } 
+7
source

The toString() object is implemented as follows:

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

Since your HelloWorld class does not override it, this is the called method.

+13
source

The toString() method returns a string representation of the object.

In general, the toString() method returns a string that "textually represents" this object. The result should be a concise but informative presentation that is easy for humans to read. It is recommended that all subclasses override this method.

The toString method for class Object returns a string consisting of the name of the class whose object is the instance, the at-sign `@ 'character, and the hexadecimal representation of the unsigned hash code of the object. In other words, this method returns a string equal to the value:

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

From the API:

The toString method for the Object class returns a string consisting of the name of the class whose object is the instance, the at-sign `@ 'character, and the six-digit hexadecimal representation of the object's hash code

+3
source

HelloWorld @ 7c6768 is a string representation of the current object, and @ 7c6768 is a hash code. In fact, you are calling toString () of the current object

Here is the java doc for toString () http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Object.html#toString ()

+3
source

If you see the toString () method in the Object class

 /** * Returns a string representation of the object. In general, the * {@code toString} method returns a string that * "textually represents" this object. The result should * be a concise but informative representation that is easy for a * person to read. * It is recommended that all subclasses override this method. * <p> * The {@code toString} method for class {@code Object} * returns a string consisting of the name of the class of which the * object is an instance, the at-sign character `{@code @}', and * the unsigned hexadecimal representation of the hash code of the * object. In other words, this method returns a string equal to the * value of: * <blockquote> * <pre> * getClass().getName() + '@' + Integer.toHexString(hashCode()) * </pre></blockquote> * * @return a string representation of the object. */ public String toString() { return getClass().getName() + "@" + Integer.toHexString(hashCode()); } 

It returns the name of the class, followed by its hash code. This is the number you get.

+3
source

This is this.hashCode() . Since you do not override hashCode() , this number is the memory address in the JVM where the object is stored.

+2
source

Viewing the toString () object method:

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

This is the hash value of the object.

+2
source

A number that uniquely identifies an object. This is a hexadecimal representation of a hash code. In simple words, the entire printed string is the link returned after the class is instantiated.

+2
source

Source: https://habr.com/ru/post/950340/


All Articles