What does variable mean in Java


Data types and variables

Variables can be thought of as a container for changing data. For those who have never heard of variables before, a comparison with a bucket may be of the best help. The bucket can also have different contents, e.g. sand or water. It is the same with the variables. For example, they can contain numbers or strings. Since Java is a strictly typed language, the declaration of each variable must include the Data type can be specified. The data types determine which values ​​the variable can assume. For our bucket this would mean that a sign with the words "sand" or "water" would have to be stuck on it.

The declaration a variable is done in Java as follows:

Data type variable name;// variable declaration

So we assign a value to the variable. The first value assignment is saved as a Initialization designated. This is necessary for complex data types so that you can work with the variable. If the variable is accessed without initialization, an error message is output. Simple data types do not necessarily need an initialization, since they are initialized by the compiler with the value 0 by default. However, it is advisable to initialize each variable as early as possible.

Variable name = value; //Initialization

The declaration and the initialization are often done in one step:

Data type variable name = value; / * Declaration and initialization * /


One differentiates in Java primitive (also known as simple or elementary) and complex data types.

There are eight in Java primitive data types:

  • The simplest data type is boolean. An attribute of the data type boolean can have exactly two values: true (true) and false (false). The boolean data type is very often used for conditional branches, which we will deal with in a later chapter.
  • Another simple data type is the so-called Character, in Java by the data type char is pictured. An attribute of this data type can contain all Unicode characters. The maximum length is 16 bits. The range of values ​​is from 0 to 65535.
  • The data type byte is a signed integer. The range of values ​​(8 bits) is from -128 (-2 ^ 7) to +127 ((2 ^ 7) -1). This data type is widely used in the field of data file processing or graphic data processing. So-called byte arrays are often used for this. Arrays are covered in detail in a later chapter.
  • Another integer data type in Java is called short. The range of values ​​(16 bits) here is twice as large as that of the byte data type. Expressed in decimal terms, the signed one starts at -32768 (-2 ^ 15) and ends at 32767 ((2 ^ 15) -1).
  • As int Another signed integer data type is referred to. The size of an int value is 32 bits. It covers a range of values ​​from decimal -2147483648 (-2 ^ 31) to 2147483647 (2 ^ 31 -1).
  • The largest signed integer data type is called in Java long. This data type is twice as large as its predecessor and comprises 64 bits. The range of values ​​is therefore between -9223372036854775808 (-2 ^ 63) and 9223372036854775807 (2 ^ 63 -1).
  • There are two data types for floating point numbers in Java. The smaller floating point data type is called float. It has a size of 32 bits and the range of values ​​is from -3.4 * 10 ^ 38 to 3.4 * 10 ^ 38.
  • The larger floating point data type is called double designated. Double has a size of 64 bits and the range of values ​​extends from -1.7 * 10 ^ 308 to 1.7 * 10 ^ 308.
int varNumber = 23; // declaration of the variable "varNumber"


Later we want to change the value of the variable to 42:

varZahl = 42; / * Variable "varZahl" is assigned the value 42 * /

In addition to the primitive data types, there are also complex data types. Any class that has multiple attributes defines a complex data type. They are called complex because, in contrast to the primitive data types, they are composed of different elements. One of the best-known complex data types is the string data type. The string data type represents character strings. We will go into more detail about complex data types later.