This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Performance and the fly likes Performance:  Constants vs Hard-Coded Text Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Performance
Bookmark "Performance:  Constants vs Hard-Coded Text" Watch "Performance:  Constants vs Hard-Coded Text" New topic
Author

Performance: Constants vs Hard-Coded Text

Gabriel Cane
Ranch Hand

Joined: Mar 27, 2001
Posts: 39
I know that, from a maintenance and code readability point of view, storing text and/or numbers in constants is a good idea. What I would like to know is, from a performance/memory standpoint, which is better?
Will hard-coding text and/or numbers save processing/memory vs. constants, or vice-versa?
Also, is placing all constants in an interface that all classes implement a good idea, again from a performance standpoint?


Sun Certified Programmer for the Java 2 Platform
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
I vaguely remember from somewhere that constants (static final) defined in a class end up in memory twice (once in the loaded class and once on heap, presumably), while constants in an interface only end up in memory once.
Can anyone confirm whether this is true or false?


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Gabriel Cane:
I know that, from a maintenance and code readability point of view, storing text and/or numbers in constants is a good idea. What I would like to know is, from a performance/memory standpoint, which is better?
Will hard-coding text and/or numbers save processing/memory vs. constants, or vice-versa?
Also, is placing all constants in an interface that all classes implement a good idea, again from a performance standpoint?

Constants whichs value can be computed at compile time (that is, they are not computed using an expression containing a method call, for example), are inlined by the compiler. Therefore the use of constants (final static fields) doesn't have any performance drawbacks.
Notice that you therefore need to recompile all classes using the constants if change their value!


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
Mark Herschberg
Sheriff

Joined: Dec 04, 2000
Posts: 6037
Ilja is correct. Keep in mind if you define

All expressions which can be computed from constants are, in this case, area will be defined at compile time, too, and anywhere it is in the code, it will be replaced with the constant value.
As for putting constants into an interface, that is generally a bad idea. It tends to lead to global constants which goes against the principles of OO. I'm not saying never to do it, but rather just to be careful.

--Mark
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Hey Mark - what's this 'x' operator you're using?
Actually in the given example, area is not defined at compile time, because the length and width were not declared final. Rules for compile-time constants are given in the JLS here - the last two bulleted items allow variables to be part of constant expressions, but only if they are final variables which were themselves initialized with constant expressions. So it can be useful to declare a variable final in a case like this.

"I'm not back." - Bill Harding, Twister
Mark Herschberg
Sheriff

Joined: Dec 04, 2000
Posts: 6037
You're right! I was being incredibly sloppy. I was going to make them private static final but was being lazy. And then using x. Boy, this is what happens when you don't code for a month!
Shamefully,
Mark
 
Consider Paul's rocket mass heater.
 
subject: Performance: Constants vs Hard-Coded Text
 
Similar Threads
Parallels vs. VMware Fusion vs. BootCamp
Few Large Classes/Methods vs. Many Small Classes/Methods
Enum vs Constant File
Hashtable vs Hashmap
XP Vs. RUP