Java Lesson 1: Environment Setup



We need to set up an environment conducive to developing and executing Java programs. The environment enables the three steps necessary to run a Java program:

  1. Create and edit the Java source code with an appropriate editor.
  2. Compile the source code to Java bytecode.
  3. Execute the program on your computer.

Create and Edit

Several options exist to create your Java source code and perform subsequent edits to the code. The simplest is to use a text editor such as Notepad (for Windows) or gedit (ie., Text Edit) (for Linux).

A better option is an editor which is specifically designed for editing source code, such as Notepad++ (for Windows), TextMate (for Mac OS) or Sublime Text which is available for several operating systems. These editors reduce your workload by handling some of the more mundane chores such as indenting source code for readability and displaying keywords in a different text color.

Java applications can also be edited, compiled, and executed, in an Integrated Development Environment (IDE). Several good IDEs are available, including Eclipse, NetBeans (which is commonly used in colleges and universities), and IntelliJ IDEA. While powerful, IDEs can be confusing to beginners.
ACTION ITEM: For simplicity we will use Notepad on Windows and the Windows Command Prompt for the purposes of these early tutorials. Later on we will examine how IDEs can facilitate the development process.

Note: In general you cannot use editors designed for word processing, such as Microsoft Word or LibreOffice Writer because these editors embed special formatting characters in the source files which the Java compiler cannot understand.

Java program file names must begin with a capital letter, contain letters or numbers, and have a .java filename extension. The contents of the source file must follow specific rules which we will discuss throughout these tutorials.


Several editions of Java exist to satisfy various needs:

Java SE
System Edition – For the most common general-purpose usage.
Java EE
Enterprise Edition – For business enterprise-level applications.
Java ME
Micro Edition – For embedded and small systems.

We will use Java SE for these tutorials.

Java programs are compiled with the javac compiler, which converts the source into Java bytecode. Bytecode, also known as p-code (portable code), is a form of instruction set designed for efficient execution by a software interpreter[1].
ACTION ITEM: Download the appropriate Java compiler (Mac OS X, Linux, Windows) for your computer from the Oracle website and install it on your computer. Oracle provides installation instructions for each operating system.

Note: The current version is Java SE 8. If you eventually intend to use Java for developing Android applications, then you will need the older Java SE 6 and a different Google development environment. All the programs that we develop in the tutorials can be compiled with the Oracle Java SE 6 or later.

To compile a program called, execute the java compiler from the command line, and specify the name of the program’s source file, for example:

The javac compiler converts the source code into Java bytecode – an intermediate representation of the program that contains low-level instructions that the Java Virtual Machine (JVM) will execute – and creates a new class file called Program1.class. The JVM is then used to execute the bytecode in the class file in the next step.

Note: You must include the file extension (.java) in the above command line.


Execute the program by passing the name of the newly created class file to the JVM application launcher, for example:

Note: Do not include the file extension (.class) in the above command line.

Example Program1

The Program Code

We will now create, compile, and run our first Java program. Use your text editor to enter the following code example and save it in a file called

[syntax_highlighter mode=”Java” height=”” font_size=”” line_height=”” wrap=”1″]JTJGJTJBJTIwUHJvZ3JhbTElMjBpcyUyMGElMjBwcm9ncmFtJTIwdGhhdCUyMGFkZHMlMjB0d28lMjBudW1iZXJzJTIwYW5kJTIwZGlzcGxheXMlMjB0aGUlMjBzdW0uJTIwJTJBJTJGJTBBY2xhc3MlMjBQcm9ncmFtMSUyMCU3QiUwQSUyMCUyMHB1YmxpYyUyMHN0YXRpYyUyMHZvaWQlMjBtYWluJTI4U3RyaW5nJTIwYXJncyU1QiU1RCUyOSUyMCU3QiUwQSUyMCUyMCUyMCUyMCUyMCUyMGludCUyMHN1bSUyMCUzRCUyMDIlMjAlMkIlMjAyJTNCJTBBJTIwJTIwJTIwJTIwJTIwJTIwU3lzdGVtLm91dC5wcmludGxuJTI4JTIyVGhlJTIwYW5zd2VyJTIwaXMlMjAlMjIlMjAlMkIlMjBzdW0lMjklM0IlMEElMjAlMjAlN0QlMEElN0Q=[/syntax_highlighter]

Our program in Notepad appears like this:

Program1 code
How our code looks in notepad.

Enter the command

at the Command Prompt to compile the program. Notice that a file called Program1.class is created by the Java compiler.

Then enter the command

to execute the program. The program runs and the output is displayed.

Program1 Output
Program1 Output

Understanding Our First Program

Our first program includes several important concepts that occur in all Java programs.

The first line

is known as a comment. Comments are often used by developers to explain the purpose of the program or a few lines in the program. Comments are not executed by the computer, but are very helpful to you or other developers who need to understand the program at a later date.

The next line of the program is:

This line says that a new class is being defined, and the name of the class is Program1. Classes will be described later, but for now just know that the contents of the class are enclosed between the opening curly brace ({) and the closing curly brace (}).

The next line of the program:

begins the main() method. This is the line where execution begins when you invoke the program with java Program1 at the command prompt. The contents of the main method are enclosed within the opening curly brace at the end of this line, and the closing curly brace three lines later.

The next line does two things. It declares a variable called sum to be an integer (ie., a whole number without a fraction), and stores the result of adding “2 + 2” in the variable. This line is a statement, and all Java statements are terminated by a semicolon. The previous lines did not end with a semicolon because technically speaking they are not statements.

The next line uses a system-defined class called System and a built-in method, println, to display the characters “The answer is ” and the contents of the variable sum on the screen. Notice the semicolon to terminate this statement.

The final two lines only contain closing curly braces, which designate the end of the main() method and the Program1 class, respectively.

Indentation and Whitespace

In most instances Java does not need or care about indentation or whitespace (spaces and tabs). However, good programming style suggests their use makes the code easier to read because related blocks of code, such as the contents of a method, are clearly identifiable. It is also permissible and suggested to use blank lines. Future examples will demonstrate good style.

Java Is Case-Sensitive

The Java language is case-sensitive, which means that uppercase letters (A-Z) and lowercase letters (a-z) are treated differently. The identifiers sum, Sum, and suM are all unique. Later we will examine numerous rules for naming classes, variables, and methods.

More About Comments

There are two types of comments that can occur in Java programs:

  • Single-line comments preceded with “//”

  • Multiline comments preceded with “/*” and terminated with “*/”

How to Learn Java

Java is not a difficult language, but it may seem frustrating at first because you have to know almost everything in order to do even the simplest thing. Do not let this discourage you. Use this strategy to accelerate your learning curve:

  • Read the first six tutorials and run the example programs within them.
  • Continue on with each tutorial even if you didn’t understand everything. This is normal. The first six tutorials will be rather dull reading – but they are essential. With this background many useful programs become possible.
  • Review the Java Snippets. Run them on your computer. Make changes to them and run them again. Every error you make will be a learning opportunity.
  • Go back and review the parts of the first six tutorials that you didn’t understand the first time.
  • Proceed with the remaining tutorials.

Over time and with repetition the strange syntax will become familiar.


In this first tutorial we learned the three steps to set up a Java development environment and run our first program. To follow along with subsequent tutorials, select a text editor (either provided by your operating system or one designed for writing programs), install the Java SE compiler and Java Virtual Machine, and try running Program1 on your computer.

Next Lesson

Next we will move on to Basic Syntax.

Leave a Reply