© Mikael Olsson 2018
Mikael OlssonJava Quick Syntax Referencehttps://doi.org/10.1007/978-1-4842-3441-9_1

1. Hello World

Mikael Olsson1 
(1)
Hammarland, Länsi-Suomi, Finland
 

Installing

Before you can program in Java you need to download and install a Java Development Kit (JDK) , such as the Standard Edition (JDK SE) from Oracle’s website.1 Among other things, the JDK includes the Java compiler, the class libraries, and the virtual machine needed to run Java applications. Oracle’s download page also has a link to obtain Netbeans2 bundled with a JDK. Netbeans is an Integrated Development Environment (IDE) that will make development in Java much easier. Alternatively, another free IDE you can use is Eclipse.3 If you don’t want to use any IDE at all, a regular text editor will work just fine.

Creating a project

If you decide to use an IDE (recommended), you need to create a project, which will manage the Java source files and other resources. If you prefer not to use an IDE, you can create an empty file with the .java extension—for example, MyApp.java—and open it in your text editor of choice.

To create a project in Netbeans, click File ➤ New Project. In the dialog box select the Java Application project type under the Java category and click Next. In this dialog box set the project name to “MyProject” and the name of the main class to “myproject.MyApp”. Change the project’s location if you want and click Finish to generate the project. The project’s only file, MyApp.java, will then open up, containing some default code. You can go ahead and remove all of that code so that you start with an empty source file.

Hello World

When you have your project and programming environment set up, the first application you will create is the Hello World program. This program will teach you how to compile and run Java applications, as well as how to output a string to a command window.

The first step in creating this program is to add a public class to your MyApp.java source file . The class must have the same name as the physical source file without the file extension—in this case, “MyApp”. It’s legal to have more than one class per file in Java, but only one public class is allowed, and that name must match the filename. Keep in mind that Java is case sensitive. The curly brackets following the class name delimit what belongs to the class and must be included. The brackets, along with their content, is referred to as a code block , or just a block:

public class MyApp {}

Java classes are organized into packages , which are similar to namespaces in other languages. A package statement needs to appear at the top of the file to designate which package a file belongs to. This name must match the directory the file is located in relative to the project’s source directory, so in this case the package name is myproject:

package myproject;
public class MyApp {}

Next, add the main method inside the class. This is the starting point of the application and must always be included in the same form as is shown in the following code. The keywords themselves will be examined in later chapters:

package myproject;
public class MyApp {
  public static void main(String[] args) {}
} 

The last step in completing the Hello World program is to output the text by calling the print method . This method is located inside the System class, and then another level down inside the out class. The method takes a single argument—the string to be printed—and it ends with a semicolon, as do all statements in Java:

package myproject;
public class MyApp {
  public static void main(String[] args) {
    System.out.print("Hello World");
  } 
} 

Note that the dot operator (.) is used to access members of a class. Similar to print, there’s also the println method , which automatically adds a line break at the end of the printed string. The System class belongs to the java.lang package, which is always included in a Java project.

Code hints

If you’re unsure of what a specific class contains, or what arguments a method takes, you can take advantage of code hints in some IDEs, such as Netbeans. The code hint window appears anytime you’re typing code and there are multiple predetermined alternatives. You can also bring it up manually by pressing Ctrl+Spacebar. This is a powerful feature that gives you quick access to the class libraries and their members, along with descriptions.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.5.15