Prerequisite Information
This chapter requires a basic understanding of variables, classes, and objects in the Java programming language. Before starting this tutorial, please make sure to watch the CSCI 1301 review videos on eLC under Content → 1301 Videos. Specifically, videos 14–22 will help you understand these concepts.
1.1. Introduction¶
The goal of this chapter is to provide readers with a concise review
of important object-oriented programming language concepts using
familiar terminology. Some terms may already be familiar to some
readers, but it’s essential to understand how they relate to other
Java concepts and how they differ from their meanings in other
contexts or languages. Consider the English verb “print,” a word that
is spoken and written by both programmers and non-programmers alike.
In the early days of computing, the primary output device for a
computer was typically a printer that applied ink to one or more
pieces of paper, much like the printer devices that exist today. As
such, programmers and non-programmers at the time would both expect a
request to “print a message” to ultimately result in that message
appearing on a piece of paper. Although screens have now replaced
printers as the primary output device for most computers, most
programmers still use the verb “print” to mean “display on the primary
output device,” which is why we use methods like print
and
println
to display text.
System.out.println("THIS WILL APPEAR ON THE SCREEN!");
Readers must also take special care to understand the differences between terms that appear to be similar. For example, variables, which are discussed in detail in the next section, are a very important part of most Java programs, and most readers with some background in programming can set up a variable and use it in their code. That being said, consider the terms “primitive type variable” and “reference type variable.” Each term describes a kind of variable, but the terms are not interchangeable.
When used to describe a Java variable, “primitive type” and “reference type” each introduce additional context that limits the set of potential values that a variable can store, determines the language syntax that is supported when using the variable value, and provides insight into how the variable’s value and related information will be stored in the computer’s memory at runtime. These additional nuances are so important that a programmer would not want to confuse one kind of variable for the other, because doing so will likely prevent their code from compiling or worse, lead to runtime errors.
1.2. Variables¶
In computer programming, a variable is just another name (alias)
for a location in memory. Since memory addresses are often written in
hexadecimal (e.g. 0x012f47ab
), modern programming languages allow
programmers to use variables instead of requiring them to remember
explicit memory addresses.
When we declare a variable in Java, we must provide both a name and a type. Here are two examples of variable declarations:
// variable declarations
int x;
double[] myArray;
Notice that you don’t see an equals sign (=
) in the code above. The
equals sign is used to assign a value to a variable. The first time
you assign a value to the variable, it is called initializing the
variable.
// variable initializations
x = 7;
myArray = new double[10];
Of course, we could combine declarations and initializations in the following way:
// declaring and initializing variables in a single line
int x = 7;
double[] myArray = new double[5];
You should be able to identify a variable’s:
value, the actual data stored in the memory location;
type, the attribute that tells the computer how to interpret the value and how much space is needed to store the value; and
name, an attribute that tells the computer the word that we want to use to refer to location in memory where the variable’s value is stored.
Test Yourself
Take a moment to identify the value, type, and name of the variables declared and initialized below. Before looking ahead, write your answers in your notes.
int x = 17;
String s = "Hello!";
Test Yourself Solution (Don’t open until completing the question above)
Did you come up with the answers below?
Declaration/Initialization |
Value |
Type |
Name |
---|---|---|---|
|
|
|
|
|
|
|
|
Notice the type of the two variables above. The first, int
, is
a primitive type and the second, String
, is a reference
type. You can immediately tell that String
is a reference type
by the fact that it start with a capital letter. These two variables
both contain values but they work very differently under the hood. In
this chapter, we will demonstrate the important differences between
these two types of variables.