Variables

In order to allocate space for data and give a name to the data
(so that we can easily refer to different pieces of data), Java
makes us declare our data.

When we declare a new piece of data, we must provide two things:
a name for the data, and what type the data will be.

These are called variable declarations as the value of
any piece of data may vary through the course of the
program.

The general format is the following:

DataTypeVariableName;

We've already seen many of these in our examples from
yesterday.

intclickCount;
Imagebackground;
Stringlocation;
Graphicsg;

Just remember that all variables must be declared before
using them.

The declaration tells Java how much space to allocate for a given
variable, what name will be used to refer to it, and, based
on its type, how that variable may be used later on.

Variable Naming

It's important to remember the following when working with
variables...

Variable names are case-sensitive! So, variable
beardSize is completely different from the
variable BeardSize.

Variable names must not contain spaces!.

Variable names can contain any lowercase letter ('a' through 'z'),
uppercase later ('A' through 'Z'), number ('0' through '9'), and
even a few more characters (underscore, etc.).

Variable names must not start with a number!

NOTE: While variable names like
x and y2 are perfectly acceptable variable names,
they are difficult to follow... Ideally, always use variable names
that are a descriptive as possible, where they describe
something about what the variable represents and how it is used.

Data Types

As you've seen above, we must always include a type when
declaring variables.

Java has many data types, but we distinguish between
two important classes of data types...

Primitive Data Types are those that are the most basic
data types, which are the building blocks for all other data
types.

Complex Data Types are those which are built using
the primitive data types and even other complex data types.

We'll see more about these shortly.

Primitive Data Types

int. Used to represent integers,
which are whole numbers (e.g. -2, -1, 0, 1, 2, 3). It can
represent whole numbers between about -2 billion and
+2 billion. This will be the most commonly used data
type.

float. Used to represent decimal numbers,
such as 1.2, 3.14, -78.3, etc. Note that like int,
float can only represent finitely many numbers, but
there is no range like there is for int (why?)

boolean. Used to represent the values
true and false. That's it! Comes in handy
as we'll see.

Complex Data Types

Java provides additional data types that are built using the
primitive data types and even other complex data types.

As we'll see later this week, we can even define our own
data types in Java, using class's.

Some of the more common complex types we've seen are:
String, URL, Image, JButton,
JPanel, etc.

Operators

Java includes many operators for working with the
primitive data types.

These include things like addition (+), subtraction (-), and
more.

The most commonly used operator, by far, is the Assignment
operator "=".

It is used in the following manner:

Variable=Expression;

When Java executes this line of code, the Expression will
be evaluated, and whatever value it results in will be assigned
to the variable Variable.

In the above examples, all of the assignments took place when we were initializing
variables. This need not be the case! We could have defined all of those variables
elsewhere, and then done the following...

NOTE: It's good to think of these operators as
functions that take two parameters (both of the same type) and
returns true or false if the relation specified
is, in fact, true or false. That is, these operators always
return a boolean value!

Logical Operators

The relational operators above provide a convienant means for
building up logical expressions or questions in Java.

Often, we want to combine multiple questions together, and Java
provides the following Logical Operators to do just that.

To see if two boolean values are both true, one can use
"and" (&&).

To see if at least one of two boolean values is true,
one can use "or" (||).

Notice in the above example, where we checked to see if any discounts applied to the price of
admission, the senior discound and child discount were the same. We could use the "or" operator
to check both conditions at once...

// Suppose we want to check the age of a customer to see if any discounts apply...
price = 20; // normal price of admission (in dollars)
age = input.getText(); // get the age of the customer from an input// Check if any discounts apply
if (age < 18 || age >= 65) {
output.setText("Discount applies.");
price = 10;
} else {
output.setText("Regular Price.");
}