A Java Approach: Variables

by Alessio Mungelli Date: 20-09-2020 java jdk jre developement variables tutorial source code


Hello everyone and welcome back!

Today we will begin a journey that will lead us to study, and possibly review, what are the basics of programming. We will start by talking about variaibli.

Introduction

Anyone who wants to approach the world of programming will soon realise that coding is not exactly like what is shown in films. It is not just about printing messages on screen, or rather, printing messages on screen is only the final part of a much more complex process that requires real craftsmanship.

In this series of articles, we will try to understand what the basic bricks of this activity are and above all we will try to understand why it is an artisan activity, even though it seems to be anything but.

Variables - Definition

The first fundamental element that anyone needs to know in order to approach the world of coding is the concept of variable.

This is a concept as simple as it is fundamental. Wanting to give a first definition, however crude, one could say that a variable is an entity that changes over time. Putting it that way might seem a rather random definition. Let's try to understand it better.

Let's start with an example

Let's imagine that we have to develop a simple application to calculate the area of a square plot of land. The user will be asked to enter the side of his terrain and the program will return the area on screen.

To print on screen, there are several instructions provided by the various languages, Java in our case. The bulk of the work consists of the actual resolution of the problem. 

The first practical problem that arises is: is there a way to save the data that the user provides us with? The answer is yes. It is possible to achieve this through the use of variables. 

It is therefore necessary to know that every data that our program uses is saved in what in the literature is called primary memory, commonly known as RAM memory. Therefore, each variable is saved in RAM. It is the programmer's responsibility not to fill up memory with useless data unnecessarily. At the same time, we must always ask ourselves on the one hand if the variable is necessary, but on the other hand if its removal brings advantages.

At this point, it is possible to give a more refined and more relevant definition of the variable context. A variable is therefore a memory location within which data is read and written.

Declaration and initialization of a variable

At this point, after a long introduction, we can move on to practice. We must, first of all, understand what are the fundamental operations that are carried out on a variable.

Declaration

The first thing we have to do is "communicate" in some way to the computer that from a certain moment on, we intend to use a certain variable.

This is possible through an operation that is called declaration

The declaration of a variable is mainly composed of two parts. We could define a prototype like the following: dataType variableName.

Data type

Let's take a closer look at what a given type is.

Type is a classification term that groups together all those variables that are stored in the same way and to which the same set of operations applies. 

In mathematics, when we define a numerical set, we define a set of operations that can be performed on that set. Thinking of the set of natural numbers, we know of operations that we can perform such as sum or division. We also know that the same operations will be applicable to each number of that set.

In the same way, we classify the variables in a series of sets. Let's summarize in a table what these sets are.

Tipo

Memory quantity 

Information represented

byte

8 bit

Variable with sign and represents values in a range [-128 and 127] (extremes included)

short

16 bit

Integer numbers (with sign) in a range [-32768, 32767].

int

32 bit

Integer numbers (by default with sign, signed) in a range [-231, 231-1].

long

64 bit

Integer numbers (by default with sign, signed) in a range [-263, 263-1].

float

32 bit

Single precision floating point numbers according to the IEEE 754 specification, using the sign, mantissa exponent representation.

double

64 bit

Double precision floating point numbers according to IEEE 754 specification.

boolean

Not specified. just one bit would be enough represents two values: true and false

char

16 bit

Used to store Unicode encoded characters in the range ['u0000', 'uffff'](in hexadecimal) or equivalent [0.65535].

Variable name

When we declare a variable we will also have to name it. This necessity arises from the fact that we cannot know a priori in which area of memory the data we are interested in will be stored. We therefore need to have an alias to refer to a specific memory location.

We can therefore give a third and final definition of variable. A variable is a reference to a memory location in which a data of interest is stored.

Initialisation

Initialisation of variables is the preliminary phase of programming in which they assign initial values to previously declared variables. Initialisation follows the declaration of the variables or, alternatively, it can be done jointly.

Let's see an example.

int var;

var = 5;

In this way, after declaring the variable var, we assign the value 5 to the point cell.

Alternatively, the operation can be performed together with the declaration as follows:

int var = 5;

The initialization uses an operator who is the assignment operator, characterized by the symbol =.

Assignment operator

The assignment operator is the operator with whom the variables are valued. The fundamental thing to remember is that a memory location can contain one value at a time. It is categorically impossible for a location to contain two values at the same time. From this we can deduce that an assignment completely deletes the previous value of that cell.

It must be said at this point that I will be making relatively improper use of language from now on. When I say "assign a value to a variable" I mean "write a value in the memory cell pointed by the variable".

Let's see an example.

int a = 0; //The variable contains 0

a = 3; //The variable a now contains the value 3

a = 5; //The variable a now contains the value 5

Conclusions

Here we have given the basic definitions, introducing the indispensable operations to know in order to use the variables. We will soon see, starting from a practical example, how this concept can be used to develop a working application.

As boring and useless as this introduction may seem, we will see later on how these concepts are really fundamental, not only to learn how to program in Java, but to program in the vast majority of languages.

 
by Alessio Mungelli Date: 20-09-2020 java jdk jre developement variables tutorial source code hits : 83  
 
Alessio Mungelli

Alessio Mungelli

Computer Science student at UniTo (University of Turin), Network specializtion, blogger and writer. I am a kind of expert in Java desktop developement with interests in AI and web developement. Unix lover (but not Windows hater). I am interested in Linux scripting. I am very inquisitive and I love learning new stuffs.

 
 
 

Related Posts

Dark Mode on website using CSS and JavaScript

In today’s article we are going to learn how to build pretty much standard these days on the web pages and that is the alternative color mode and switching between…

A Java approach: The Cycles - Introduction

Hello everyone and welcome back! Until now, we have been talking about variables and selection structures, going to consider some of the fundamental aspects of these two concepts. Theoretically, to…

A Java Approach: Selection Structures - Use Cases

Hello everyone and welcome back! Up to now we have been concerned to make as complete an overview as possible of the fundamental concepts we need to approach the use…

JavaScript: Spread and Rest operators

In today’s article we are going to talk about one of the features of the ES6 version(ECMAScript 2015) of JavaScript which is Spread operator as well as Rest operator. These features…

A Java approach: boolean variables

The previous time, we talked extensively about Boolean variables, trying to outline the main operations that can be carried out at a practical level.  Of all the cases examined, we have…

A Java approach: condtional structures

Hello everyone and welcome back! The previous times we have introduced the concept of variable, trying to define some basic concepts about it.  However, some situations suggest that the concept of…

Hashmap: hashing, collisions and first functions

Today we are going to study some concepts closely related to hashmaps. The concepts we are going to see are hashing and collisions. Hashing The idea of hashing with chaining is to…

Hashmap: Overflow Lists

In this short series of articles we will go to see how it is possible to create the Hashmap data structure in C. In the implementation we're going to use the…

Javascript: what are callbacks and how to use them.

Today we are going to learn about a concept that is widely used in javascript and that is used quite a lot by today's frameworks, libraries, especially NodeJS. This is…

Data structures in Java - Linked Lists

With 2020 we are going to look at a new aspect of programming: data structures. It is often the case that everyone uses structures provided by the various programming languages.…

A Java approach: variables - use case

Hello all friends and welcome back! After the introduction made on the variables, we try to analyse some critical issues that may arise in quite common situations. Let's start by analysing…

HTTP Cookies: how they work and how to use them

Today we are going to write about the way to store data in a browser, why websites use cookies and how they work in detail. Continue reading to find out how…

We use our own and third-party cookies to improve our services, compile statistical information and analyze your browsing habits. This allows us to personalize the content we offer and to show you advertising related to your preferences. By clicking on "Understood" YOU AGREE TO THE USE OF THESE COOKIES. You can also SET OR DISCONTINUE the installation of cookies by clicking on "Settings" or "Reject".

Accept All Only sistem cookies Configuration