CLASSES IN JAVA

GARBAGE COLLECTOR

java-logo

In this lesson we will look at how memory is managed in Java. In the previous post we saw how creating an instance causes dynamic memory allocation. Memory is associated with a variable that contains a reference to the instance created.

Allocazione memoria

THE REFERENCE COUNT

Behind the scenes, each object has an associated value called Reference Count, which is used to count how many references there are to the object.

Reference Count

We have seen that because of the assignment, some objects are orphaned, that is, they no longer have references.

Oggetti orfani

The Reference Count for some objects goes up while for others it goes down to zero. Think of a program that has thousands of references, the memory is not infinite is due to overuse the program may stop working. It is at this point that the Garbage Collector comes into action.

Garbage Collector

Every now and then during the execution of a program in Java, this cleaning engine is activated that checks the objects in memory and selects those that have a Reference Count that has reached zero. When this occurs the Garbage Collector frees up memory to make room for other objects. When it runs, the GC consumes CPU cycles and this can cause an overall slowdown. The Garbage Collector is not present in all programming languages such as, for example, C and C++. In these languages it is the programmer who has to take charge of freeing memory when an object is no longer active; this allows for better performance.

THE DOT OPERATOR

We will learn how to access the members of an instance of a class. Let’s look again at the class ContoCorrente.

ContoCorrente

We have not specified any access modifier, so as you know that modifier will be the default (visibility in the package it belongs to).

Dot Operator

We used the instance variable followed by a dot to access and initialize the balance. Let’s look at the general form.

Object.member

You specify the name of the object, a dot, and the member you want to access. Let us now introduce, as should always be done, access modifiers.

ContoCorrente with modifiers

After this change we are no longer able to change the balance attribute as we did before since the variable is private. However, we can deposit money into the account using the public interface. This time we used Dot Notations to access a method of the class ContoCorrente.

Method Deposit

DEFINE A METHOD

Methods are one of two types of members of a class, and are containers of instructions that then allow us to assign a range of operational responsibilities to the class. Let us see the general structure in the figure.

Struttura generale di un metodo

Start by defining a Data Type that indicates the type of return we should expect; it can return a primitive value, void (null), or an object. After the return type must be given the method name, which follows the normal conventions except for the first letter, which is lower case. Then there is the list of parameters, if there are more than one they must be separated by the comma, the values will have to be provided each time the method is invoked. A method may have no parameters, in which case round brackets should still be indicated. This should all be enclosed in a block of code within which we specify the instructions to be executed by the method. Let’s look again at the class ContoCorrente.

ContoCorrente

The deposit and withdrawal methods are similar, returning nothing (void) accept a parameter of type double called amount. The last balance() method accepts no parameters and returns a double (statement return).

RETURN FROM A METHOD

Each time we execute a method the instructions in its Body are executed, one after the other. However, sooner or later the execution of the method must end. Let’s analyze the deposit method, within it you have access to the attributes of the class by simply specifying the name. In this case the Body consists of a single instruction that increments the balance with the value of the amount and ends without returning anything. Let us now look at the balance() method. This returns a double and that is why we used a new keyword:
return
.That keyword causes a method within its Body to terminate immediately by returning to the caller.

A method that does not return a value (void) can still return to the caller with the statement return.

Modify withdrawal

LINKS TO PREVIOUS POSTS

THE JAVA LANGUAGE

LINK TO CODE ON GITHUB

GITHUB

EXECUTION OF THE EXAMPLE CODE

  • Download the code from GITHUB, launch the JAR file with the following command in Visual Studio Code, locating in the directory containing the JAR.

       java -jar –enable-preview CourseJava.jar

  • Or run the main found in the file CorsoJava.java.