Chapter 2.2.1 – Variables in java | Introduction to Programming Using Java

Chapter 2.2.1 – Variables in java| Introduction to Programming Using Java

 

2.2.1 Variables in java

 

Programs manipulate data that are stored in memory. In machine language, data can only be referred to by giving the numerical address of the location in memory where it is stored. In a high-level language such as Java, names are used instead of numbers to refer to data. It is the job of the computer to keep track of where in memory the data is actually stored; the programmer only has to remember the name. A name used in this way—to refer to data stored in memory—is called a variable.

 

Chapter 2.2.1 - Variables in java| Introduction to Programming Using Java

 

Variables are actually rather subtle. Properly speaking, a variable is not a name for the data itself but for a location in memory that can hold data. You should think of a variable as a container or box where you can store data that you will need to use later. The variable refers directly to the box and only indirectly to the data in the box. Since the data in the box can change, a variable can refer to different data values at different times during the execution of the program, but it always refers to the same box.

Confusion can arise, especially for beginning programmers, because when a variable is used in a program in certain ways, it refers to the container, but when it is used in other ways, it refers to the data in the container. You’ll see examples of both cases below.

(In this way, a variable is something like the title, “The President of the United States.” This title can refer to different people at different times, but it always refers to the same office.

If I say “the President went fishing,” I mean that George W. Bush went fishing. But if I say “Hillary Clinton wants to be President” I mean that she wants to fill the office, not that she wants to be George Bush.)

 

Chapter 2.2.1 - Variables in java| Introduction to Programming Using Java

 

In Java, the only way to get data into a variable—that is, into the box that the variable names—is with an assignment statement. An assignment statement takes the form:

hvariablei = hexpressioni;

where hexpressioni represents anything that refers to or computes a data value. When the computer comes to an assignment statement in the course of executing a program, it evaluates the expression and puts the resulting data value into the variable. For example, consider the simple assignment statement rate = 0.07;

The hvariablei in this assignment statement is rate, and the hexpressioni is the number 0.07. The computer executes this assignment statement by putting the number 0.07 in the variable rate, replacing whatever was there before. Now, consider the following more complicated assignment statement, which might come later in the same program:

interest = rate * principal;

 

Chapter 2.2.1 - Variables in java| Introduction to Programming Using Java

 

Here, the value of the expression “rate * principal” is being assigned to the variable interest. In the expression, the * is a “multiplication operator” that tells the computer to multiply rate times principal. The names rate and principal are themselves variables, and it is really the values stored in those variables that are to be multiplied. We see that when a variable is used in an expression, it is the value stored in the variable that matters; in this case, the variable seems to refer to the data in the box, rather than to the box itself.

When the computer executes this assignment statement, it takes the value of rate, multiplies it by the value of principal, and stores the answer in the box referred to by interest. When a variable is used on the left-hand side of an assignment statement, it refers to the box that is named by the variable.

(Note, by the way, that an assignment statement is a command that is executed by the computer at a certain time. It is not a statement of fact. For example, suppose a program includes the statement “rate = 0.07;”. If the statement “interest = rate * principal;” is executed later in the program, can we say that the principal is multiplied by 0.07? No! The value of rate might have been changed in the meantime by another statement.

 

Chapter 2.2.1 - Variables in java| Introduction to Programming Using Java

 

The meaning of an assignment statement is completely different from the meaning of an equation in mathematics, even though both use the symbol “=”.)

 

 

 

 

Read More…

Introduction to Programming Using Java – David J. Eck

1. The Mental Landscape । Programming Using Java

Chapter 1.2 – Asynchronous Events | Introduction to Programming Using Java

Chapter 2 – Names and Things | Introduction to Programming Using Java

Chapter 2.2 – Variables and the Primitive Types | Introduction to Programming Using Java

2 thoughts on “Chapter 2.2.1 – Variables in java | Introduction to Programming Using Java”

Leave a Comment