Variables in Java programming

variables in java

What are Variables in Java?

Variables in Java are something that can store a value. A Variable is a container in which you can keep values of different kinds like numbers, characters, strings, etc. A variable has a name and a value that it stores in it. The value is actually stored in the computer memory at a specific location, but accessing value with a memory address can be quite complex. Hence, a name is used.

To incorporate a variable in your java program, two steps need to be done:

  • Variable Declaration
  • Variable Initialization

Variable Declaration in java

To declare a variable, its data type and name need to be specified. The syntax is given below:

int x;

where int is the data type of the variable specifying type of data it can hold and x is the name of the variable.

Variable Initialization in java

To initialize a variable, a valid value is assigned to it using the “=” equals sign. The validity of the value depends on the type of variable which specifies the kind of data the variable is going to hold.

 x=10;

Program: Depicts variable declaration and initialization

public class Main{
public static void main(String[] args)
{
int x// variable of type int has been declared
x=10;//and initialized to 10
System.out.println("x= "+10);//value of x is displayed
}
}

A basic program written above does nothing but declares and initializes a variable “x” of type int to 10. It is then printed using System.out.println() function. It’s important to note that every statement in Java ends with a semi-colon.

Note: A variable can be used only after it has been declared and initialized to some valid value. Using a variable before its declaration/creation will result in a compile-time error.

Declaration and initialization of a variable can be done either separately or together.

int x; and x=10; is same as int x=10;

Types of Variables in Java with example

Variables in Java can be classified into three categories:

  • Local Variables in java

  • Instance Variables in java

  • Static Variables in java

What are Local Variables in Java?

A variable is said to be local when it is declared inside the method of a class. Variables declared inside the body of a method are local to it and exist as long as that method does. Other methods are not even aware of its existence.

What are Instance Variables in Java?

A variable is an instance variable if it is declared inside the class but not in any method. It is named instance as it is instance specific and each instance has its own copy of these instance variables.

What are Static Variables in Java?

Static variables are declared inside the class but not in any method. To make a variable static in Java, its declaration is preceded with the keyword static. Static variables are initialized at the beginning of the program and are not instance-specific. Every instance has the same copy of a static variable which means change made to it by an instance variable will be reflected in other instance variables.

 

Note: Java does not have global variables. They are not technically allowed in it because they are declared at the start of the program and are accessible by every part of it and Java is object-oriented which means everything is part of a class.

 

Also, read the difference between C++ and Python.

Share this post

Leave a Reply

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