Like any student beginning their first programming course, you’re probably eager to dive right in and write your first program. And you’re in luck — this chapter will get you started with coding right away. Writing and designing computer programs can be incredibly rewarding and fun, though it might feel a bit intimidating at first since it’s likely unlike anything you’ve done before. But stick with it, and not only will you start to enjoy it, you might even find yourself staying up late, lost in solving a problem. You’ll know you’re hooked when the solution suddenly hits you just as you’re dozing off, and you’re back at the keyboard before the sun comes up. If that sounds familiar — don’t worry, it’s completely normal!
Before jumping into writing code, though, it’s important to understand a few key terms like program, software, code, and programming languages.
Software
A computer isn’t very useful on its own — it needs instructions to tell it what to do. This set of instructions is called a program. Programs can be stored in various places, such as electronic chips within the computer or external storage devices like hard drives, CDs, DVDs, and USB drives. Many programs can also be downloaded from the internet.
The term software refers to a single program or a collection of programs. There are two main types of software:
- Application software: These are programs designed for users to perform specific tasks, such as word processors, spreadsheets, accounting tools, or games. These are often simply called applications.
- System software: These are specialized programs that help the computer function. Examples include operating systems (like Windows™ or UNIX™) and network software that allows computers to communicate.
Software isn’t just for traditional computers. Many modern devices — such as smartphones, microwaves, and gaming consoles — rely on built-in programs known as embedded software.
Whether it’s application, system, or embedded software, all of it is created by writing a series of instructions the computer can follow. This process is called programming or coding. These instructions are written in special languages known as programming languages, such as C++, Python, Visual Basic, and many others.
In this book, we’ll be using Java, which is an example of an object-oriented programming language. Don’t worry if that term sounds unfamiliar right now — you’ll learn what it means as we move forward.
Compiling programs
Like most modern programming languages, Java uses instructions that resemble English. Words like while
and if
are part of the Java language. The collection of these instructions written in a programming language is called program code or source code.
However, computers don’t understand this high-level code directly — they only understand binary instructions, made up of 0s and 1s. For example, a machine might interpret 01100111
as the instruction to add. This binary language is known as machine code.
To bridge the gap, a special system software called a compiler is used. It translates the source code into machine code that the computer can execute. This translation process is known as compiling. Figure 1.1 shows how this process works in many programming languages.
Like natural languages, programming languages follow a strict set of rules known as syntax. If your code contains syntax errors — such as using incorrect keywords, missing brackets, or forgetting semicolons — it won’t compile. But if your source code is syntactically correct, the compiler will convert it into machine code that the computer can run.

Once a program has been compiled and the machine code has been saved, it can be run on the target computer as many times as needed. When you purchase software like a game or a word processor, what you’re actually buying is this compiled machine code version of the program.
Programming in Java
Before Java came along, most programs were compiled in a way that made them work only on a specific type of computer, as shown in Figure 1.1. For instance, a program compiled for a Windows PC wouldn’t run on a Mac™ or a UNIX™ system. This lack of flexibility was a major limitation.
Java changed that. It is platform-independent, meaning a Java program can run on any type of computer, regardless of the operating system or hardware. This is possible because of a special program called the Java Virtual Machine (JVM).
To run a Java program, the target machine must have a JVM installed. There are different versions of the JVM available for different platforms — for example, one for Windows PCs, one for Macs™, another for UNIX™ or Linux™, and even versions for mobile phones and embedded systems.
Traditional compilers convert code directly into machine code tailored for a specific computer. Java compilers, however, convert the code into Java bytecode — a universal set of instructions made up of 0s and 1s, just like machine code, but not specific to any one type of system.
The JVM’s role is to take this bytecode and translate it into machine-specific instructions in real-time, so the program can run correctly on whichever device it’s on. This is what gives Java its powerful cross-platform capability.

There are several ways to install a Java Virtual Machine (JVM) on a computer. Some operating systems include a JVM by default, along with Java libraries (also known as packages), which are pre-compiled modules you can use in your own programs, and a compiler. Together, the JVM and these libraries make up the Java Runtime Environment (JRE).
If your system doesn’t already have a JRE installed — which is often the case with Windows™ — you can download the full Java Development Kit (JDK). The JDK includes the JRE, the compiler, and additional development tools. It’s available from Oracle™, the official maintainers of the Java platform.
Integrated Development Environments (IDEs)
It’s very common to write, compile, and run your programs using a tool called an Integrated Development Environment (IDE). An IDE offers a user-friendly interface where you can type your code, view the files you’re working with, and see error messages in a dedicated window.
In addition to simplifying code writing and error tracking, an IDE allows you to compile and run your programs with just a click. Depending on which IDE you’re using, your screen layout might look similar to the one shown in the figure.

The IDE shown in Figure above is NetBeans™, one of the most commonly used IDEs for Java. Another popular choice is Eclipse™. These environments offer menus for tasks like compiling, running, and saving programs, a code editor for typing your Java source code, windows for file management, and a console area that displays output and error messages.
While it’s entirely possible to compile and run Java programs without using an IDE—by working from the command line—it’s far less convenient. In this case, you write your code in a plain text file with a .java
extension.
To compile a program named MyProgram.java
, you’d use the Java compiler included in the JDK, called javac.exe
, by typing:
javac MyProgram.java
This generates a MyProgram.class
file containing Java bytecode. To run it, you’d use the Java Virtual Machine (java.exe) with the command:
java MyProgram
That said, especially when you’re just starting out, it’s highly recommended to use an IDE like NetBeans™ or Eclipse™ for a much smoother and more beginner-friendly experience.
Java applications
As we discussed in section 1.2, Java applications can run on various devices, including computers, mobile phones, game consoles, and even embedded systems. In the case of embedded software, you may not even be aware that a program is running, while on other devices like computers and mobile phones, you’ll see the program’s output on a screen and interact with it using a keyboard, mouse, touchscreen, or game controller.
The screen that displays the program’s output and allows you to input data is called the user interface. There are two main types of user interfaces:
- Text-based user interface: Information is displayed as text, without images or graphics. User input is typically provided through the keyboard. These programs are known as console applications.
- Graphics-based user interface: This interface includes visual elements like buttons, icons, and images, providing a more interactive experience.
If you’re using an IDE, the console window (where you interact with text-based programs) is usually integrated within the IDE, as shown in Figure 1.3. However, if you’re running a program from the command prompt, you’ll see a similar window, which provides a text-based interface for interacting with your program.

You’re likely more familiar with programs that have a graphical user interface (GUI). GUIs allow pictures, shapes, and various elements like text boxes and buttons to be displayed on the screen. They also use the mouse, along with the keyboard, to gather input from the user. A typical example of a GUI is shown in the figure.

Eventually, the goal is for all of your programs to have graphical user interfaces (GUIs), but creating them takes more programming effort than simple console applications. So, during the first semester, while we’re focusing on teaching you the fundamentals of programming in Java, we’ll stick to console-based applications. Once you’ve mastered the basics, though, you’ll be ready to create visually appealing graphical interfaces by the end of this semester.
Self-test Questions
- Explain the meaning of the following terms:
- Program: A set of instructions written in a programming language that tells a computer what to do.
- Software: A general term for computer programs and the associated data that enable the computer to perform specific tasks.
- Application Software: Programs designed to help the user perform specific tasks, such as word processors, games, or spreadsheets.
- System Software: Programs that help manage the computer’s hardware and resources, such as operating systems and network software.
- Machine Code: The low-level binary instructions (0s and 1s) that a computer can directly execute.
- Source Code: The original code written by a programmer in a high-level programming language like Java, before being compiled.
- Embedded Software: Software that is built into hardware devices, such as microwaves, mobile phones, or smart TVs, and operates the device.
- Compilation: The process of converting source code into machine-readable code, typically Java bytecode, so that it can be executed by the computer.
- Java Bytecode: A platform-independent, intermediate representation of Java source code, which can be run by the Java Virtual Machine (JVM).
- Java Virtual Machine (JVM): A special program that runs Java bytecode, translating it into machine-specific instructions so the program can run on any platform.
- Integrated Development Environment (IDE): A software application that provides tools for writing, compiling, debugging, and running programs, offering a user-friendly interface.
- Explain how Java programs are compiled and run:
- Java programs are written in a high-level language and saved as
.java
files (source code). - The Java compiler (
javac
) translates the.java
file into Java bytecode, which is saved as a.class
file. - This bytecode is platform-independent and can run on any computer that has the Java Virtual Machine (JVM) installed.
- The JVM reads the bytecode and translates it into machine-specific instructions to execute the program on the given system.
- Java programs are written in a high-level language and saved as
Programming Exercises
- If you do not have access to a Java IDE, go to the accompanying website and follow the instructions for installing an IDE. You will also find instructions on the website for compiling and running programs.
- Follow the specific instructions provided for installing a Java IDE like NetBeans™ or Eclipse™ on your system.
- Once installed, you can begin writing, compiling, and running your Java programs directly from the IDE, or use the command line to compile and run Java programs manually.
***END OF THE TOPIC***