Variables and Data Types : Java Programming Tutorial

Variables and Data Types - Java Programming Tutorial
Variables and Data Types

Let's start by setting up the basics that will allow us to gradually write a Java program.

We will start with variables, a fundamental concept in development. 

The purpose of a program is to do something with data or, in other words, with the things that you put in your program. Often your program will use one or more variables to manipulate the data. 

Variables are essential elements of any program. A variable is a container used to store information that your program might need later.

The name of a variable must reflect the meaning of its content. In Java, variable names cannot start with a number or contain spaces.

  • A variable is made up of three elements: name , value and type.
  • The values ​​of the variables can be changed.
  • Variables with constant values ​​are called constants.
  • Name variables must follow common naming conventions.

The types of the data.

You have seen that we can store values ​​in variables and that we can easily manipulate them. But with all the “typed” variables, it is not possible to perform arithmetic operations.

There are eight simple, or primary, data types in Java, and you need to decide which one to use based on the type and size of the data you intend to store in each variable:

  • Four data types for integer values: byte , * short , * int, and * long .
  • Two data types for decimal values: float and double .
  • A data type that allows you to store an isolated character: char .
  • A logical data type named boolean that allows only two values: true or false .

You can assign an initial value to a variable when it is declared. We then speak of initialization of the variable

char lettre = 'B';
int filles = 12;
boolean estMajeur = false;
double budgetNational = 50063494965745.78;
float prixVoiture = 12500.50f;
long totalPopulation = 4637283648392l;

There is also the final special keyword ; if it is used in the declaration of a variable, a value can only be assigned to this variable once, this value cannot be modified subsequently. In some languages, invariant variables are called constants.

In Java, the names of invariant variables are usually written in upper case letters:

final String CAPITALE = "Text";

In addition to primary data types, you can also use Java classes to declare variables. Each type of primary data has a corresponding envelope class, for example Integer, Double, Boolean, etc. 

These classes have useful methods for converting data from one type to another. While the char data type is used to store a single character, Java also has a String class for handling longer text, for example:

String ville = "Text";

You can continue to use jshell to try out the different operations and familiarize yourself with variables and data types. We meet in our next chapter to see how to retrieve the information entered by the user from the keyboard.

Post a Comment

Previous Post Next Post