Expressions and variables

Variables in programming are much like variables in algebra. These variables can store different data. However, each variable can only store a certain kind of data. This restriction seems arbitrary, but helps the computer do a proper job with handling the data. For instance, a computer will handle words differently than it handles numbers.

In Java, variables can be either a primitive type or an object type. Primitive types are the building blocks of object type variables.

Java's four main primitive types are:

For our numerical types (int and double), we can write them as mathematical expressions using the five primitive arithmetic operations Java provides:

The order of operations from math are used.

When we mix types together (for instance, adding an int to a double), the result will be in the more general type. For instance, if I add 3 (an int) to 2.0 (a double), I get 5.0 (a double) because you can represent all whole numbers as decimal numbers, but not the other way around.

How do we form words using the primitive types? Well, we can't. However, we can use a built-in object type called String to represent a word. A String is really just a bunch of chars strung together.

Now that we have a few different variable types, we can create variables.

A variable can be created by doing two things:

  1. declaration - saying the variable exists and will be called something. When we declare a variable, we're also setting the type of the variable. The type cannot be changed later on.
  2. assignment - giving the variable an actual value.

In the next two lines, we first declare a variable, then set a type for it.

In the last line, we do both of these things in the same line.

String name;
name = "John Doe";
String address = "123 University Ave";

Once you have assigned a value to a variable, you can now use its value. You can also change the value of the variable, as long as the new value is still a valid value under the data type it was first declared as. So for instance, you cannot assign a decimal number to an int. Furthermore, these variables can be incorporated into expressions, just by referring to them by their variable names.

For instance:

int a = 2;
int b = 3;
System.out.println(a * b);

We've created an expression a * b, which should multiply the value of a by b. We substitute the value of the variables into the expression. Hence, since a is 2 and b is 3, a * b means 2 * 3 in this case. Thus, the output from the print statement should be 6.

Expressions can also evaluate to true or false responses. In other words, they can answer yes or no questions. Technically speaking, they will still return a boolean value, which is either true or false. For instance, x == 4 is an expression that will return either true or false.

A few operations that we can use for comparison, which return boolean values, include:

There's usually a lot of confusion between = and ==. The = operator is used for assignment of a new value. For instance, when you do:

int diff = x - y;

you are assigning a new value to the diff variable. While it's true the variables are equal during this line, you could later in your program change the value of diff to something else. So = doesn't necessarily mean the left and right side are the same.

The way you check for equality of the two sides is by using the == operator. If you say diff == 5, then this will check whether diff is equal to 5 without assigning the value 5 to it.

One thing to realize is that we also use the + operator to denote another operation for Strings: concatenation. This is a fancy word for combining strings together. Since we cannot mathematically add up words, we can use + to denote we're stringing up the Strings together. For instance:

String x = "The best university in the world is ";
String y = "The University of Texas at Austin";
String z = x + y;
System.out.println(z);

Then the print statement would print out the value of z, which is "The best university in the world is The University of Texas at Austin".

Back to top

Back to list of tutorials

© 2019–2024 Jeffrey Wang. All rights reserved.




In loving memory of Haskell, 19??-2001.