Once a variable is declared, it must be explicitly initialized using an assignment operator, since a variable that has not been assigned a value cannot be used.
To assign a previously declared variable a specific value, you need to indicate its name on the left, put an equal sign (
=
), and write some expression in Java that specifies the required value on the right. Eg:
int daysInMay; // объявление целочисленной переменной
daysInMay = 31; // оператор присваивания значения этой переменной
Here is an example of assigning a value to a symbolic variable:
char noChar;
noChar = 'N';
The Java language has the convenient ability to combine variable declaration and initialization on the same line.
int daysInMay = 31; // Пример объявления и инициализации переменной в одной строке
Also in Java, a variable declaration can be placed anywhere in the code, for example, the code below works:
int daysInJune = 30;
System.out.println(daysInJune);
int daysInMay = 31;
It is clear that in one scope it is impossible to declare two variables with the same names. The C and C++ languages differ in the declaration and definition of a variable. Here is an example of a variable definition:
In Java, variable declarations and definitions are the same. Link to source: Assignment and initializationint i = 100; А вот пример ее объявления:
extern int i;
GO TO FULL VERSION