Variables in Java

A Java variable is a place where data values are stored and used to run Java programs. Each element is given a certain information type that determines the kind and amount of significant information it can contain. The name of the data’s memory location is a variable.

A memory space is given a name called a variable. It is a program’s fundamental unit of capacity.

• A variable’s value can be altered while a program is running.

• A variable is just a name relegated to a memory address. That memory address is impacted by all   activities performed on the variable.

• Before being used, all variables in Java must be defined.

In Java, there are different types of variables in java, for example

  • String – stores text values , such as “Welcome”. String values are enclosed with double quotes.
  • int – stores integers values (numbers), without decimals, such as 100 or -100
  • float – stores floating point numbers, with decimals, such as 10.99 or -10.99
  • char – stores single characters, such as ‘x’ or ‘Y’. Char values are  enclosed by single quotes
  • boolean – stores values with two options: true or false

In order to create a variable, you must specify the type and assign it a value:

Syntax

type variableName = value;

In above syntax , type is one of Java’s types (such as int or String), and variableName is the name of the variable (such as z or name). The equal sign is used to assign values to the variable.

Now to create a variable that can store text value, shown on below example:

Example

Create a variable called name of type String and assign it the value “Aman“:

String name ="Aman";
System.out.println(name);

Output :  Aman

Now to create a variable that can store a number, as shown on below example:

Example

Create a variable called myNum of type int and assign it the value 25:

int myNum =25;
System.out.println(myNum);

Output :  25

Types of Variables in Java

There are three types of variables in Java :

  • local variable
  • instance variable
  • static variable

1) Local Variable

Local variables are variables that are declared inside the method body. Only that method may utilize this variable, and the other methods in the class are completely unaware of its existence.

A local variable cannot be defined with “static” keyword.

2) Instance Variable

An instance variable is a variable that is declared inside a class but outside the method’s body.

It is not declared as  static .

It is known as an instance variable since its value is instance-specific and not shared by other instances.

3) Static variable

Static variables are those that have been explicitly designated as such. It can’t possibly be local. All instances of the class can make use of the static variable once it has been created. During class loading, static variable memory is only ever allotted once.

Example to illustrate types of variables in java

public class A  

{  

static int m=250; //static variable  

void method()  

{    

int n=300; //local variable    

}  

public static void main (String args[])  

{  

int data=50;  //instance variable    

}  

} //end of class 

Scroll to Top