I am wondering if i included many import
in my java program, would it affect the performance of my code (for example, program will be slower)? Is the logic behind the import
in Java the same as include
in C?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
Not in the slightest. In fact, the compiled classes (using imports or not) will be identical. An import is merely syntactic sugar that allows you to use a shorter name for an external class or (with a static import) class member in your source code. In other words, it allows you to write:
instead of
That is all.
There is potentially a small (tiny) difference in compilation times. But, AFAIK, something like
import java.util.*;
does NOT cause all of thejava.util
classes to be loaded by the compiler. Rather it just adds the names of the classes to the symbol table.Having said that:
.*
) can lead to unexpected collisions.No it is not.
A C / C++ include directive injects arbitrary1 C / C++ "code" into the source stream. This can include declarations and executable statements ... which can affect both performance, execution memory footprint and the size of the executable.
1 - That is, whatever the authors of the include file chose to put into the file. It could be simple method and class "signatures", but it could also be macro's, code and other declarations that are impactful. You have to examine the file to be sure.
import
does not slow your program. It is better to have different kinds ofclasses
in differentpackages
and import them as needed. Improves code readability.It will have no impact on the ~run~time speed of your program.
It may have an impact on the ~compile~time speed of your program.
If you
import java.util.*;
it will load all of the java.util package into the compiler, which may increase the compile time when you.*
an entire package for a single usage (although you should perform some profiling if it's going to be a concern.)In addition to potential compile time issues, don't forget to consider the readability issues. Generally, I (and people I've talked with) find
import pack.age.Class;
to be more readable thanimport pack.age.*;
- have a talk with your team of course before making a decision about this.But the logic behind it is very different from
#include
and does not bloat the code. You may end up with more than necessary as you include dependency jars, but that's probably not a big issue.Importing is not an issue. You can import everything you want from package and execution of your code will not get slower. import is for convenience so you can use shorter names.
The classes are loaded when their constructor is called implicitly or explicitly (new operator). In case of enums it will be when you refer to enum names in your code. This will cause class (enum) to be loaded. (You can try using println in private constructor of enum to experiment and see when enum gets loaded). Loading a class takes time and memory. Generally speaking loaded classes are not meant to be unloaded.
No, it wouldn't affect performance of your code.
The binaries (the class files) do not increase in size as the import is not implemented with any cut-and-paste mechanism.
It is merely a syntactic sugar for avoiding to have to write for instance
Here is a little test demonstrating this:
(modifying
Test.java
)No, an
#include
is a preprocessor directive and is implemented with a cut-and-paste mechanism.