sometimes printf("%p", this) helps to see different instances.
What's the equivalent of that in android?
(to print out address of this
variable or something unique(it may not be address
) to the instance)
It seems I can new
interface like OnTouchListener
then, how do I print something to differentiate the different instances of them?
To print a variable inside the print statement, we need to use the dollar symbol($) followed by the var/val name inside a double quoted string literal. To print the result of an expression we use ${ //expression goes here } .
You first include the character f before the opening and closing quotation marks, inside the print() function. To print a variable with a string in one line, you again include the character f in the same place – right before the quotation marks.
Description. disp( X ) displays the value of variable X without printing the variable name. Another way to display a variable is to type its name, which displays a leading “ X = ” before the value. If a variable contains an empty array, disp returns without displaying anything.
Print : Click to print the logcat messages. After selecting your print preferences in the dialog that appears, you can also choose to save to a PDF. Restart : Click to clear the log and restart logcat.
Something like this should do it:
android.util.Log.i("Instance", "This is: " + this);
By default, the toString
implementation of Object
will print the class type plus a hash code which can be considered somewhat equivalent to the this pointer in C++.
The
toString
method for classObject
returns a string consisting of the name of the class of which the object is an instance, the at-sign character'@'
, 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:getClass().getName() + '@' + Integer.toHexString(hashCode())
If an object provides a different implementation of toString()
, like for instance String
does, then you can use the above canonical implementation of the base toString()
method to get the same results.
To get something distinct to the instance, you should use System.identityHashCode(this)
. It returns what the default implementation of .hashCode()
in Object
returns (which may have been overridden in subclasses, so that's why you shouldn't use .hashCode()
directly), which, according to the documentation, is "As much as is reasonably practical" distinct for distinct objects.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With