views:

63

answers:

1

I have to get size of object which type I does not know. It's a template where I want to achieve sth like this:

 void sth(T data)
 {
    System.out.println("Data size = ", sizeof(data));
 }

How I can do this in Java?

sizeof - like a C sizeof ;)

+1  A: 

It is not an easy task. You have to use java's Instrumentation.

Here is an example.

jutky
but will this work when T is double (not Double) ?
mich
It can't be. You can't write a method signature that can accept any java type and provide it as that type if you include the primitive types.
EJP