Home Java Tutorials Variables In JAVA The Complete Tutorial – With Java Example

Variables In JAVA The Complete Tutorial – With Java Example

0

Variables are something that makes the computer programs very useful for storing information in computer temp memory. When you think of variables, think of boxes that are used to store the real-world object. In the computer world, you can have hundreds, thousands n 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 a variable where int is an integer in this case and x is the name:

Eg: int n;
Here int is integer primitive data type and n is the variable.

Syntax Of Java Variables:

JAVA has its own rules for syntax of variables and we need to follow it. Syntax of variables in JAVA is divided into 3 parts:

  1. The types of data you are storing (int, char, byte, etc.)
  2. Naming (let’s say n). where you store the data information.
  3. Putting a semicolon (;) at the end. which is telling the program statement ends here.
int n;

If you want to store values into java variables directly, you can do it by using = (which is also called as assignment operator) :

int n = 10;

In the above syntax, we are storing values directly in a variable n with value initial value as 10. n = 10;

Few more Java Variables Examples:

Let us create a student variable that stores student details :

int rollno=123;
char name =”x”;

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

Important Note: 

char variable can only store one character. If you want to store full name then you need to use the String variable.

Defining variable for storing decimal number say 500.36

float account_balance = 1000.58;

Above Example Explanation:

  • int, char, and float are a data type that is telling the program to assign area in memory for storing integer, character or decimal number.
  • Rollno, name & account_balance are names of variables and value, 3, “Rajat” & 1000.58 is stored in them.

Types of variable in java

There are 3 types of variables in JAVA:

1. Local Variables:

 These can be defined inside the method, constructor or also inside the block. The scope or lifeline of the local variable destroyed with the end of method completion.

2. Instance variables: 

These are associated with object creation. As the object gets created instance variable also gets created.

3. Class Variable/static variables: 

These are loaded and initialized when class is loaded in JVM (java virtual machine) and there exists only one copy of the class variable.

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

For more details read Type Of Java Variable Tutorial With Example

How To Print Java Variable Value:

We can easily print/display java values by using a java inbuilt method System.out.print();

Here the System is a class, out is an object and print is the method to print/display values on the screen.

If you are new in java and not able to understand what is class, what os object don’t worry just remember System.out.print(); is used to print values on the screen in JAVA.

System.out.print(); will just print values without a new line, if you need to give a new line then you should use System.out.println();

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

int n = 10;

Now using the inbuilt JAVA method for printing it:

System.out.println(x);

Complete Java Code for printing Variable in the screen

public class Main
{
	public static void main(String[] args) {
	    int n = 10;
		System.out.println("Value of n = "+n);
	}
}

Output

Value of n = 10

Here JAVA takes a looks for the value stored in the memory of variable name n, finds it and then prints it which is 10.

Why it is important 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 that can calculate the current age based on the birthday of the person. The person will enter his birthday details which need to be stored somewhere in the 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 the program because it gives the ability to store information which can then be processed. Without variables, the computer won’t be able to process anything.

Post referred from https://abhiandroid.com

No Comments