Java Tutorials for Beginners- Variables in Java

Variables in Java

Variables in Java

A variable is a named memory location where the data value of a specific data type is stored. In Java, a variable is a type of container that holds the value while the program is running. Let’s get started with Java Variables.

A variable is a basic unit of storage in a program that represents reserve storage locations, the values of which can be changed during the program’s execution. A datatype is assigned to a variable. We can refer from this also:

The word variable is broken down into two words: “vary” & “able,” which means that the value of variables will change.

  • variables are also symbolic variables.
  • A variable is a memory location, and any manipulation or operation on the variables affects this memory location.
  • All variables in Java must declare before they can be use
  • Each variable has a data type that determines how much memory it has and what kind of data it can store.

For Example:

int JavaClass;

Declaration of Variables


The declaration of variables in java is usually done in this way:

dataType variableName;

Other variable declarations are:

float num1;
int num2;
double num3;

When we need to declare more variables of same data type we can declare it by comma separated values.

int num1,num2,num2;
float n1,n2,n3;
double s1,q,x,y;

Note: When declaring variables in any programming language try to remember the rules about declaring variables in java.

  1. Variable name cannot contain whitespaces in it.
  2. First letter in variable should not be digit.
  3. we cane use special characters while naming variables i.e. (&),(_).
  4. Always start Variable name with lowercase and if name is long then use CamelCase, for example: salaryAfterMonth, NumberThree .
  5. In java, Variable names are case sensitive.
  6. Using data type like int, float as variable name is wrong practice in Java.

Initialization of Variables

Syntax: we can initialize variables in java as given below:
varName = value;
int varName= 107;

Given below the image which illustrates about variable declaration and its memory allocation:

Variables in java


public class Variables {
    public static void main(String [] args){
        int marioScore = 200;
        int lifeUps = 50;
        int total = (marioScore * lifeUps);
        float x = 3.50f;



Process finished with exit code 0

Values of Variables:

As local variables have no default value, we must assign one before using them for the first time. When a class variable, instance variable, or array component is created, it is initialised with a default value:

variable Default Values


Here we come to an end to our blog on Java Variables. We’ve gone over the Java variables and their types in depth in this article, along with their examples, syntax, and logical codes, so you can understand them.

If we miss anything in this blog, please do share your thoughts in comment Section. We will be glad to add your Suggestions!!!

Please Go through this blog pages also: