Java: Data, Variable, Algorithm

Java: Data, Variable, Algorithm
Page content

Data and Variables in Java

Computer programs, including Java programs, are written to operate on data. For example:

• the data for an action game might be keys pressed or the position of the cursor when the mouse is clicked;

• the data for a word processing program are the keys pressed while you are typing a letter;

• the data for an accounting program would include, among other things, expenses and income;

• the data for a program that teaches Spanish could be an English word that you type in response to a question.

For a program to be run, it must be stored in the computer’s memory. When data is supplied to a program, that data is also stored in memory. Thus we think of memory as a place for holding programs and data. One of the nice things about programming in a high-level language (like C or Java) is that you don’t have to worry about which memory locations are used to store your data. But how do we refer to an item of data, given that there may be many data items in memory? We use a variable. In Java, data and variable go hand in hand.

Think of memory as a set of boxes (or storage locations). Each box can hold one item of data, for example, one number. We can give a name to a box, and we will be able to refer to that box by the given name. In our example, we will need two boxes, one to hold the side of the square and one to hold the area. We will call these boxes s and a, respectively.

If we wish, we can change the value in a box at any time; since the values can vary, s and a are called variable names, or simply variables. Thus a variable is a name associated with a particular memory location or, if you wish, it is a label for the memory location. We can speak of giving a variable a value, or setting a variable to a specific value, 1, say. Important points to remember are:

• a box can hold only one value at a time; if we put in a new value, the old one is lost;

• we must not assume that a box contains any value unless we specifically store a value in the box. In particular, we must not assume that the box contains 0.

Variables are a common feature of computer programs. It is very difficult to imagine what programming would be like without them. In everyday life, we often use variables. For example, we speak of an ‘address’. Here, ‘address’ is a variable whose value depends on the person under consideration. Other common variables are telephone number, name of school, subject, size of population, type of car, television model, etc. (What are some possible values of these variables?)

Now that we know a little bit about variables, we are ready to develop the algorithm for calculating the area of a square.

Develop the algorithm (continued)

Using the notion of an algorithm and the concept of a variable, we develop the following algorithm for calculating the area of a square, given one side (click the image to enlarge):

Algorithm for calculating area of square, given one side

(1) Ask the user for the length of a side

(2) Store the value in the box s

(3) Calculate the area of the square (s x s)

(4) Store the area in the box a

(5) Print the value in box a, appropriately labelled

(6) Stop

When an algorithm is developed, it must be checked to make sure that it is doing its intended job correctly. We can test an algorithm by ‘playing computer’, that is, we execute the instructions by hand, using appropriate data values. This process is called dry running or desk checking the algorithm. It is used to pinpoint any errors in logic before the computer program is actually written. We should never start to write programming code unless we are confident that the algorithm is correct. Here, the algorithm is fairly simple, so it is easy to check that it is indeed correct.

In the next article, Java Example: Algorithm and Program For Area Of Square, we show how to write the program to implement this algorithm.


Java Programming – A Beginner’s Course

Related programming references

Article: Java: Data, Variable and Algorithm Explained To A Beginner

This post is part of the series: Introduction to Java Programming

This is a series which aims to discuss and teach Java programming to the complete beginner. Absolutely no programming background is assumed.

  1. Introduction to Java Programming - An Overview
  2. Java - Data, Variable and Algorithm Explained To A Beginner
  3. Java Example: Algorithm and Program For Area of Square
  4. Java Programming For Beginners - Test, Debug, Document, Maintain
  5. JDK Java Compiler: The Java Development Kit
  6. Java Programming For Beginners - How To Compile And Run Java Programs
  7. Data Types, Constants And Variables
  8. Java Programming For Beginners - Characters and printf
  9. Java Programming For Beginners - Part 9
  10. Java Programming For Beginners - Part 10
  11. Java Programming For Beginners - Part 11
  12. Java Programming For Beginners - Part 12
  13. Java Programming For Beginners - Part 13
  14. Java Programming For Beginners - Part 14
  15. Java Programming For Beginners - Integer Data Types
  16. Java Programming for Beginners - Part 16
  17. Java Integer Arithmetic For Beginners
  18. Java Programming For Beginners - Part 18
  19. Java Programming For Beginners - Part 19
  20. Java double to int and Other Conversions