G
gwlucas
I am considering using static imports in my Java code and was
wondering whether there was a performance cost or resource issues
associated with doing so. Can anyone tell me about how Java
implements the "import static" syntax that was introduced in Java
1.5? Is there anything I need to be careful about?
I think I would have implemented this feature as a simple alias, but
that keyword "static" suggests the possibility that the compiler might
actually be doing something more complicated and potentially costly.
For example, I can see the possibility that "static" means it's
importing byte code from the referenced class. I did try comparing the
size of the class files for the same logic coded with and widthout
static imports and the sizes are similar. But I'm not confident about
drawing any conclusions from that. I am especially concerned about
hidden memory or performance costs.
Thanks,
Gary
Background
Much as I admire the Java programming language, one thing I have
always disliked is the need to include the class name in cases where I
am programming math-intensive applications. For example:
double a = Math.cos( angleInDegrees * Math.PI/180);
If you use math methods once or twice this is no big deal. But anyone
who has ever tried to do something non-trivial knows that this can be
a real problem. Reading math code is hard enough without all the extra
clutter. And though I know that this whole discussion can lead to some
rather thorny issues (it's not unrelated to the whole question of
overloading operators... which I'd never advocate), I've always felt
that at least Math should get some special dispensation. Anyway, the
"import static" syntax renders a lot of this discussion moot. Consider
the following code fragment:
import static java.lang.Math.cos;
import static java.lang.Math.PI;
public class StaticShock {
public static void main(String[] args) {
double a;
a = cos(45*PI/180.0); // a = Math.cos(45*Math.PI/180.0);
System.out.println("a="+a);
}
}
wondering whether there was a performance cost or resource issues
associated with doing so. Can anyone tell me about how Java
implements the "import static" syntax that was introduced in Java
1.5? Is there anything I need to be careful about?
I think I would have implemented this feature as a simple alias, but
that keyword "static" suggests the possibility that the compiler might
actually be doing something more complicated and potentially costly.
For example, I can see the possibility that "static" means it's
importing byte code from the referenced class. I did try comparing the
size of the class files for the same logic coded with and widthout
static imports and the sizes are similar. But I'm not confident about
drawing any conclusions from that. I am especially concerned about
hidden memory or performance costs.
Thanks,
Gary
Background
Much as I admire the Java programming language, one thing I have
always disliked is the need to include the class name in cases where I
am programming math-intensive applications. For example:
double a = Math.cos( angleInDegrees * Math.PI/180);
If you use math methods once or twice this is no big deal. But anyone
who has ever tried to do something non-trivial knows that this can be
a real problem. Reading math code is hard enough without all the extra
clutter. And though I know that this whole discussion can lead to some
rather thorny issues (it's not unrelated to the whole question of
overloading operators... which I'd never advocate), I've always felt
that at least Math should get some special dispensation. Anyway, the
"import static" syntax renders a lot of this discussion moot. Consider
the following code fragment:
import static java.lang.Math.cos;
import static java.lang.Math.PI;
public class StaticShock {
public static void main(String[] args) {
double a;
a = cos(45*PI/180.0); // a = Math.cos(45*Math.PI/180.0);
System.out.println("a="+a);
}
}