2013-07-26 1 views
6

このプログラムjavaの** this **ポインタを印刷したときに表示される数字は何ですか?

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(); 
    } 
} 

は私にこの出力を与える:

Hello, World 
Class = [email protected] 

2行目のHelloWorldの後@7c6768は何を意味するのでしょうか?

+0

HelloWorldクラスのhashCode –

+1

オブジェクトのtoString()を返します。もしoveriddenされていなければ、それはクラス名@ハッシュコードhexaになります。 –

+0

@ KanagarajMは_class_ではなく、クラスの_instance_です。 – jlordo

答えて

7

As per Docs of toString() method in Object class

クラスオブジェクトのtoStringメソッドは、以下からなる文字列を返しますオブジェクトがインスタンスであるクラスの名前、アットマーク文字 `@ '、およびオブジェクトのハッシュコードの符号なし16進表現を返します。言い換えれば、この方法は、の値に等しい文字列を返します:

とき

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

あなたovveride以下のように、あなたが独自の実装を取得する場合は、オブジェクトのtoString()を呼び出すと

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

そうでなければあなたが今

012を見ているデフォルトの実装を与えます

オブジェクトの文字列表現を返します。一般に、toStringメソッドは、このオブジェクトを「テキストで表す」文字列を返します。結果は、人が読むのが簡単な簡潔で有益な表現でなければなりません。すべてのサブクラスがこのメソッドをオーバーライドすることをお勧めします。

ObjectクラスのtoStringメソッドは、オブジェクトがインスタンスであるクラスの名前、アットマーク文字 '@'、およびオブジェクトのハッシュコードの符号なし16進表現からなる文字列を返します。換言すれば、この方法は、の値に等しい文字列を戻します。 のgetClass + Integer.toHexString(ハッシュコード()) '@'()のgetName()+

Returns: 
a string representation of the object. 


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

+1正解です。 –

+1

私はちょうど追加したい:より意味のある何かを印刷するために、そのオブジェクトのtoString()メソッドをオーバーライドすることができます。 – Lenymm

+0

もし関数を '@Override 'として定義しているのなら、 \t public String toString(){ \t \t return" TEST "; \t} \t 'デフォルト値の代わりにTESTが表示されます。 – user13267

2

これはthis.hashCode()です。 hashCode()を再定義しないので、この番号はオブジェクトが格納されているJVMのメモリアドレスです。 APIから

3

ObjectクラスのtoStringメソッドは、オブジェクトがインスタンスであるクラスの名前で構成される文字列、アットマーク文字 `@」、および符号なしを返します。オブジェクトのハッシュコードの16進表現は

9

toString()方法は、Aを返しオブジェクトの文字列表現。

一般に、toString()メソッドは、このオブジェクトを「テキストで表す」文字列を返します。結果は、人が読むのが簡単な簡潔で有益な表現でなければなりません。すべてのサブクラスがこのメソッドをオーバーライドすることをお勧めします。

class ObjecttoStringメソッドは、オブジェクトがインスタンスであるクラスの名前、アットマーク文字 '@'、およびオブジェクトのハッシュコードの符号なし16進表現からなる文字列を返します。言い換えれば、この方法は、の値に等しい文字列を返します。次のように

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

オブジェクトのtoString()が実装されています

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

あなたHelloWorldクラスはそれをオーバーライドしていないので、これは方法であり、と呼ばれる。

+0

申し訳ありませんが、私はちょうど理解しているような非常に基本的な質問をしたい:getClass()。getName()、getClass()とgetName getClass()は、このtoString()関数が定義されているクラスのメンバ関数ですか?もしそうなら、getName()はどこから来ますか? – user13267

+0

Javaのすべてのクラスは 'Object'を拡張します。http://docs.oracle.com/javase/7/docs/api/java/lang/Object.htmlを参照してください。そのため、すべてのクラスにも' getClass() 'メソッドがあります。 – jlordo

2

ルックオブジェクトのtoString内部()メソッド:

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

、オブジェクトのハッシュ値です。

2

オブジェクトを一意に識別する番号。ハッシュコードの16進表現です。簡単に言えば、文字列全体がクラスをインスタンス化した後に返される参照です。

3

HelloWorld @ 7c6768は現在のオブジェクトの文字列表現で、@ 7c6768はハッシュコードです。実際、あなたは現在のオブジェクトのtoString()を呼び出している

ここ

はのtoStringのJavaドキュメント()あなたはObjectクラスのtoString()メソッドを参照してください場合http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Object.html#toString()

3

/** 
* 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()); 
} 

それはクラス名を返すですその後にそのハッシュコードが続きます。それはあなたが得ている数字です。

関連する問題