ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel
  • »
  • Technology»
  • Computers & Software»
  • Computer Science & Programming»
  • Computer Programming Tutorials

How to use Variables in Java ?.

Updated on September 17, 2014


Java is a powerful programming language. It has numerous features which makes it most popular programming language of the world. In this tutorial, you will learn how to use variables in Java. Variables are important in any programming language. Variables are the temporary place holders used to store values in a program. It can be modified or used later in the program. Variables is nothing but a named location in memory. This memory location is used to store values of the programs that can be modified by programming instructions. It is been termed as variable because its values can be changed later in the program.

There are few things which make up and create a variable, such as a name, a data type, a size and a value.

How to define a variable ?.

A Variable contains a name, datatype, size and value. In order to define a variable we have to follow syntax such as :

<Datatype> <Name>;

For example :

  • int age; (Here, int is the datatype and age is name of variable).
  • String description; (Here, String is the datatype and description is name of variable).

As Java is strongly typed language we have to provide a datatype to define a variable. If we don't provide a datatype to a variable compiler will throw a compile time error.

Video tutorial - How to use Variables in Java.

How to define and initialize a Variable in Java ?.

The second form to define a variable is as follows :

<Datatype> <Name> = <Value>;

For example :

  • int age = 45; (Here, int is the datatype, age is name of variable and 45 is the value assigned to variable). It creates a memory location in computer by name age and assign a value 45 to it. So that now age is a place holder for that value.
  • String description = "He is a good programmer"; (Here, String is the datatype, description is name of variable and "He is a good programmer" is the value assigned to variable). It creates a memory location in computer by name description and assign a value "He is a good programmer" to it. So that now descrption is a place holder for that value.

The size depends on the datatype, such as int can have 32 bit, byte can have 8 bits etc. The size tells compiler that variable can take only a particular range of a value. The size of a variable make it easy to be used in situations where size matters.

Instead defining multiple variables one after other, Java also provides a way to define multiple variables simultaneously. So, defining multiple variables simultaneously is a good practice in Java. Lets take an example for this important way to define variables in Java.

int a ;

int b ;

int c;

Instead doing above things in multiple lines, we can perform all these steps in a single step such as :

int a, b, c ;

Java also provides a way to initialize multiple variables simultaneously. Lets look into an example :

int a = 4 ;

int b = 4 ;

above variables can also be initialized simultaneously as :

int a ;

int b = a = 4 ;

Here, both the variables a and b are initialized simultaneously together with a value 4.

public class Variables {
	int a;
	int b;
	int c;
	// can be written as : 
	// int a , b , c;
	int d = 4;
	int e = 4;
	// can be written as : 
	// int d;
	// int e = d = 4;

Source code - Variables in Java

public class VariablesDemo {

	public static void main(String[] args) {

		// <Datatype> <Name>;
		int age;
		String description; 
		// <Datatype> <Name> = <Value>;
		int count = 10;
		String firstName = "Sachin";
		System.out.println("The value stored in count is : " + count );
		System.out.println("The value stored in firstName is : " + firstName );
		int number1 = 4;
		int number2 = 5;
		int sum = number1 + number2;
		System.out.println("The value stored in sum is : " + sum);		

Output of the program -

The value stored in count is : 10
The value stored in firstName is : Sachin
The value stored in sum is : 9

Constant variables in Java.

In Java there are also variables which remain constant. By constant we mean that once assigned a value, there values cannot be modified in the program. A constant variable is been marked with a Java keyword as final. After making a variable final Java treats it differently. A variable marked as final and its value is changed later, a compile time error is thrown. Some of the examples are as follows :

final int age = 5;

final String description = "He is good".

Static variables in Java

In Java there are also variables which can be marked as static variables. Static variables makes variable belong to a class. It can only be accessed through a class to which it belong. Static variables can be marked as static through static keyword. Any variable marked with keyword static belong to a particular class.

For example :

static int age;

static int age = 35;

Here, age belongs to a class in which they are declared and can be accessed by it.

static and final variables in Java

public class Variables {

	// final age variable once assigned a value cannot be 
	// modified later.
	final int age = 45;
	// static variable welcome belongs to class Variables
	// and can be accessed by it. 
	static String welcome = "Welcome to Java"; 

Identifiers in Java

The name provided to the variables is been decided by the identifiers in Java. Identifiers in Java are nothing but names given to variables based on some rules. Below is the video tutorial which demonstrate how to name these variables based on some rules. Some of the rules are as follows :

  1. Identifiers should not be named by reserved keywords in Java. Some of the keywords are int, boolean, class etc.
  2. Identifiers should not start with a number.
  3. Identifiers first letter should start with a character, underscore. Digits can be used there after.
  4. Capital letters should be used to separate two words.
  5. Java identifiers are case - sensitive in nature. For example - birthday , Birthday and BIRTHDAY are three different identifiers in Java.
  6. Usage of dollar sigh or any currency symbols should not be used in naming an identifier.

Video tutorial - Identifiers in Java


    0 of 8192 characters used
    Post Comment

    No comments yet.