O'Reilly logo

Learning Java Through Games by Lubomir Stanchev

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 2
Data Types and Conditional Statements
2.1 Our First Java Program ................................................................ 11
2.2 Variables ................................................................................ 16
2.3 Random Numbers and the if Statement ............................................... 23
2.4 Combining Conditions .................................................................. 27
2.5 The String Class ....................................................................... 29
2.6 The switch Statement .................................................................. 32
2.7 The Conditional Operator .............................................................. 33
2.8 Summary ................................................................................ 35
2.9 Syntax .................................................................................. 35
2.10 Important Points ....................................................................... 37
2.11 Exercises ................................................................................ 38
2.12 Lab ...................................................................................... 39
The chapter introduces the basic structure of a Java program. The main method is where
the program starts executing. For now, we will use the console for reading and displaying
data. Most programs will read data from the console and save it in main memory. The
memory in a Java program is accessed through variables. These variables can be used to
store integers, real numbers, and strings, among others. The chapter shows how to read
data from the keyboard, save it in variables, manipulate the data, and then display the
result. The chapter also shows how the keywords if and switch canbeusedtointerrupt
the sequential execution of a program.
2.1 Our First Java Program
Java code can be written using any editor, including Notepad. However, using an Inte-
grated Development Environment (IDE) simplifies things. For example, an IDE can highlight
in red Java code that has a syntactic error. It can also auto-format the code for us and can
execute our code in a single click of the mouse. Lastly, an IDE gives us the ability to debug
the code. Debugging allows us to execute Java code line by line. This allows one to monitor
how the data changes as the program executes and helps isolate errors. This section will
provide a quick overview of NetBeans, which is an IDE that is supported by Oracle, the
same company that supports the Java language.
In most programming textbooks, the very first program displays “Hello World!” Let
us follow this tradition and see how we can write this program in NetBeans. Before you
continue reading, make sure you download and install the latest version of Java and then
the latest version of NetBeans. Luckily, both Java and NetBeans are freely available for
most operating systems. Next, start NetBeans and use the menu to create a new project.
Select Java Application; see Figure 2.1.
On the next screen, see Figure 2.2, type in the name of the project, for example,
HelloWorld. Under Project Location, NetBeans will show you the folder where the IDE
will store the files. Make sure Create Main Class is not selected. If it is, then NetBeans
11
12 Learning Java through Games
FIGURE 2.1: Creating a new Java project.
will create some default code. However, we will write all the code from scratch. Also, please
make sure that Set As Main Project is selected. This will guarantee that the new project
is the main project (i.e., the project that will be executed when the green forward arrow is
clicked). As a last step, click the Finish button.
Next, we need to create the Java files. For now, our code will consist of a single file.
In the project menu, expand the project HelloWorld and right-click on Source Packages.
Select New and then Java Class.TypeMain as the name of the class. Enter helloworld
as the package name; see Figure 2.3. Click the Finish button.
A new file is created. It should contain roughly the following text.
package helloworld ;
public c la ss Main {
}
The only difference between this file and the file you will see will be the comments.
Comments are usually written in a natural language, such as English. They are meant to be
read by other humans and not by computers. Comments have no effect on how the program
is executed. There are two ways to create comments. A one-line comment is created by
typing // followed by some text description. Consider the following code.
package helloworld ;
public c la ss Main {
public s ta tic void main( String args [] ) { // sta rt o f program
}
}
Data Types and Conditional Statements 13
FIGURE 2.2: Setting the properties of a Java Application project.
FIGURE 2.3: Creating a new Java class.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required