How to Create Variable in Java?

Friends, today in this post we will read about Variable in java and also see variable types, so let’s start :-

Introduction to Java Variables

Variable in java is used by Java to store any information and you do that information to the value of that variable. This information can be anything, like if you want to store the name and age of a person, then for this you need 2 types of variables, in one variable you will store the name of that person and in another variable you will store his age.
The value of any variable is changeable, you can store any other value whenever you want, those Java variables whose value cannot be changed are called constants. Before storing the information, you have to create the variable. To create the variable, you give the name of the variable, but the name of the variable should always be unique, otherwise the compiler will generate an error, as well as before the name of the variable, you have to enter the data type. have to be defined.
Data type means what type of information you want to store, such as whether you want to store a number or a word or a decimal number. That is, what kind of number do you store before the name of the variable, so it is very important to show it which
is called data type.

Type of variable

In java, the variables are divided into their 2 types, all of them are given from the details below.

Local Variable – When you create a variable in a method, then using that variable remains till that method, there is no use of the variable outside that method, it is called the scope of that variable whose scope is in any method. They are called local variables.

Global Variable – When you want to use a variable in the whole program, then you do not declare it in any method, in this case the variable is declared at the beginning of the program, this variable can be used anywhere in the whole program. The global variable is called the scope of the global variable in the whole program.

Creating Variable in java

There are 2 stages to create a variable in java, an attempt is made to understand these two stages.

variable declaration

In this stage, you only define the name of the variable and what kind of value it has to store.

int person age; // data type

variable initialization

In this stage you are defining the value of the variable.

person age=20;

It can also be done with both

int person age=20;

If you want the value of the variable to be input by the user at run time (dynamically), then you do not initialize the variable, just declare it and leave it in the first stage itself.

Note:-

Taking input from user

To input a value into a java variable at run time, you create an object of the scanner class, this class provides many types of methods, which are used to get input from the keyword, different for each type of data type. method is provided. While creating the object, you pass system.in in it argument.
system.in means that whatever the user types in the keyboard, you want to take it, you can use different methods on this object to read the value.
For example, to read integer value, you can call methods like next int(), next line() for string and next float() to read float value.

int person age; //variable declaration
scanner s=new scanner(system.in); //scanner class object
person age=s.next int(); //reading from keyboard