Skip to main content

Variables and Data Types

Java Variables

What are variables?

To do anything interesting in our programs, we need a way to store and manipulate values. A variable is simply a named area of storage.

Variables store data, but what kind of data is a variable storing? This raises a problem and introduces a concept called "data type"( or the type of data).

It's important to note that Java is a strongly typed language. This means that variables must be declared with a specific data type before they can be used. This helps ensure the code is more reliable and less prone to errors.

Data types

In Java, a data type is a classification of data that determines the type of values a variable can hold and the operations that can be performed on it.

Java data types define variables that store different types of values, such as numbers, characters, and boolean values. They play a crucial role in ensuring that programs are reliable, efficient, and error-free.

Syntax

The syntax of declaring a variable with a specific data type is as follows:

<data_type> <variable_name>;

For example, to declare a variable named "age" with an integer data type, the syntax would be:

int age;

age is an un-initialized variable, it means it's been declared but does not yet have a value.


Data types in Java are divided into two types that are used to define variables include:

  1. Primitive data types
  2. Non-primitive (or reference) data types.