This was the question asked in one of the interview I have to write开发者_如何学C a program but there should not be any import statements in the program as i need to import java.io.* package. Is this program more efficient without using import statements?
It is not more efficient. I guess it is just Guys tried to figure out whether you understand what goes under import or not.
There is a difference in "imports" in C++ and Java.
And it seams that they just check your awareness of it.
If you explicitly import on the beginning of the source-file or explicitly qualify the class-use (java.io.Writer input = new java.io.Writer ...) makes no difference as the compiler creates the bytecode.
This is a silly question. You will have to use the fully qualified names in your source code. I don't think that this makes any difference than using imports.
The question would had a meaning, if you were asked to do a task that can be implemented only with java.lang classes. But reading a file is not such a task.
Import statements don't affect run-time efficiency.
But it is an interesting question. I assume you can do this in a platform-dependent way. For example:
Use java.lang.Runtime
to execute (exec
) a native command (like cat
) and capture its output, using process.getInputStream().read(..)
(this uses InputStream
, but doesn't import it)
Another option, which I assume is not the point of the question, because it will be a dumb question then, is to use the fully-qualified names of the java.io.
classes in your code.
One possible way:
Use java.lang.Runtime to call OS commands to read the file.
yes you can use fully qualified name. as the compiler will covert your import statements to fully qualified names.
精彩评论