Hello World
Goals
- Compile and execute a Java program, with appropriate comments and documentation.
Concepts
- Application Programming Interface (API)
- Don't Repeat Yourself (DRY)
- Java Development Kit (JDK)
- Java Runtime Environment (JRE)
- Java Virtual Machine (JVM)
- bytecode
- class
- command prompt
- default package
- execute
- imperative programming
- method
- object code
- OpenJDK
- package
- procedural programming
- project root directory
- refactor
- source code
TODO
Preview
Preparation
- Download and install the latest version of the Java Development Kit (JDK). This course is based on Java 11.
- Make sure the download says “JDK” and not “JRE”.
- Unpack the archive contents into the directory of your choice. 7-Zip is recommended.
- Don't forget to set the
JAVA_HOME
environment variable to indicate the root directory where you unpacked the download file. - You will want to add the Java
…/bin
subdirectory to your system path.
- Install a good text editor that supports the UTF-8 encoding.
- Visual Studio Code (recommended)
- Modern, extensible, and supports other user interface languages in addition to English.
- Atom
- Modern and extensible, but can be buggy and unstable.
- Notepad++
- Has localizations for other user interface languages.
Lesson
The Java programming language became available in 1995. It was primarily created by James Gosling at Sun Microsystems, which was later sold to Oracle Corporation. Nowadays Oracle provides a distribution of Java that requires a license for commercial use, but the open-source version of Java used in this course, the OpenJDK, can be used without restrictions.
Java is object-oriented and runs on the Java Virtual Machine (JVM). The Java compiler javac
takes source code, stored in .java
files, and compiles them into object code, stored in .class
files. Java prefers to call its object code bytecode.
For Java to run on a platform there must therefore exist a JVM implementation for that CPU. Usually the JVM will function together with libraries and tools, collectively known as the Java Runtime Environment (JRE).
Project
It is best to create a separate directory for each project you will working on. For now, this project root directory will contain a simple subdirectory named src
for storing the source code. Create a directory structure named helloworld/src
.
Source Code
Packages of Classes
Generally speaking, each .java
file contains code for a Java class, which is a bundle of program logic. The Java class will indicate a package, which is a way to group related classes. You need to choose a package name that is unique.
In the src
directory, place the source code in a series of subdirectories that match the package structure, considering each component of the package to be a directory. For example a package of com.example
would result in the directory structure com/example
. The example below assumes you have changed to the src
directory, continuing the steps above.
Now using your editor, create the source code file inside the helloworld/src/com/example
directory. The .java
source code file should have the same name as the class.
Methods
Java is based on an imperative programming paradigm: each line in the source code is an instruction telling Java to perform some action. The JVM will read the codes in the .class
file representing the instructions you originally placed in your source code, and execute the commands, one at a time. You could modify the above program to print more information by adding more lines containing the command System.out.println(…)
.
The JVM would then execute each of these five instructions, one after the other. You'll note that the first two instructions are exactly the same as the last two instructions. Java doesn't care how many whether instructions are duplicated—it will execute them as provided. But from the perspective of program design, duplication of code or data makes the program larger and harder to maintain.
Java additionally allows for procedural programming, allowing you to group together multiple instructions into a “procedure” to prevent repeating the individual instructions yourself in the code. Java calls these procedures methods, and they will appear outside the main(…)
section. The simplest method begins with the words static void
and is followed by parentheses ()
, as you saw above.
In order to reduce the number of repeated instructions in the above code, you can place the instructions inside a method named printWelcome
. In each of the original locations of the duplicated code, you “call” the method using the method name followed by parentheses: printWelcome()
. A method therefore acts as a single instruction representing several instructions. Don't forget to add Javadoc documentation to your new method!
Compiling the Source Code
From the source code root directory src
, use javac
to compile the source code. It will produce a .class
file containing bytecode in the same directory as the source code.
Running the Bytecode
From the source code root directory src
, use java
to run the compiled class, making sure to indicate the entire package name. Don't indicate the name of the .class
file; Java will find it automatically based upon the package and class name.
Generating Documentation
The javadoc
tool will examine all your source code for some package(s) and generate HTML documentation for you automatically, based upon the Javadoc tags you have included. This documentation can be distributed to other developers to quickly and easily communicate how to interact with your program. Correct and thorough Javadoc documentation is therefore very important within the code.
From the project root directory, create a separate directory such as apidocs
for the documentation. Then from the source code root directory src
, use javadoc
to create documentation for all classes in the com.example
package. With this form of the Javadoc comment, don't specify the individual filenames, just the name of the package to be included in the documentation.
The documentation will be generated and placed in the helloworld/apidocs
directory. You can view it by loading helloworld/apidocs/index.html
in a web browser.
Review
Gotchas
- In Java, all code must be part of some class. Java will not accept simply a file containing “bare” code or methods.
- If you leave off the
package
indication, your Java classes will be placed in the default package, which is not recommended. - Make sure the directory structure for each class matches the components of its package.
- Make sure each statement ends in a semicolon. A missing
;
can cause all sorts of grief. - Javadoc comments, as opposed to normal comments, should be associated with something, such as a class or a method; they should not stand alone.
- The
javac
command expects a path to a source file, but andjava
command expects a package and class name, and will figure out the path itself from the current directory. - If you don't include the
-encoding UTF-8
option with thejavac
and thejavadoc
commands, you have no guarantee that your program will support international characters on your systems.
In the Real World
- Don't put your classes in the default package, or they won't play well with other programs and libraries. No one uses the default package in real life.
- Some people line up the braces
{
and}
vertically, as in the Preview section above. Some people put the first brace at the end of the previous line, as in the Source Code in the Lesson. Which you prefer may be a matter of personal taste, but above all should match what your team has agreed to use for a project. System.out.println()
is great for example programs, but you will seldom use it in real life unless you are writing a command-line program (which is hardly ever). Don't get in the habit of using it. Usually a logging facility is better, even for testing values.- The
main
method is only included if you want to run a program. Usually each application will only have one class with amain
method. Avoid the temptation to addmain
methods to other classes to test them; usually a unit testing framework is better. - Good developers will add plenty of comments to the source code, using
TODO
notations to indicate where improvements need to be made.
Think About It
- Did I include documentation for the class?
- Did I include an
@author
tag? - Did I include documentation for the methods?
Self Evaluation
- How is Java different from a compiled language such as C/C++ or Fortran?
- What is a “virtual machine”?
- What are the differences among the terms JDK, JVM, and JRE?
- The Wildlife Conservation Network (WCN) owns the domain
wildnet.org
. What might be an appropriate package name for a WCN application called “animal tracker”?
Task
Create your own “Hello World” program from scratch. Type it all in manually; don't copy and paste.
- Choose an appropriate package name.
- Add your own copyright comment at the top.
- Add your own Javadoc description of the class.
- Indicate that you are the author of the class.
- Provide Javadoc documentation for the
main
method. - Make the program print out your name in the
main
method. - Call a separate method that prints something else.
- Provide Javadoc documentation for your other method.
- Generate API documentation in the project's
apidocs
directory.
Put your entire project directory and contained files into an archive such as .zip
or .7z
and send it to your teacher. The 7-Zip application, indicated in the Resources, uses an especially efficient, open-source .7z
file format and is recommended for homework submission.
See Also
- History of the Java™ programming language (Wikibooks)
- How to install OpenJDK 11 on Windows? (Stack Overflow)
- OpenJDK 11 Installation on Windows 10 with JAVA_HOME (YouTube - java frm) recommended
- How To Install Java with `apt` on Ubuntu 18.04 (DigitalOcean) recommended
- How to install OpenJDK 10.0.1 on Ubuntu 18.04 (YouTube - Linux Help)
- OpenJDK 11 Zip Installation on CentOS 7.5 and Set JAVA_HOME Environment Variable (YouTube - java frm) recommended
- Installing OpenJDK 11 on macOS Mojave (Solarian Programmer) recommended
- How do I install Java 11 on Mac OSX allowing version switching? (Stack Overflow) Includes multiple approaches, including Jabba and Homebrew.
- How do I set or change the PATH system variable? (java.com)
- Compiling & Running a Simple Program (Oracle - Java Developer Tutorials and Training)
- The "Hello World!" Application (Oracle - The Java™ Tutorials)
- Hello World Application (YouTube - JavaKidProgramming)
- Naming a Package (Oracle - The Java™ Tutorials)
- How to Write Doc Comments for the Javadoc Tool (Oracle)
References
java
(Oracle JDK 11 Documentation)javac
(Oracle JDK 11 Documentation)javadoc
(Oracle JDK 11 Documentation)jshell
(Oracle JDK 11 Documentation)