Exercises with variables in Java

After the previous post about variables in java, in this one, I leave some exercises to practice what I explained then. This will be the way of acting for the rest of the Java topics, firstly I explain the topic and then, in the following post, I will propose a series of exercises and their solutions.

There are several possible solutions to solve the same problem, some more efficient than others. Here you can see a possible solution for each proposed exercise, which is not the only one possible.

Continue reading Exercises with variables in Java

Variables in Java

In this post, I am going to talk about variables in Java. I will explain how to declare and initialize them, the types of primitive variables that we handle in Java, how to change the type of a variable and its scope.
But before we start, we must introduce a series of preliminary concepts:
• A variable is a container, a memory location, that stores a value while a program is running.
• We put a name to each variable, so that we can reference it in our code, and use the value it stores.
• Variables are of one type. The type of data that we intend to store in the said variable. For example, if a variable is of type int, it will be used to store integer values. However, if it is of type String, it is used to store a string of characters, that is, text.
• Variables have to be declared. In this declaration, we indicate the type of the variable and its name.
• A variable has to be initialized to be used in our program. Initializing a variable is putting a value in it.

Continue reading Variables in Java