Variables In JAVA With Examples – Complete Tutorials

Variables is something that makes computer program very useful by storing information in memory. When you think of variables think of boxes which is used to store something. In computer world you can have hundreds, thousands or more number of boxes (mean variables) each containing their own pieces of information. That box is called variables in JAVA.

While creating variables in JAVA we need to assign two things. First is the type of information we want to store and second is the name of the variable. Below is an example of variable where int is a type of information (integer in this case) and x is the name:

Variable Example In JAVA

int x;

Here int is integer primitive data type and x is the variable.


Syntax Of Variables:

What Is Syntax: Syntax in programming language basically means combinations of symbols based on the set of rules defined for that language. JAVA has its own rules for syntax of variables and we need to follow it.

Syntax of variables in JAVA is divided in 3 parts: first assigning type of data you are storing (int,char,byte etc.), second is naming (let’s say x) and third is putting semicolon(;) which is telling program statement has end here.

int x;

You can also store value directly here if you want by putting value after = (which is an assignment operator):

int x = 5;

In above syntax we are storing integer(number without any decimal point). In JAVA “int” is used to assign integer.


Variables Examples In JAVA:

Let us create a simple example of variables storing student details:

int rollnumber = 3;
char name = 'X';

In the above example we have defined two variables rollnumber and name for storing student details.

Important Note: char only store one character. If you want to store full name then you need to use String.

Defining variable for storing character say A

char name =’A’;

Defining variable for storing decimal number say 500.36

float income = 500.36;

Above Example Explanation:

  • int, char and float are data type which is telling program to assign area in memory for storing integer, character or decimal number
  • rollnumber, name & income are names of variables and value 3, abhishek & 500.36 is stored in them.

Types Of Variables

There are 3 types of variables in JAVA:

1. Local Variables: These can be defined inside method, constructor or also inside block. The scope or life time of local variable destroyed with end of method completion.
2. Instance variables: These are associated with the object creation. As the object get created instance variable also get created
3. Class Variable/static variables: These are loaded and initialized when class is loaded in JVM and there exists only one copy of class variable

Examples Of Types Of Variables:

public class TypeOfVariable{
	public static int staticvariable;
	int instancevariable;
	public void printValue(){
		int localvariable;
		System.out.println("the value of staticvariable \t"+staticvariable);
		System.out.println("the value of instancevariable\t"+instancevariable);
		System.out.println("the value of localvariable \t"+localvariable);
	}
	public static void main(String args[]){
		TypeOfVariable object=new TypeOfVariable();
		object.printValue();
	}
}

For more details read Type Of Variable Tutorial With Example


How To Print Variable Value:

Here we will show you how easily we can print variable value in JAVA by using inbuilt JAVA method System.out.println();. Here System is class, out is an object and println is a method for printing. If you don’t understand class, objects and methods don’t worry about it. Right now just remember System.out.println(); is a method for printing in JAVA.

Now lets print a number 5 in JAVA. First we will create a variable and store value 5 in it. Remember 5 is an integer so we will assign int as a data type:

int x = 5;

Now using inbuilt JAVA method for printing it:

System.out.println(x);

Complete code:

int x = 5;
System.out.println(x);

Output:

5

Here JAVA look for value stored in the memory of variable name x, finds it and then print it which is 5.

Important Note: Other than simply printing variables we can use them for many other purpose like adding, subtracting etc. which we will share in the Operators topic.


Importance Of Variables In JAVA:

  • It makes the computer program very useful by storing information which can be easily accessed. For example if you want to create a JAVA program which can calculate current age based on birthday of the person. The person will enter his birthday details which needs to be stored somewhere in computer with proper naming to make it easily accessible for calculating his age. Here you need variables to do that for you.
  • In simple words, you need variables in program because it gives the ability to store information which can then be processed. Without variables computer won’t be able to process anything.

Leave a Reply

Your email address will not be published. Required fields are marked *