CSC300 / CSC402: Import [2/11] Previous pageContentsNext page

PythonJava
01
02
03
04
05
def main():
    print("Hello")

if __name__ == "__main__":
    main()
01
02
03
04
05
06
07
package algs11;
import stdlib.*;
public class Hello {
  public static void main (String[] args) {
    StdOut.println ("Hello");
  }
}

System.out and System.in refer to objects.

I don't want to talk about objects yet, so we will use the version about, which replaces System.out with StdOut.

StdOut is a class in the stdlib package.

System is a class in the java.lang package.

All of the classes in java.lang are imported implicitly into every java program. All other classes must be imported explicitly.

The statement import stdlib.* makes the classes declared in stdlib visible in our program.

Instead of importing every class from a package, you can also import a single class.

Unnecessary imports will generate a warning.

If your code has a compiler error, it will not run.

If your code has a warning, you can run it. But you should fix the warning.

Previous pageContentsNext page