Home / Java Patterns and Pitfalls / Java Patterns and Pitfalls     frequal.com

Lesson 0 -- Variables

Variables let you store information like numbers, words, or sentences. A variable has a type and a name.

If the program says

int yearBorn = ask("In which year were you born?");
then it is storing the answer to the question in the variable called yearBorn. The variable is of type "int", a number (integer). That year can be used later by using the variable name. For example, to compute how old someone is, subtract their year born from the current year.
int age = 2013 - yearBorn;
Then print this out:
System.out.println("You are this old: " + age);


  1. Have the guessing program tell you how far off your guess is.

Last modified on 14 Jun 2013 by AO

Copyright © 2020 Andrew Oliver