Page 156 - ComputerScience_Class_11
P. 156
variableS aNd exPreSSioNS
07
Learning Objectives
7.1 Variables as Names for Values 7.2 Named Constants (final)
7.3 Operators 7.4 Precedence of Operators
7.5 Associativity of Operators 7.6 Arithmetical Expression and Statement
Variable is a name given to a specific location in memory where data can be stored. The value of a variable can change
during the execution of the program, depending on the logic of the code. Variables are essential in any program,
as they allow the program to store and manipulate data dynamically. In this lesson, we will explore the concept of
variables, how they are used to store values and how constants can be used when a value needs to remain fixed
throughout the program.
7.1 VARIABLES AS NAMES FOR VALUES
A variable can be thought of as a placeholder that holds a value in memory. The value stored in a variable can change at
different points in the program depending on the logic of the program. This ability to change the value makes variables
highly useful for handling dynamic data during execution.
For example, consider the following code:
int var = 0; // "var" is a variable of type integer, initialized with the value 0.
var = 5; // The value of "var" is updated to 5
In this example, the variable var is initially assigned the value 0, but later it is updated to 5. This demonstrates how a
variable’s value can change during execution, which is one of the key characteristics of variables in programming.
While naming a variable, it should follow certain protocols. Some of them are as follows:
• It must start with a letter, an underscore (_) or a dollar ($) sign.
• Apart from the first character, it can have any combination of characters including alphabets, digits and underscore
only.
• A variable name is case-sensitive, i.e., we can declare two different variables with different cases, e.g., int Sum, sum;
• It can be of any length.
• It cannot contain white space.
• Reserved words cannot be used as variables in Java.
154 Touchpad Computer Science (Ver. 3.0)-XI

