Free Ebook cover Learn to program in complete Java, from programming logic to advanced

Learn to program in complete Java, from programming logic to advanced

3.5

(4)

238 pages

Basic Java Syntax: Variable Declaration

Capítulo 6

Estimated reading time: 5 minutes

+ Exercise
Audio Icon

Listen in audio

0:00 / 0:00

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.

Continue in our app.

You can listen to the audiobook with the screen off, receive a free certificate for this course, and also have access to 5,000 other free online courses.

Or continue reading below...
Download App

Download the app

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.

Java is a strongly typed language, requiring variables to be declared with a specific data type before use. It is not allowed to use reserved words as variable names, and variable names cannot start with a digit. Thus, the correct statement is 3: a variable in Java must be declared with a specific data type before being used, and cannot start with a digit.

Next chapter

Basic Java Syntax: Arithmetic and Assignment Operators

Arrow Right Icon
Download the app to earn free Certification and listen to the courses in the background, even with the screen off.