Java Variables

Java variables are containers which hold values during execution of java program. These are names of a memory locations where values are temporarily saved.
A variable can hold:
simple text called String
Single character represented as char
a whole number called integer and represented by int
a number with fraction such as 2.65 called floating point numbers and represent as float
values with two states: true or false represented as boolean

How to create or Declare a variable

To use a variable in java we first need to declare it. To declare a variable first of all data type of variable is mentioned e.g. int, String, char, double followed by variable name and a termination character i.e. semicolon (;).

Syntax:


DataType variableName ;

Example

Following java code will create a variable named age with data type int and put a value “10” into it.

int age;
age = 10;

We can create and assign values in a single statement as follows:

int age = 10;