Page 93 - 2617_JSSPS_C-7
P. 93
Primitive Data Type Non-Primitive Data Type
There are 8 Primitive data types. There are mainly 4 types of non-Primitive data
types.
To use them “new” operator is not required. Use of “new” operator is mandatory for
creating instances.
The size of a primitive type depends on the Size of non-primitive types may vary.
data type.
Primitive data types hold only single values. The non-primitive data types are used to store
the group of values.
VARIABLE
A variable is a name given to a space in computer memory to store a value. In other words, it is a reference
to a memory location where data is held. The term "variable" comes from the combination of "vary" and
"able," meaning its value can change during the program's execution. Variables can take different values
at various points, depending on the logic of the program. You might be wondering, what is the difference
between an identifier and a variable. An identifier is a name used to uniquely identify an entity (such as
a function, class or variable) in the program. It is a general term that encompasses variables, functions,
constants and more. On the other hand, a variable is a specific type of identifier that refers to a memory
location used to store data that can change as the program runs.
Declaring a Variable
Before using a variable in Java, it must first be declared. If a variable is not declared, it cannot be used
and attempting to do so will result in a compile-time error.
The syntax for declaring a variable is as follows:
<data type> <name of the variable>;
Where, the <data type> specifies any primitive or non-primitive data type and the name of the variable
should be a valid Java identifier. For example, int age; Here, int is the data type and the age is the
variable name. As you know, that every statement is terminated with the help of a semicolon. Similarly,
the declaration statement is also terminated by using a semicolon.
For example:
int age;
Here, int is the data type and age is the variable name.
You can also declare multiple variables of the same type in a single statement, separating them with
commas.
For example:
int age, marks;
Java Programming 91

