Introduction: Hello World in Java on Mac OS X

This tutorial is an introduction to object oriented programming in the high-level computer programming language, Java, on a Mac OS X operating system. The following instructions will guide you through writing, compiling, and executing a simple computer program that prints “Hello World!” to your console.This tutorial will introduce new programmers to basic java syntax. A computer running on a Mac OS X operating system is required. You will be using a text editor and java compiler included on your computer. No additional software is needed.

Step 1: Open TextEdit Application

To write your code, you will use a text editor application included on all Mac OS X operating systems. For more complex computer programming projects, it is recommended to use a java Integrated Development Environment (IDE) to edit your code. However, this program will be simple enough to use a basic, open source text editor. Open the application called TextEdit. Click on Finder, Applications, then click on the TextEdit application. Open a new document.

Step 2: Configure TextEdit for Java Programming

First you must configure the text editor for java code. On the menu bar on the top left corner of your screen, click on TextEdit, then Preferences.

In the New Document tab, change the document format to Plain Text under the Format section. Uncheck the Smart quotes box under the Options section towards the bottom of the preference window.

Switch to the Open and Save tab. Change the Opening files and Saving files to Unicode (UTF-8).

Close the TextEdit application and re-open it. Open a new document.

Step 3: Create a HelloWorld Class

Now you can begin to write your code.

The first step in writing your program is to create a class called HelloWorld. In object oriented programming, a class is a template for an object. An object is an instance of a class. For reference, you can think of a class as the blueprints of a house, and an object as the house itself.

To create your class, type in your editor

public class HelloWorld {

}

The key words public class creates a new class. HelloWorld is the name of your class. Everything placed inside the brackets following HelloWorld will be included in the class.

Step 4: Create the Main Function

Next, create a function in your HelloWorld class called main. When the computer executes your program, it will perform the instructions inside the brackets of the function called main.

To create the function, inside the brackets of your HelloWorld class, type

public static void main(String[ ] args) {

}

The key words public static void will create a function that can be accessed by your computer. main is the name of the function. When the program executes, it will only perform the instructions inside the brackets of the mainfunction. All functions in java are followed by a set of parenthesis. Some functions in java have a variable inside the parenthesis called a parameter. The parameter inside of the parenthesis following the main function, String[ ] args, represents data that can be supplied to the program by the user when executing the program. For this project, you will not be supplying any data to the program.

Step 5: Define the Main Function

The final step in writing your code is to define the main function in you HelloWorld class. The instructions inside the brackets of the main function will tell your computer what to do when it executes the program. For this program, you want the computer to print the words "Hello World!" to your console. The java instruction

System.out.println(parameter)

will print the parameter of the instruction to the computer console. Inside the braces of the main function in the HelloWorld class, write the instruction

System.out.println("Hello World!");

Be sure the words that you want the program to print to your console are placed inside quotation marks. Place a semi-colon at the end of your instruction to show that the instruction on that line of code has ended. This step concludes the coding portion of the tutorial. Your program should look similar to the program in the image above. The indentations are not necessary for the functionality of your program, but they are recommended to make you code more readable.

Step 6: Save Your Program

Save your computer program to your Desktop. On the menu bar on the top left corner of your screen, click on File, Save. Save your computer program as

HelloWorld.java

The extension .java creates a java file that can be compiled by the compiler.

Be sure that the file name is the same name as your class and that the TextEdit application encodes your file to Unicode (UTF-8)

Step 7: Compile Your HelloWorld Program

Next, compile your program using the java compiler included on Mac OS X operating systems. You can compile your computer program using your terminal.Your terminal is a device that allows you to communicate with your computer. To open your terminal, click on Finder, Applications, Utilities, then click Terminal.

In your terminal, navigate to your HelloWorld.java file.To navigate to your file, you can change directories on your terminal to your desktop where you saved you HelloWorld file. Use instruction cd (short for Change Directory) to navigate to your desktop. In your terminal, type

cd Desktop

and press Enter. You can now compile your HelloWorld program. Compiling your program will convert the code in you HelloWord java file to language the computer can read. To compile your program, type

javac HelloWorld.java

into your terminal and press Enter. This will create an additional executable file on your desktop that your computer can run. If there are any errors in your code, the compiler will point them out to you on your terminal.

Step 8: Run the Program

After you compile your program you can run your executable file. In your terminal, type

java HelloWorld

and press Enter to run your program. The computer should print "Hello World!" to your console.

For a more personalized computer program, return to step five and change the instruction

System.out.println([parameter])

to make the computer print whatever instruction you'd like.