Variables

After deciding on a type and a name, you can declare the variable.

Here are a few variable declarations:

int age;
double cost;
char answer;

You can also declare variables with an initial value:

int age=19;
double cost=4.85;
char answer='Y';

White space, blanks, tabs and new lines are also allowed:

int age =    19;
double 
  cost = 4.85;
char answer  =  'Y';

You can also declare more than one variable at a time:

int num1, num2, num3;

Active Learning!

Think of something that could be described using variables. Write statements to declare variables with initial values. For example you might describe a house as:

int bedrooms=4;
double baths=2.5;
String address="35 Main Street";
boolean gasHeat=true;

Work in groups or share your description with others. Make sure that others can understand the variable names that you use. Others should be able to identify the object you have described.

Group activity

Each person is to describe themselves by declaring 4 variables with initial values on a piece of paper.

For instance you might describe yourself as follows.

int age=22;
char gender='M';
String city="Centerville";
boolean married=false;

Put all the pieces of paper in a bag and everybody draws one, then tries to identify the person described.

NEXT: