Java

Variable types in Java

Variables are entities that hold data values while a Java program is in the execution phase. Variables denote the memory location where the assigned values are stored. You need to declare and initialize a variable in order to use a variable.

In this tutorial we will explore different types of variables in java and we will also learn how to declare and initialize a java variable.

Variable Declaration

Variable declaration is the procedure of specifying the data type and unique name of the variable.

Syntax

datatype variableName;

Variable Initialization

Variable initialization is the procedure of assigning a valid value to the variable.

Syntax

variableName= variableValue;

There are different types of variables in Java and here we discussed all Java variable types in detail.

1. string

The string variables in Java are responsible for holding text. Use double quotation marks while using this variable type.

Syntax

string variableName= "variable value";

Example

string text;

text= "Hello World!";

System.out.printIn(text);

Output

Hello World!

2. int

The int variable is responsible for holding whole number values.

Syntax

int variableName= variableValue;

Example

int number;

number=12345;

System.out.printIn(number);

Output

12345

3. float

The float variable is responsible for holding floating-point numbers.

Syntax

float variableName= variableValue;

Example

float pi;

pi=3.1416;

System.out.printIn(pi);

Output

3.1416

 

4. char

The char variable is responsible for holding single characters. Use single quotation marks while using this variable type.

Syntax

char variableName= 'variable value';

Example

char a;

a= 'X';

System.out.printIn(a);

Output

X

5. boolean

The boolean variable is responsible for holding either true or false bool values. Use single quotation marks while using this variable type.

Syntax

boolean variableName= value;

Example

boolean flag;

flag= true;

System.out.printIn(flag);

Output

true

Conclusion

Variable are a vital part of any programming language that is used for the purpose of storing data values. These are basically memory locations in which assigned values are stored. Java has the following variables; string, int, float, char, and boolean. Each of these variable types along with their syntax and suitable example are discussed in this article.

About the author

Naima Aftab

I am a software engineering professional with a profound interest in writing. I am pursuing technical writing as my full-time career and sharing my knowledge through my words.