![]() | Session 1 | ![]() |
Data types
As many other programming languages, Java uses variables to represent values.
PS! Pay attention, the variable names:
- the first character is always written in lower case when using the camel case notation;
- must not contain special characters or spaces;
- must not start with numbers;
- cannot be composed solely of numbers.
Variables must be declared before they are used.
Unlike Python, you must tell Java what type of data the variable will store. For example, if we need to use a variable which contains an integer, we have to define the variable data type and the variable name as follows:
int var;
This declares a variable and its data type as well as reserves memory for it. However, the statement says nothing about the value. To do so, an assignment statement = must be used:
var = 5;
The previous two lines can be merged:
int var = 5;
We can also declare two variables of the same data type, reserve memory, and put an initial value in each variable as well:
int var1 = 5, var2 = 15;
PS! We can declare a variable once. If we try to do it twice, Java will through out an error.
In general, there are eight primitive data types in Java:
| Data type | Range |
|---|---|
byte | -128 to 127 |
short | -32 768 to 32 767 |
int | -231 to 231 |
long | -263 to 263 |
float | -3.4x1038 to 34x1038 |
double | -1.8x10308 to 1.8x10308 |
boolean | true or false |
char | any symbol represented in 16-bit Unicode from '\u0000' to '\uFFFF'. |
PS! Strings are not primitive data type in Java. It is an instance of class String.
![]() | Session 1 | ![]() |

