Skip to content

Variables

Representing and using data won't take us far if we cannot store them and use them for manipulations. Therefore, Java and many other programming languages have mechanisms called variables for temporarily storing data values.

We will cover following topic to understand variables:

  • What is Variable
  • Creating & Using Variable
  • Variable Naming Conventions



› What are Variables

It is a named data storage that holds raw data values in our code. Using variables allows us to write clean and simpler code that is easy to understand. Their sole purpose is to label and store data in your code, eliminating the need to work with the raw data value.

Benefit of using variables:

  • Its label gives meaning to the data value it holds
  • It makes programming simpler by working with a label instead of raw data value
  • A change in data value can be accommodated by a variable content update

Code sample for creating integer variable:

1
2
3
int age ;  // creating empty variable with label [age]
age = 29;  // assigning data value [age] --> 29
age = 35;  // updating data value  [age] --> 35
This is roughly what is taking place when you execute the above code sample.

Creating Variable Animated

As you can see, the label age is describing the data values ( 29 and 35 ). This makes your code so much easier to read. That is the primary benefit of variables, making the development easy.



› Creating & Using Variable

Let's look at how we can create and use the variable. You can make any variables by using the following two different syntax.

1
2
3
4
5
6
7
8
9
[Data Type] [Label] = [Data Value];

// Code example:
String fullName = "John Miller";

// Where
[Data Type]  -- String 
[Label]      -- fullName
[Data Value] -- "John Miller"
[Data Type] [Label];
[Label] = [Data Value];

// Code example:
String fullName;
fullName = "John Miller";

// Where
[Data Type]  -- String 
[Label]      -- fullName
[Data Value] -- "John Miller"

These two variable creation syntax does exactly the same thing.   Here is the example of creating and using a variable for each data types we have covered so far.

Creating and using variables

Please note that you must assign a data value once you create a variable.  Using an empty variable is a programming error.

For example, the following code throws an error:

int age;                 // empty varaible   
System.out.println(age); // error, cannot use empty variable
Empty variable error animated

Always assign an intended or default data value to prevent such an error.   Recommended default data values for each of the data type's variables are as follows:

Variable Data Type Default Data Value Example
int 0 int age = 0;
double 0.0 double money = 0.0;
boolean false boolean isJavaFun = true;
char '\0' char emptyChar = '\0';
String null String userName = null;



› Naming Conventions

Like many other modern programming languages, Java has rules and conventions for variable label naming. Here's what you need to know:

  • Variable name in Java are case-sensitive
  • Variable name must be a sequence of letters and digits.
  • Variable name can start with a letter, $ or _ characters
  • Variable name cannot start with a number
  • Space between two words are not allowed
  • First word's letter must be lowercase

You can use this table to better understand the above rules:

Variable Name Remarks
userSpeed ✔️amazing variable name
speed1 ✔️valid variable name
speed ✔️valid variable name
_speed 😪 valid but bad variable name
$speed 😪 valid but bad variable name
UserSpeed 😪 valid but big no no
spe ed ❌ invalid variable name
9speed ❌ invalid variable name
spe"ed ❌ invalid variable name

Also make sure that you choose a variable label name that describes the data.   For example, take a look at the following two variables:

int   x = 29;  // name 'x' does not make sense, what is 29 representing?
int age = 29;  // name 'age' does make sense, 29 represents age
As you can see, variable name age describes the data 29 far better than just character x.