Java : Declaration Statements


Java declaration statements are one kind of statement, where declare a variable with specifying it’s data type and name.

A variable is named container (identifier) that holds values used in a Java program. A variable can be initialize with some value and can also have calculated values from expressions which can vary over time.

Example of variable declarations in Java

The following statements are declaration of variables of int, boolean and String. Here these variables will initialize with default values.

See also: Java : Primitive Type Default Value and Range

Note: Declaration statements always end with semicolon(;).

int perk;
int salary;
boolean isValid;
boolean isCompleted;
String empFirstName;
String empLastName;

In statement declaration, apart from declare variable and type we can also initialize with some literal values.

int perk=20000;
int salary=50000;
boolean isValid=true;
boolean isCompleted=false;
String empFirstName="Saurabh";
String empLastName="Gupta";

To reduce number of lines of code and more clarity, we can also declare same type, more than one variable on same line with comma separated.

int salary, perk=20000;
boolean isValid=true,isCompleted;
String empFirstName="Saurabh",empLastName="Gupta";
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s