Java Tutorial


Chapter 5 – Java Variables


In the previous tutorial we went through Java Basic Syntax. In this tutorial we shall try to understand variables in Java.

Java Image

What is a Variable in Java?

In any programming language, variables are like containers used to store values. We can have different types of variables depending upon the data types allowed in the programming language.

In Java, we can have variables of type such as int, float, boolean, string, etc. We shall learn more about Java data types in later chapters. In this tutorial we shall explore more into the variable aspect of Java.

Let us explore how we can:

  • Create a new variable
  • Assign value to an existing variable
  • Creating multiple variables together
  • Creating a constant variable



1. Creating a variable in Java

To create a new variable in Java, we use the below the syntax:

variableDataType variableName=variableValue;

For. e.g.
String name=”John”;

The above code statement will create a new variable titled as “name” of the String data type and will assign the value of “John” to the variable. You can view the variable value by printing it in the console as below:

System.out.println(name); // It will be printed as “John”


Similarly let us look at another example

int age=21;

The above code statement will a create a new variable titled as “age” of the int datatype (primitive data type referring to numbers), and will assign the value “21” to this variable. Please note that the numeric values are not included in quotes in the program. You can view the value of the variable by printing it in the console as shown below:

System.out.println(age); //It will be printed as 21




2. Assigning a variable in Java

As we can see in the earlier examples, we created and assigned the variable in a single line. Let us suppose you want to create an empty variable first and want to assign the value to it later during code execution runtime, you can do that in this way:

variableDataType variableName;
variableName=variableValue;


For. e.g.
Let us use the earlier example to demonstrate this approach.

String name;
name="John";
System.out.println(name); //It will print John.
name="Lara"; //It will reassign the variable
System.out.println(name); //It will print Lara.




3. Creating multiple variables together in Java

Let us say you have multiple variables to be used in your program. You may want to create and assign the variable like below:

int length=50;
int breadth=30;
int height=20;
System.out.println(length*breadth*height); //This will give the volume value.

Instead of creating variables of similar data type in multiple lines, you can create and assign them in a single line using comma as separator.

int length=50, breadth=30, height=20;
System.out.println(length*breadth*height); //This will give the volume value.

If you want to assign same values to multiple variables, then you can do that by creating empty variables in the beginning and assigning all of them in a single line as shown below:

int length, breadth, height;
length=breadth=height=20;
System.out.println(length*breadth*height); //This will give the volume value.




4. Creating a constant variable:

If you want to create a variable with a constant value that cannot be modified during code runtime, you can create the variable with a keyword called “final”. This keyword is a type of Java Modifiers, which we will explore in later chapters. The syntax of declaring constant variables is as shown below:

final variableDataType variableName=variableValue;

For e.g.

final int max_value=3;

This will create a constant variable with the name as “PI”, whose value is assigned as 3.14 and is fixed. If you try to assign any new value to this variable during runtime as shown below, you will get an error.

max_value =7;




Best practices for variable declaration

Now that we have a fair understanding of variable declaration and assignment, let us understand some of the best practices we must follow while dealing with variables.

  1. Every variable name should have unique name, we cannot create two new variables with the same name.
  2. A variable name can contain alphanumeric values.
  3. A variable name should ideally start with a letter though it can contain symbols such as $ and _ also.
  4. Try to create variable names which can quickly help understand the purpose of the variable, instead of giving random generic names. For e.g., variable as “age”, “name” would give a better understanding of the purpose of the variable instead of variable name such as “x”, “y”, etc.
  5. The variable names are case sensitive. Hence the variable name “age” and “Age” are not same and will be treated as 2 different variables.
  6. We are not allowed to use reserved keywords of Java as variable names. For. e.g. data types such as int, String; or access modifiers such as public, private, final, etc; or the other Java keywords such as class, return, etc, cannot be used as variable names.

I hope this tutorial gave you a fair understanding of Java variables. See you soon in the next tutorial.

Follow Me on Social Media

Advertisement
FREE Sales CRM Software

Fully customizable CRM Software for Freelancers and Small Businesses

Signup for Free

Sign up for DigitalOcean Cloud

Get FREE $200 credits, deploy your hobby projects for Free

DigitalOcean Referral Badge
Sign up for Hostinger Cloud or VPS plans and get upto 20% Discount