Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

print `this` variable in android

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?

like image 396
eugene Avatar asked Dec 27 '12 05:12

eugene


People also ask

How to print variable value in Android Studio?

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 } .

How do you print the value of a variable?

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.

How do you display one variable value?

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.

How do I print a log message on android?

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.


2 Answers

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 class Object 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.

like image 145
K-ballo Avatar answered Oct 01 '22 19:10

K-ballo


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.

like image 22
newacct Avatar answered Oct 01 '22 20:10

newacct