Skip to main content
Advertisement

2.1 What is a Variable?

In mathematics, a variable is defined as a 'changing number', but its meaning in programming is slightly different.

1. Definition of a Variable

In programming, a variable refers to "a memory space capable of storing a single value."

Memory (RAM) is like numerous storage cabinets where data can be stored. To store data, we first need to secure a cabinet and attach a name tag to it so we can easily put in and take out data anytime. This name tag is the variable name.

2. Variable Declaration and Initialization

To use a variable, you must first 'declare' it by determining what kind of data it will store (type) and what name it will have (variable name).

How to Declare a Variable

int age; // Declares a variable named age of integer (int) type

When a variable is declared, a storage space of an appropriate size for the 'variable type' is secured in the empty space of memory, and this storage space can be used through the 'variable name'.

Variable Initialization

From the time a variable is declared, it can be used, but before that, it must be 'initialized'. Initialization means storing a value for the first time before using the variable.

int age;       // Variable declaration
age = 25; // Variable initialization (value is stored)

int year = 2024; // Declaration and initialization can be done simultaneously

3. Naming Conventions

All names used in programming, like variable names, are called 'identifiers', and the following rules must be followed when creating an identifier.

Mandatory Rules (cause errors if violated)

  1. Case-sensitive and no length limit.(True and true are different)
  2. Keywords cannot be used.(int, class, true, etc.)
  3. Cannot start with a number.(top10 is allowed, but 10top is not)
  4. Only _ and $ are allowed as special characters.($harp, S_ystem are allowed, S#arp is not)
  1. The first letter of a class name is always capitalized.
    • The first letter of variable and method names is always lowercase.
  2. For names consisting of multiple words, capitalize the first letter of each word. (Camel Case)
    • ex: lastIndexOf, stringBuffer
    • ex: PI, MAX_NUMBER

💡 Coding Tip: Swapping the Values of Two Variables

During programming, there are frequent situations where you need to cleanly exchange (Swap) the literal values stored natively inside two existing variables. The universally most common methodology heavily involves proactively preparing an additional 'empty cup (temporary variable)' strictly to temporarily preserve one of the values.

int x = 10;
int y = 20;
int temp; // Declare a variable dynamically to temporarily securely store a value

// Actively conduct the value swap
temp = x; // Preemptively copy and meticulously safeguard the vulnerable value of x directly into temp. (temp = 10)
x = y; // Aggressively overwrite exactly the previous value of x flawlessly with the designated value of y. (x = 20)
y = temp; // Confidently insert the securely preserved older value of x (currently inside temp) meticulously into y. (y = 10)

System.out.println("x:" + x + ", y:" + y); // Prints correctly -> x:20, y:10
Advertisement