Scope of objects. Deleting objects. The advantages of using the “garbage collection” approach for objects

Scope of objects. Deleting objects. The advantages of using the “garbage collection” approach for objects


Contents


 1. How does Java define the scope of primitive type variables?

In the Java programming language, the scope of variables or objects is determined by the position of the curly braces { }. A variable that is declared within the scope is available only in this scope. For example.

{
    double x; // x is accessible
    x = 3.86;

    {
        double y; // x, y are accessible
        x = 5.55;
        y = 7.393;

        {
            double z = 3.88; // x, y, z are accessible
            x = 2.88;
            y = 33.22;
            z = 11.33;
        }

        // here z is not accessible
        //z = 29.99; // error
    }

    // here y is not accessible
    //y = 1000.05; // error
    x = 13.23; // x is accessible, correctly
}

 

2. Is it possible to “replace” the name of a variable of primitive type in the inner scope, which is declared in the outer scope?

In the Java language, it is not allowed to “replace” the name of the variable that is declared in the outer scope of the action.

For example. In the following code, a compilation error occurs.

{
    double x = 2.88; // correctly

    {
        double x; // error "Duplicate local variable x"
    }
}

In C/C ++, such a code will not cause an error.

 

3. Is it possible to give the same names to variables that are declared in different scopes?

Yes, It is.

For example. The following code declares variables with the same name, but which are placed in different areas of action. This code is correct.

{
    {
        double x = 2.88; // correctly
        System.out.println("x = " + x);
    }

    {
        double x; // also correctly
        x = 3.55;
        System.out.println("x = " + x);
    }

    System.out.println("OK!");
}

The result of the program

x = 2.88
x = 3.55
OK!

 

4. How is the scope defined for objects? What is the difference between the scope of primitive types and the scope of objects?

When declaring a reference to an object, the reference variable itself has an action area within curly braces { }. If for an object the memory has been allocated by the operator new, then the object itself will exist even after the variable-reference to it deletes.

For example. Let the class CPoint be given.

class CPoint
{
    private int x, y;
    public CPoint(int nx, int ny) { x = nx; y = ny; }
    public int GetX() { return x; }
    public int GetY() { return y; }
}

In the following code snippet, a reference to a CPoint class object is declared for which the memory is allocated by the ‘new’ operator.

{
    // ...

    {
        CPoint pt1 = new CPoint(5, 6); // pt1 refers to an instance of the object in which x = 5, y = 6
        int d = pt1.Get(); // d = 5
    } // the variables pt1, d disappear; and an object of type CPoint remains

    // here the object that was created in the scope of the action {} is still in memory
    // if there is no reference to it, it will be removed by the "garbage collector"

    // ...
}

The reference variable pt1 disappears after the curly closing bracket }. However, the object pointed to by the reference variable remains in memory. If there are no more references to it, it will be removed by the garbage collector. In the above code snippet, you can not access the object after destroying the variable pt1. Because, this single reference to the object has gone beyond visibility. If you want to keep access to the object, then in Java there are ways to copy object references. But that’s another topic.

 

5. What are the advantages of the “garbage collection” approach compared to the old approaches that were implemented in C/C ++?

The approach used in Java provides several advantages over C/C ++ programming languages. In C/C ++ programming languages, you need to manually delete memory for unnecessary objects created by the new operator. If this is not done, a memory leak will occur. In the Java programming language, you do not need to monitor the deletion of the allocated memory, this will all be done by the garbage collector. This, in turn, simplifies programming and reduces the risk of errors.

The “garbage collector” scans objects and determines those to which there are no references anymore. After that, it frees the memory allocated for these objects.


Related topics