|
![]() |
JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Originally posted by Max Habibi:
If a method declares a variable internally, and then it(the method) is called several hundred times, then the the variable will be instantiated hundreds of times. OTOH, if the variable declared outside of the method, and used by the method, then it is only created once.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
A variable doesn't get instantiated, only objects.
In most JVMs, local variables will be placed on the stack. As the amount of memory needed for all the local variables of a method can be evaluated at "compile time" and a method call needs stack memory anyway, a local variable wouldn't need any additional commands to get its memory allocated.
Originally posted by Max Habibi:
A variable is the name given to an instantiated object. For example, in String myName = new String();, myName is variable.
You're probably thinking of the distinction between primitives and Objects.
The second way is more memory intenstive, regardless when 'when' the memory gets allocated, because of the class's design.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Originally quoted by Max Habibi
The second way is more memory intenstive, regardless when 'when' the memory gets allocated, because of the class's design.
Originally quoted by Ilja Preuss
But that is because of the instantiation of the MyHugeObject (via the 'new' keyword), not because of the declaration of the mho variable.
Originally posted by arun mahajan:
even if it is because of 'new' keyword, do you mean that Max second method will be more memory intesive.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
The answer is: "Most probably..."
The first example is instanciating one object, whereas the second instantiates 200 of them. Of course only one of them is referenced at the same time, so the others *might* get garbage collected - or they might not, depending on the implementation and configuration of the garbage collector
Originally posted by Max Habibi:
Regarding variables: I think we're saying the same thing, only in symatically different ways.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Well, we possibly *mean* the same thing, but it seems to me that we are *saying* substantially different things
....does that help?
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Originally posted by Max Habibi:
It helps explain where you're coming from. You're addressing the literal question "Is declaring variables inside a method is efficent or declaring it outside is efficient." by focusing on the word "declare". I'm addressing it based on what I consider to be the interesting part: that is: "What are the Advs and Disadvs of both.". That's why my reply goes into the pros and cons, explores the ramifications of each, and gives examples of when one path or another might be appropriate.
Just different styles of helping people, nothing to get worked up about.
Hopefully, the original poster picked up something useful.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
As declaration happens at compile time, that most probably doesn't present any performance issue.
QUOTE]
It's not a matter of "when", but a matter "how much/How often".
Does that help?
M
Originally posted by Max Habibi:
I say a variables is "the name given to " a variable, you say it's "a reference to" a variable. AFAIK, that's a slight variation in termanology.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Originally posted by Max Habibi:
As declaration happens at compile time, that most probably doesn't present any performance issue.
It's not a matter of "when", but a matter "how much/How often".
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
I say a variables is "the name given to " a variable, you say it's "a reference to" a variable. AFAIK, that's a slight variation in termanology
Originally posted by Ilja Preuss:
Originally posted by Ilja Preuss:
If the "when" is answered "before runtime", then the "how often" is nonrelevant for runtime performance, isn't it?
Do you really care about compile time that much?
Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |