Reputation: 1970
I have a simple code:
public class Hello
{
public static void main(String[] args)
{
System.out.println("Hello World");
}
}
I know that Object
class is imported by default in every java program, but I wanted to ask whether PrintStream
class is also imported by default if we are using print()
or println()
functions?
Because Object
class belongs to java.lang
package and PrintStream
class belongs to java.io
package, so if it is not imported by default, then how we are able to use the println()
method of PrintStream
class?
If it (PrintStream
class) is not imported by default, then why the decompiler is importing it?
This is the output after decompiling it:
Upvotes: 2
Views: 1433
Reputation: 2423
The types of intermediate expressions in your Java program do not need to be imported on the source code level. It's only when you assign the value of such an expression to a declared variable that you have to make its type explicit; at that moment you have to add the import (or use the qualified name).
In your case, System.out
is such an intermediate expression; its type is indeed java.io.PrintStream
, which is not imported by default as it is not in java.lang
. If you would modify your class to
import java.io.PrintStream;
public class Hello {
public static void main(String[] args)
{
PrintStream myOut = System.out;
myOut.println("Hello World");
}
}
you need to add the import statement, or use the qualified name as in
public class Hello {
public static void main(String[] args)
{
java.io.PrintStream myOut = System.out;
myOut.println("Hello World");
}
}
On the bytecode level the story is different: since all dependencies need to be loaded for the JVM to be able to execute the code, all of them are listed in the .class
file, including the types of intermediate expressions. Apparently the decompiler used in the screenshot of the OP isn't clever enough to realise that such imports are unnecessary on the source code level, and so it just creates import statements for all dependencies listed in the .class
file.
Upvotes: 1
Reputation: 100169
why the decompiler is importing it
Looks like a bug in the decompiler you are using. This import is completely unnecessary here.
You can program without imports at all, just using the fully-qualified class names like this:
java.io.PrintStream out = System.out;
Imports are used just for convenience, so you can use simple class names in your code. It's possible that the same simple name appears in different packages (for example, java.awt.List
and java.util.List
) so to resolve this ambiguity you have to either use full class name or import the one you want (in case you want to use both of them, you will still have to use the full name for one of them). As you correctly mentioned, only classes from java.lang
are imported always by default. Again, this is done for convenience, so you can use just System
instead of java.lang.System
(though java.lang.System.out.println()
is also valid).
In your example as you don't directly mention the PrintStream
in the source, no need to import it. Imports have nothing in common with class loading (which happens in runtime, not during the compilation).
Upvotes: 0
Reputation:
I wanted to ask whether PrintStream class is also imported by default if we are using print() or println() functions
No, from the JLS:
A compilation unit automatically has access to all types declared in its package and also automatically imports all of the public types declared in the predefined package
java.lang
.
So you can use System
because it belongs to java.lang
.
so if it is not imported by default,then how we are able to use the println() method of PrintStream class?
Because System.out
is accessible to your type, so you can use all visible method of System.out
Upvotes: 0