4.2 Basic Java Syntax: Variable Declaration

Java is a strongly typed programming language, which means that all variables must be declared before they can be used. Variable declaration is one of the fundamental concepts in Java, as it defines the type of data that a variable can contain and establishes the basis for manipulating that data.

Data Types

Before we declare a variable, it is essential to understand the data types available in Java. There are two main types: primitive types and reference types.

  • Primitive Types: include int, float, double, boolean, char, byte, short and long. These represent simple values ​​and are not objects.
  • Reference Types: include classes, interfaces and arrays. They reference an object and can be null.

Variable Declaration

To declare a variable in Java, you must follow the following syntax:

type variablename;

Where type is one of Java's data types and VariableName is the name you choose for the variable. For example:

int age;

This line of code declares a variable called age that can contain integer values.

Variable Initialization

After the declaration, you can initialize the variable, assigning it a value:

age = 30;

It is also possible to declare and initialize the variable on the same line:

int age = 30;

Rules for Variable Names

There are some rules and conventions that must be followed when naming your variables in Java:

  • The name can contain letters, digits, underscores (_) and dollar signs ($), but cannot start with a digit.
  • It is not allowed to use language reserved words as variable names (e.g. int, class, static, etc.).
  • The name must be meaningful and represent the contents of the variable to make the code more readable.
  • By convention, variable names begin with lowercase letters and follow the camelCase style for compound names (for example, numeroDeEstudantes).

Scope of Variables

The scope of a variable refers to the part of the program where the variable is accessible. In Java, there are three main types of scope:

  • Local Scope: Variables declared within a method are accessible only within that method.
  • Instance Scope: Variables declared within a class, but outside any method, are accessible by all methods of the class (unless marked private ).
  • Static Scope: Variables marked with the static keyword are accessible by all methods of the class without the need to instantiate an object.

Constants

If you want to create a variable whose value should not change during program execution, you can declare a constant using the final:

keyword.
final double PI = 3.14159;

Once a constant is initialized, its value cannot be changed.

Type Conversion

In some situations, you may need to convert variables from one type to another. This is known as casting. There are two types of casting in Java:

  • Implicit Casting: occurs when the system automatically converts a smaller data type to a larger type (for example, from int to long ).
  • Explicit Casting: requires you to explicitly specify the cast. This is necessary when you are converting from a larger type to a smaller type (for example, from double to int).

For example:

int myAge = 30;
double myAgeDouble = myAge; // Implicit casting
int myAgeAgain = (int) myDoubleAge; // Explicit casting

Conclusion

Declaring variables is an essential step in Java programming. Understanding basic syntax, data types, naming rules, and variable scope helps you write clear, efficient code. Always remember to initialize your variables and use constants when necessary to protect your data from unwanted changesof. Diligent practice of these concepts is critical to becoming a competent Java programmer.

Now answer the exercise about the content:

_Which of the following statements about variable declaration in Java is correct?

You are right! Congratulations, now go to the next page

You missed! Try again.

Article image Basic Java Syntax: Arithmetic and Assignment Operators

Next page of the Free Ebook:

7Basic Java Syntax: Arithmetic and Assignment Operators

6 minutes

Obtenez votre certificat pour ce cours gratuitement ! en téléchargeant lapplication Cursa et en lisant lebook qui sy trouve. Disponible sur Google Play ou App Store !

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text