4

With regards to memory efficiency I have the following question:

It is imperative that correct Data types are used for the corresponding variables. Representing a numeric value of 1 as a byte requires an eighth of the memory a long(but please correct me if I am wrong). My question though is if memory allocation/deduction takes place upon defining your variable type or initialization? It may seem odd, but I ask in relation to global variables that do not need to be initialized as they have defaults as apposed to local variables. I would also like to know if there is a default size for Object data types? I presume this is based on the JVM (32bit vs 64 bit)?

thejartender
  • 9,339
  • 6
  • 34
  • 51

1 Answers1

1

It is imperative that correct Data types are used for the corresponding variables.

Of course. Java is a strongly typed language. Your point?

Representing a numeric value of 1 as a byte requires an eighth of the memory a long(but please correct me if I am wrong).

You're wrong. Depending on what other variables and types are declared adjacently, it may take 4 or even 8 bytes, depending on the padding used by the JVM.

My question though is if memory allocation/deduction takes place upon defining your variable type or initialization?

Neither. It happens at allocation time, i.e. at new time, rather than during the constructor for example.

It may seem odd, but I ask in relation to global variables that do not need to be initialized as they have defaults

All variables need to be initialized. You just don't have to write initializer code in the case of static or instance variables. The word 'global' in reference to Java has no meaning.

as opposed to local variables.

It doesn't make any difference what it is. The variable still has to have space allocated to it and a value stored in it, whether static, instance, or method-local.

I would also like to know if there is a default size for Object data types? I presume this is based on the JVM (32bit vs 64 bit)?

The question is meaningless. Instances of the class Object are always of the same size, which carefully isn't documented or specified anywhere, and is therefore free to vary with the JVM. Instances of other classes ditto. There is no 'default' anywhere in any useful sense that I can see.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • I know Java is strongly typed I was referring specifically to the context of primitives that are documented to have difference in memory and thus it's essential to "choose" the correct data type no? My cprimitive class variables don't seem to need to be initialized! Can you explain why? And can you clarify what allocation time is exactly? literals have no "new" to go by so when is memory deducted regarding literals? – thejartender Apr 16 '13 at 16:02
  • @thejartender It's essential to choose the data type correctly anyway. I don't understand your point here. All static and member variables are initialized, either to your values or the defaults. The primitive types default to zero or false. Memory for a class is allocated when the class is loaded: for an instance, when you 'new' the instance. Primitive members included. If you have code that is behaving unexpectedly you should edit it into your code. – user207421 Apr 16 '13 at 23:53
  • @EJP So the memory is allocated when the `new` keyword is actually invoked, not if it is declared. But what is going on in the background when a object variable is declared i.e. `Object r;`? – Honinbo Shusaku Feb 12 '16 at 23:41
  • 1
    @Abdul It affects the size required to allocate the object, at compile time. It doesn't cause execution of specific code at runtime other than being another word or so of memory to zero when the object is allocated. – user207421 Jan 15 '18 at 14:39