2.6. Memory Concepts

Variable names such as number1, number2 and sum actually correspond to locations in the computer’s memory. Every variable has a name, a type, a size and a value.

In the addition program of Fig. 2.7, when the following statement (line 18) executes

number1 = input.nextInt(); // read first number from user

the number typed by the user is placed into a memory location to which the compiler assigned the name number1. Suppose that the user enters 45. The computer places that integer value into location number1, as shown in Fig. 2.8. Whenever a value is placed in a memory location, the value replaces the previous value in that location. The previous value is lost.

Figure 2.8. Memory location showing the name and value of variable ...

Get Java™ How to Program, Seventh Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.