Variables. Declaring variables. Initialization of variables

Variables. Declaring variables. Initialization of variables


1. What purpose have variables in Java programs?

The variables used for saving the data in the program. The variable – a named cell of memory that stores data. During the program development, the programmer has the ability to use any number of variables needed to solve the problem.

The variables are used for the calculation of expressions. The names of variables can be specified at its own discretion. The variable name must be specified according to the rules for specify names of identifiers, which are described in detail here.

It is desirable that the variable name corresponded to its intended purpose in the program. For example, if a variable is used to store a minimum value, then it can be the name “Min”.

In Java every variable must have its own type. The type can be:

Each variable has its own scope.


2. What is the variable declaration in the program?

Before using, each variable must be declared. The general form of a variable declaration is:

type identifier [= value] [, identifier [= value] ...];


type – one of the primitive data types, class or interface;

identifier – the variable name;

[= value] – the initial value (initialization), which is assigned to the variable when it is declared.

Variables of this type can be described by a few.


3. Examples of the declaration and initialization of variables of primitive types


int a;     // declaring of the variable with the name of a type int
int b = 6; // the declaration of the variable b and its initialization by the value 6
byte t = 0;
double pi = 3.1415; // description and initializing a variable pi of type double
double x = 0, y, z = 5; // description of 3 variables, initialization of variables x and z
boolean b1 = true, b2;
char c = 'A';
char c1, c2, c3 = 'Z';
String s1; // description s1 variable with a name of the "String" class
String s2 = "This is a string"; // declaring s2 string and its initialization

// the use of variables after describing
a = 8;
y = 0.338;
b2 = false;
c1 = c2 = '5';
s1 = s2 + "!"; // s1 = "This is a string!"


4. Example of declaring and initializing the variable that is an instance (object) of the class



// class definition
class MyPoint
    int x;
    int y;


// use in program
// variable definition
MyPoint mp;
mp = new MyPoint();
mp.x = 23;
mp.y = -10;

// and so can
MyPoint mp2 = new MyPoint();
mp2.x = 5;
mp2.y = -32;



5. Example of a variable, which is the interface


// declare the MyInterface interface
interface MyInterface
    // declare the interface method
    void SetXY(int xx, int yy);

// declare MyPoint class
class MyPoint implements MyInterface
    int x; // internal variable x
    int y; // internal variable y

    // interface method implementation
    public void SetXY(int xx, int yy)
        x = xx;
        y = yy;

    // the methods of class
    public int GetX()
        return x;

    public int GetY()
        return y;


// using the interface and class in the program
MyInterface mi = new MyPoint(); // variable definition - references to the interface

MyPoint mp = new MyPoint(); // variable definition - the class object

// use the variable mi, which is a reference to the interface
mi = mp;



6. What is a dynamic variable initialization? An example of a dynamic variable initialization

In dynamic initialization, for the variable is assigned the value via an expression, which at the moment is really (the moment of variable declaration).

Example. Calculating the area of a triangle using the formula of Heron. Heron’s formula is as follows:

where p – semiperimeter:

a, b, c – the lengths of the sides of the triangle.

In the example below, when the variables p and s are declared, is carried their dynamic initialization according to the above formulas.

A fragment of code that solves the following problem:


// Calculating the area of a triangle using the formula of Heron
// the lengths of the sides of the triangle are set
double a = 3.5, b = 3.6, c = 2.8; // ordinary initialization

// the calculation of semiperimeter
double p = (a+b+c)/2; // dynamic initialization

// area calculation
double s = Math.sqrt(p*(p-a)*(p-b)*(p-c)); // again dynamic initialization


At the time of the variable p definition, variables a, b, c have been defined and identified (have a value). Also at the time of the variable s defining, variables a, b, c, p have been defined. Therefore, is possible to use dynamic initialization in the definition of these variables.


7. What is the scope and lifetime of a variable? Example.

The scope of a variable is determined by the code block in which it is declared. The code block may be in a method. Furthermore, in the method can be parameters that also have scope of the code block. The code block of method begins from the opening brace.

The code block is limited by by braces:


    // code block
    // ...


Scopes can be nested. With each new block of code is created a new scope of the variable. The external scope includes an internal scope.

Example of a variable in the code block and outside.


    // the code block
    double d = 0; // definition of variable d
    d = 5; // this code block has access to the variable d

        // one more nested block of code
        int x; // the definition of the variable x
        d = 8; // the variable d is accessible here
        x = 3; // the variable x is accessible here
    x = 10; // error! the variable x is not available outside the block of code

d = 5; // error! Here variable d is not available


Variables can be declared anywhere in the code block. However, the variable is possible to use after it is declared. The variable can not be used before its declaring.

The lifetime of a variable is determined by its scope (visibility). Variable is destroyed immediately after the end of scope.