The Author's Development Environment

Enjoy Chapter 1 of LearnToProgram’s new book Java for Beginning Beginners posted as an excerpt. The complete book is available in printed form or in Kindle form from

Java Programming:
Beginning Beginners’ Guide

Chapter 1 Excerpt

by Keshav Patel based on original material by Mark Lassoff

Java for Beginners: Writing Your First Program

Welcome. If this is your first time programming, congratulations! You’re about to start an adventure that most find very rewarding. If you are trying to learn after a previous abortive attempt, you deserve recognition as well. Tackling programming isn’t always easy—but it’s not outside the grasp of the average person, either.

Over the years, I’ve taught programming to over 500,000 people both online and in person. The programmers I’ve met over the years—both beginners and seasoned vets—have come from a variety of backgrounds. I’ve found that both PhDs and high school dropouts can make good programmers. I’ve had mathematicians, psychiatrists, and 12 year-olds in my classes over the years and all have been able to learn some programming.

You will learn too. In this introductory chapter—which is specially designed for the slightly apprehensive beginner—I am going to take you step-by-step through the process of writing your first program. You don’t need any special equipment except a laptop or desktop computer.

So without further delay, let’s get started!

What You’re Going to Learn

This first chapter will serve as an introduction on setting up your programming environment and getting started. Each chapter following is designed to take you through the basics of a certain aspect of development and give you a foundation for greater learning. Just to give you some context, here’s what’s coming up in the rest of the book:

Chapter 2: Input and Output
Input and output are key to any program. In this chapter we’ll look at how to get data into and out of a program.

Chapter 3: Understanding Variables
Variables are key building blocks in any program. This chapter takes readers through declaring and utilizing variables in expressions. Video tutorial included.

Chapter 4: Conditionals and Loops
Conditionals and Loops are structures which allow programs to make decisions that alter the execution of the program and result. These will be demonstrated and explained in this chapter.

Chapter 5: Dealing with Data
Almost every useful program, to some extent, deals with data. In this chapter you’ll learn how to store various forms of data in a program.

Chapter 6: Putting it All Together
In this final chapter, readers will learn how to create a useful program that processes and stores data, using the skills developed in the previous chapters.

Before we write any programs, we have to get your development environment set up. I won’t make you buy anything and you’ll have a few options to choose from.


The Author's Development Environment

Figure 1-1: The Author’s development Environment. Doobie Brothers’ Music not Included

Setting Up Your Development Environment

We’re going to be using a language called Java in this book. Installing Java on your machine is rather simple and the process is generally the same for most major operating systems. With your browser you’re going to want to navigate to Oracle is the company that maintains Java, and the JDK (Java Development Kit) that you need to download to write code in Java.

Once at, find “Downloads” on the top menu bar and click on “Java for Developers” under “Popular Downloads”.

Figure 1.2: with the Downloads Menu Open

Figure 1.2: with the Downloads Menu Open

After clicking that link you will see a download page. A dialog box will appear that reads JDK, with a download button under it. When you click it, it will redirect you to a page where you can select your operating system. The website will provide a set of instructions detailing how to go about installing the JDK onto your computer.

Figure 1.3: The terminal in the author’s computer. Note that the author changed the default colors through the terminal’s preferences

Figure 1.3: The terminal in the author’s computer. Note that the author changed the default colors through the terminal’s preferences

In the event that you’d rather not go through the process of installing a JDK to your machine I recommend you use the virtual Linux server provided by my friends at Point your browser to and register for their free account. The free account gives you a virtual Linux machine that you can access through your web browser—and it can compile and run Java code right out of the box! No downloads are required. It’s what I used to write the code we’ll be using in this book.

Once you have your account, click the button to turn on your Virtual Machine(VM) and get coding. You’ll be using a text editor on Koding to type Java files and the terminal to run them.

Figure 1.4: Command Line (or Terminal) in

Figure 1.4: Command Line (or Terminal) in

Writing and Runnig

Now that you have your environment set up, it’s time to (finally) write some code. We’ll write code in a text editor. A text editor is different than a word processor. A word processor will introduce invisible formatting code into a file. If you were to run code written in a word processor it would likely cause an error due to the embedded formatting code.

A text editor, on the other hand, saves the text you enter as pure text—without introducing any unnecessary formatting. The text produced by a text editor can be easily processed by the Java compiler. Within the command line environments in the virtual machine and on the Mac there are two text editors you can use. They are known as nano and vi. These text editors have a heritage that goes back to the 1970s—so as you use them you can truly feel “old school!”

The other option is to use the default text editor on Koding, TextEdit on a Mac or Notepad on Windows. They’re all rather self-explanatory, although some may need a few settings changed to highlight Java code. As a side note, make sure that whenever you first save your code files, you save them as “”.

Nano is an easy to use Linux-based editor. It’s pretty intuitive. You can simply start typing your code when the nano environment loads. Nano has a number of control codes which are listed at the bottom of your screen for your reference. The most important to note are CTRL-O which saves your file and CTRL-X which exits nano and returns you to the command line.

By default, nano will save your file in the same folder you were in when you opened nano itself.

Figure 1.5: The nano text editor.

Figure 1.5: The nano text editor.

You can start nano by typing nano and then hit enter on the command line.

vi is a slightly more complex (and even more “old school”) text editor. The most important thing to understand about vi is that there are two modes: command mode and insert mode. To enter insert mode, press ‘i’ on your keyboard. When you want to move into command mode to issue commands like write and quit, hit the ESC key. If you screw up and find yourself in the wrong mode, press ESC a lot and that seems to get you back into command mode from wherever you are at.

There’s an old joke about vi that goes like this: vi has two modes, and you’re in the wrong one. (If you don’t think that’s funny right now, you will after you use vi for a bit!)

When you enter vi, type i to move into insert mode and you’ll be able to type your code. When you’re done, hit ESC to move into command mode. All commands must be preceded by a colon. So to write your file you’ll enter :w. To quit you’ll enter :q. vi also lets you combine commands. For example, the combination :wq will write your file and then quit back to the command line.


Figure 1.6: vi wants you to help poor children in Uganda. It offers limited help for new programmers worldwide.

You can start vi by typing vi and then hitting return on the command line.

So now you should have your text editor started and be ready to enter your code. Enter the following code into your text editor exactly as it appears:

It should be at least somewhat evident what this code does. The println() function prints content to the command line. In this case you are printing two strings. Strings are simply lines of characters. Strings are always enclosed in quotes.

Once you’ve typed the code, it’s time to run it. In nano, save your file by typing CTRL-W. When prompted, enter the filename The “.java” extension indicates that this is a Java source code file. Next enter CTRL-X to quit and you’ll return to the command line. In vi, enter command mode and then save your file by typing :w This writes your file under the appropriate file name. Finally, in vi, hit :q to quit and return to the command line. In any other editor, including the one on Koding, generally you can open a menu and click ‘save as’ just like you would in any other computer program.

Notice that the filename we gave this program matches the name of the class (HelloWorld). This is the Java standard. This standard makes class files easily identifiable in build when you have multiple source files. So for housekeeping purposes, whenever you make a new ‘.java’ file make sure that the name of the class in the code (which is written after ‘public class’ in the first line of the code.) matches the name of the file. This way anyone reading your code will know which ‘.class’ file is associated with what ‘.java’ file.

Figure 1.7: Saving your file in Koding.

Figure 1.7: Saving your file in Koding.

Now that you have typed your code into an editor, open your terminal, and your cursor should appear at the prompt. Run the program by entering the following at the prompt (don’t type the ‘$’—that just represents the prompt itself):

The first line here commands Java to compile the source code that you wrote into what’s known as Java byte code and write it to a ‘.class’ file. This byte code is executed by the Java Virtual Machine (or JVM) whenever you run your program. JVMs can be installed on numerous devices, from cellphones to supercomputers, and they all do the same thing: run Java code. The result of this is Java’s ability to run on multiple platforms. So long as a JVM is installed on a machine, it can run the compiled Java code that you write.

The second line instructs Java to actually run what’s in the ‘.class’ file. Note that the ‘.class’ is implied. You don’t have to type it when you run your code. If you open the folder where the file is saved, you will see it as a ‘.class’ file though.

If you’ve done everything correctly you should see the result of your program printed before the next prompt.

Figure 1.8: Execution of the program

Figure 1.8: Execution of the program

If your output doesn’t look similar to figure 1.9, you need to load your text editor and code again and find your error. You can reload your code at the command line prompt with the following command:

vi: vi
nano: nano

In any other editor, open the file as you would any other.

Some common errors include:
-Not closing brackets
-Forgetting semicolons at the end of lines
-Mistyping “public static void main(String[] args)” (It’s case sensitive!)

Congratulations! You have successfully written your first program.

Understanding the println() function

In the preceding example we used the println function to output a string. The print function is also capable of printing out the following types of data:

Integers: Integers are “whole numbers” and don’t have a decimal point. In Java, you can print an integer like this:


Floating Point Numbers: Floats are more precise numbers that include a decimal point. In Java you can print a floating point number like this:


Expressions: Expressions must be evaluated before a result is determined. Expressions are usually arithmetic problems. To print an expression in Java:


In the expression above, the part of the expression in parenthesis will be evaluated first due to the order of operations. The result printed is 0.3.

We’ll look at expressions more closely in our next chapter.

As a side note, it’s good to understand that the println function is a version of the print() function. The ‘ln’ at the end stands for line. This means after printing what you place in the parentheses the function will make a new line to print the next expression. The ‘print’ function will simply print whatever you put in the parenthesis without introducing its own formatting.

To purchase the entire book, visit Java Programming: Beginning Beginner’s Guide is available as a Kindle eBook or paperback.

2 thoughts on “Java for Beginning Beginners: Book Excerpt

  1. Minor edit needed: The line right after the byline should be, “Java for Beginners: Writing Your First Program” not, “Javascript for Beginners: Writing Your First Program.”

Leave a Reply

Your email address will not be published. Required fields are marked *