Java Variables

Java variables are used to store the data values in the memory location. Each variables should be having a unique name.

Create a Variable

You can create a variable using the following syntax,

type variableName = value;

Here, type represents the type of the variable, variableName represents the unique name for the variable and value is the value assigned to the variable. The value should match the type of the variable.


string name = "Andrew";
System.out.println(name);   // Andrew

Here, string is the datatype(type) of the variable. We have declared a variable name and assigned Andrew as its value.

Finally, we print the value in the variable name.

Types of Variable

There are different types of variables available in java. They are,

  • String
  • char
  • int
  • float
  • boolean

String Datatype

A series of characters contained in double quotations is referred to as a string. To create a variable of type string we use the keyword String.


String name = "Readersbuddy";

Character Datatype

Anything that is enclosed within single quotes('') is referred to as character. We use the keyword char to create a variable of type character.


char option = 'a';

Integer Datatype

A number with no fractional or exponential parts is referred to as an integer.


int decNumber = 34;

Float Datatype

A float datatype is a numeric literal that can take either an exponential or a fractional form.


double pi= 3.14;

Boolean Datatype

A boolean datatype, is used to hold a value that can be either true or false.


boolean flag = true;

Most Read