2 Creating a Simple Java Program

 

Exam objectives covered in this chapter

What you need to know

[2.1] Create executable Java applications with a main method.

The right method signature for the main method to create an executable Java application.

The arguments that are passed to the main method.

[2.2] Compile and run a Java program from the command line.

[2.3] Import other Java packages to make them accessible in your code.

Compiling and executing a Java program using javac and java tools.

Understand packages and import statements. Get the right syntax and semantics to import classes and interfaces from packages in your own classes.

In the OCP Java SE 11 Programmer I exam, you’ll be asked questions on the executable Java applications and its requirements, packages, importing classes. Given that information, this chapter will cover the following:

  • Understanding executable Java applications
  • Command line – code compilation and execution
  • Java packages

2.1      Executable Java applications

[2.1] Create executable Java applications with a main method 

Doesn’t the Java Virtual Machine execute all the Java classes when they are used? If so, what is a nonexecutable Java class? Let’s uncover the details in this section.

2.1.1   Executable Java classes versus nonexecutable Java classes

2.2      Main method

2.4      Java packages

2.4.1   The need for packages

2.4.2   Defining classes in a package using the package statement

2.4.3   Package naming conventions

2.4.4   Directory structure and package hierarchy

2.4.5   Using simple names with import statements

2.4.6   Using packaged classes without using the import statement

2.4.7   Importing a single member versus all members of a package

2.4.8   Recursively importing subpackages

2.4.9   Importing classes from the default package

2.4.10 Java source files with multiple classes or interfaces

2.4.11 Static imports

2.5      Summary

2.6      Sample exam questions

2.7      Answers to sample exam questions