Java Reference
In-Depth Information
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
 
Search WWH ::




Custom Search