Variables in Java


Creating variables with initial values

In Cinnameg, you create a name and give it a value using Let. In Java, the general form of the same thing is

  type name = value;
where type is the type of value that the variable holds, name is the variable name and value is an expression that gives the value. For example, Java statement
  int n = 0;
is roughly equivalent to Cinnameg statement
  Let n: Integer = 0.
It starts with the type, int, of the variable. Next comes the name, an equal sign, and the value. Notice the semicolon at the end. It is required, and the Java compiler will be very upset if you omit it.

Here are a few more examples.

  double x = 1.0;
  double frog = 2.0*x;
  boolean b = true;


Using variables

To use a variable, just use it in an expression. Do not write its type. For example, the definition of frog above uses x.

Later, we will see variables that hold values that are not numbers. No matter what kind of a thing a variable holds, regardless of how complicated that thing is, to use the variable, just write its name.


Changing the value of a variable

You can change the value of a variable using an assignment statement, of the form

  variable = expression;
For example, statement
  x = 2.0;
changes the value of variable x to be 2.0. It is rougly equivalent to a Relet statement in Cinnameg. Notice the semicolon at the end. It is required.

Notice that you do not write the type of the variable. You are not creating a new variable, but changing an existing one, and you only write the type to create a new variable.


Creating variables without initial values

Java makes a distinction between creating (or declaring) a variable and giving a value to the variable. You are allowed to create a variable without saying what value it has, as long as you are sure to give it a value before you use it. To create a variable that has no value, just write the type, then the name of the variable, then a semicolon. For example, the sequence of two statements

  int robot;
  robot = 1;
is equivalent to
  int robot = 1;


Creating several variables in one line

Often you create a few variables of the same type in one statement. Just use a comma between variables. For example,

  int m, n = 0, i = 1;
  double x = 2.0, y;
creates variables m, n and i of type int and variables x and y of type double. It also makes n initially 0, i initially 1 and x initially 2.0, but does not give a value to variables m or y.

You can only create several variables in one statement when they all have the same type.


An abbreviation

In Java, it is very common to add 1 to a variable or to subtract 1 from a variable. So Java has some shorthands for those common operations. You can write

  n++;
as a shorthand for
  n = n + 1;
and you can write
  n--;
as a shorthand for
  n = n - 1;


Problems

  1. [solve] Write a Java statement that creates a variable called mouse of type double and makes mouse initially hold 0.0.

  2. [solve] Write a Java statement that creates a variable called ladybug of type int and makes ladybug initially hold 10.

  3. [solve] Write a Java statement that creates a variable called flag of type boolean and makes flag initially hold false.

  4. [solve] Suppose that you already have a variable called ladybug, of type int. Write a Java statement that makes ladybug hold a value that is one larger than what it used to hold. For example, if ladybug holds 10 before doing the statement, then it should hold 11 afterwards.


Summary

To create a variable called horse of type int, write

  int horse;
To create a variable called zebra of type double, and give it an initial value of 1.0, write
  double zebra = 1.0;
Do not forget the semicolon at the end.

When you use or change a variable, do not write the type of the variable. For example,

  horse = horse + 1;
makes horse hold a value that is one larger than what it formerly held. Notice that the type of horse is not written.

There are some useful abbreviations. Statement

  x++;
abbreviates
  x = x + 1;
There is a similar abbreviation x-- that subtracts 1 from x.