views:

116

answers:

4

I have 2 variable like below

char s='s'; 
char Kaazoooombaa ='s';

how can I know which variable is consuming how much space ?

+1  A: 

Variable names mean nothing after compilation; there are only there to make the program easy for humans to read and write.

jrockway
Sure? What about public fields in a class, that are also variables?
TomTom
Did you see the Java tag? How can you decompile Java code, recovering variable names, if they're not stored anywhere?
paxdiablo
Even if the space taken by variables is negligible, I don't think compiler will go on ignoring such things
Ravi Gupta
*Local* variable names mean nothing after compilation. *Member* variable names are embedded in the .class file, as you can see with javap.
EJP
Oh yeah, Java is one of those languages where they call attributes variables.
jrockway
+6  A: 

Other than a small amount of space for the metadata (which will be allocated once in total, not once per instance) the name makes no difference1. So in the example given, each of those variables will take 2 bytes, possibly padded to 4 bytes depending on the other fields and the VM being used.

Note that even for reference types (including arrays) the space taken by the variable itself is fixed by its type (and the details of the VM) - the space taken by a reference to an empty string is the same as the space taken by a reference to a huge string.


1 There may be a cost per use of the variable - or perhaps per class using the variable. You'd have to consult the VM specification to check the exact binary representation. Even then that's the representation in the class file, which may well not lead to increased memory use by the time it's all JIT compiled etc. It's almost certain to be irrelevant in the grander scheme of things anyway - the memory required by actual objects almost always dwarfs the memory required by the code itself.

Jon Skeet
A: 

Variable names are for human readability. In this case s and Ka..a are the same size.

@TomTom for things like public their names again don't change size but the fact that they are public does if I'm not mistaken. That size may be taken on by the class which contains them though.

Mimisbrunnr
A: 

Generally variable names is used by the compiler, not the runtime. If the class are exported from your module, then the names are encoded in the metadata.

But if you are worried how much space your program will take, then the name of variables is the very least of you worries.

Arve