I want to learn how to program in Java on my Mac. What's the best way to get started?

Java programming is a great thing to learn - and your Mac is the perfect place to get started. Java is by far the most popular programming language in the world, and OS X is a fantastic programming environment. Programming is a highly valued skill in the modern world, and we think it's great fun to learn.

In this feature we're going to look at the following:

  • Installing and setting up Java in OS X
  • Writing your first Java program in NetBeans
  • Writing Java programs from the Terminal
  • The best online resources for learning Java

Don't worry. We'll have you learning to code on your Mac in no time.

Want to read more about Mac coding? Learn to code using C on Mac | Learn to code in C# on MacLearn HTML & web design on Mac | Learn Python coding on Mac | Learn to code in SQL on Mac

Learn how to code in Java on a Mac: Installing Java in Mac OS X

Learn how to code in Java on a Mac: Installing Java on a Mac

The first thing you need to do is download Java SE (Standard Edition) from Oracle. You may have Java installed in some form on your Mac already (probably the consumer version), but Java SE for Developers (known as the Java SDK) is the one to get.

You'll also need a IDE (Integrated Developer Environment), and Oracle has a great one called NetBeans. We'll install both the Java SKD and NetBeans at the same time.

  1. Open Safari and head to Oracle's Downloads page. Click on Java SE.
  2. Click on the Download icon under NetBeans with JDK 8. You'll want a programming environment, and Netbeans is a good one. The JDK 8 will come alongside it and it handles the install for you.
  3. Click on Accept License Agreement and click the Download link next to Mac OS X. The DMG file will be saved into your Downloads folder.
  4. Open the download file (ours is "NetBeans 8.1 wtih JDK 9 Update 73") and follow the installation instructions.
  5. Open System Preferences and click on Java then About. Check that Version 8 Update 74 (or later) is installed. Alternatively open Terminal and enter "java -version."

You should now have Java and NetBeans installed in OS X. NetBeans is where you'll be creating most of your programs, so let's take a look at it.

Learn how to code in Java on a Mac: Create HelloWorld in Java with NetBeans

Learn how to code in Java on a Mac: Create HelloWorld in Java on a Mac

The next step is to open NetBeans in OS X and create your first program. Tradition has it that the first program you create in any language should print "Hello World!" on the screen. Here's how to create HelloWorld in NetBeans:

  1. Open the Applications folder and launch NetBeans 8.1.
  2. Choose File > New Project.
  3. Ensure Java is selected in Categories and Java Application in Projects. Click Next.
  4. Enter "HelloWorld" in the Project Name field. Ensure "Create Main Class" is highlighted and click Finish.
  5. You should see the contents of HelloWorld.java in the main window. If not, use the Projects window on the left and locate HelloWorld.java (under Source Packages).
  6. Locate the code marked "// TODO code application logic here". Underneath this add a line that says "System.out.println("Hello World!");". Ensure this line of code is lined up with the "// TODO..." line above. See the image for exact positioning.
  7. Choose File > Save.
  8. Click the green Run Project icon in the Toolbar.

In the Output window at the bottom you should see the following:

Run:

Hello World!

BUILD SUCCESSFUL (total time: 0 seconds)

If you see anything else, then go back and check the code.

Learn how to code in Java on a Mac: Compiling Java programs from Terminal in OS X

Learn how to code in Java on a Mac: HelloWorld in Terminal

As an aside, some people prefer to create code directly in the Terminal (rather than use an SDK). Here is how to compile and run Java from the Terminal in OS X.

  1. Open Terminal.
  2. Enter mkdir HelloWorld to create a new directory and cd HelloWorld to move into it.
  3. Enter touch HelloWorld.java to create an empty Java file.
  4. Now enter nano HelloWorld.java to edit the file.
  5. In the Nano editor type out the following code:

public class HelloWorld {

   public static void main(String[] args) {

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

   }

}

  1. Press Control-X then y and Return to save the file and exit Nano.
  2. Now compile the program by entering javac HelloWorld.java in Terminal.
  3. Enter java HelloWorld to run it.

You should see Hello World outputted into the Terminal.

Learn how to code in Java on a Mac: Where to learn Java

You are now completely set up to learn Java on your Mac. Obviously you need to progress beyond just writing "Hello World" on the screen.

The good news is that there's absolutely no shortage of places to learn Java. Here are some of the best Java courses we know about:

Good luck learning Java!

Read next: 

How to write apps with Apple Swift 2

Best Mac for app development

How to make an iOS game