A variable can be thought of as a memory location that can hold values of a particular type. The value of a variable can change during the lifetime of the program, hence the name "variable".
2. Variables are held in memory in different formats depending on the type they are assigned
to, and these types have different sizes from each other. The software is generally designed to
process data and present the result to the user. However, the user can request data, or oper-
ations can be performed entirely on the values kept in memory. Variables, data are stored in
RAM (Random Access Memory) for a short time.
In real life, data is used for classification. Classification is
usually done according to the Types of Data.
Variables are structures that store temporary information in programming and are deter-
mined by the programmer. Variables form the basic structure of programming, and it is a struc-
ture used in all programming languages. Thanks to the variables, we keep the operations we
do in the program in memory and exploit them where necessary.
Variables have four properties; Data Type, Name, Value, and Address.
In Variables, Data Type, as the name suggests, indicates the type of data to be stored,
which can be numbers, words, and programmatic fields. Each variable has a unique character
from which it is used and called within the program. We assign a value to these variables; our
operating system keeps this variable in memory and sets an address.
Variables in Java have Data Types. These types can be primitive ones defined by default in
Java or non–primitive ones described by the developers themselves. Primitive types always have
value. Non–primitive types can be “null.” While non–primitive types all have the same size, the
size of primitive data types depends on the data type they will receive.
anumak.ai
3. In Classical Languages, basic Data Types such as integers, fractional numbers, boolean,
void, char, and composite Data Types such as string and array are built–in–types in the pro-
gramming language, each specified by a keyword. Whereas, in Object–Oriented Program-
ming Languages, each type is a class. However, primitive Data Types are used very often when
writing programs. Therefore, Object–Oriented Languages give an exception to such Data
Types. The Java language allows these data types to be used without creating objects belong-
ing to them. This makes it easier to write programs and speeds up.
The Java compiler automatically embeds (wrapping, boxing) each defined primitive data in
the class it belongs to, when necessary. Boxing puts a primitive variable into the category it be-
longs to and turns it into a variable belonging to an object. In other words, it is the conversion
of a primitive type to a reference type. Unboxing is the reverse process: Converting a boxed
variable back to a primitive data type. The java compiler does both things by itself, without the
programmer’s intervention.
Why Data Type?
We may need to handle different Data Types in a program. For example, integers, fraction-
al numbers, characters (letters and other symbols on the keyboard), text (string), logical (bool-
ean) values (true=true, false=false) are the different data types that come to mind first. These
additional data types differ in size (the number of bits they will occupy in memory) and the
operations that can be done. For example, we can do four functions with numbers but not texts.
Therefore, Java and some other languages separate data into types. Defining a variable speci-
fies which type of data it will hold. Thus, it allocates enough space for that variable in the main
memory and allows operations to be performed according to that Data Type.
anumak.ai
4. What is a Variable?
In a computer program, Variables are the names (identities) given to them to distinguish the
data used in the program from each other. Giving each data a separate name prevents them
from getting confused. It is the same in everyday life.
For compilers, it doesn’t matter whether the names given to the Variables are short or long,
meaningful or meaningless. However, it is appropriate to provide them with meaningful names
so that the source program writer and reader can easily understand what the Variables repre-
sent. Of course, this is not a problem in short programs, even single letter names are given, and
it is primarily convenient. But in extended programs, it is easy to understand and preferable to
name Variables with names that indicate the data they represent.
Technically speaking, a Variable is an address in the main memory
where values of a particular Data Type can be entered.
Variables are tools that carry the raw Data Types of the program. There are no global Vari-
ables in Java. Instead, each Variable is defined in a class or a block in the class. Those de-
scribed in the class become a class members of the class in which it is defined. Those defined
within an inner block within the type become the local Variable of that inner block.
Each Variable is allocated a place in the main memory large enough for the Data Type to
hold that Variable. This place is called the address of the Variable in the main memory. Each
Variable is given a name. Using this name, the Variable can be assigned a value, the set value
can be read, and the assigned value can be changed (updated). Accessing a Variable means
assigning a value to that Variable, reading the set value, and changing the given value at will.
Java is a type–protected language. In other words, the Java language separates the Data
Types to be held by the Variables to be used in the program with strict limits. In this respect,
it is similar to C and C++ languages. Therefore, when declaring a Variable, the Data Type of
that Variable is strictly specified. Then the data assigned to that variable cannot go outside the
selected data type. An exception to this is when, in some cases, the data held by a Variable is
converted to another type.
anumak.ai