Variables

In your programs, you can declare variables that help you store information. These storage devices are called variables because you can vary the information stored there.

Data types

Java is a strongly typed and statically typed programming language. This means that whenever we declare a variable we must give it a data type, and this type cannot change over the course of the program. The data type specifies what kind of information you plan to store in the variable.

Below is a table of common types in Java and their uses:

Type Use
int whole numbers, such as 4 and -23
char single characters, such as ‘a’
double numbers with decimals, such as 3.14
boolean boolean values: either true or false
String a sequence of characters, such as “Hello”

There are other types in Java, but these are the ones we will use most in CIS 200.

Declarations

You can declare a variable like this:

type name;

Here, type is one of the types in the table above, and name is the name we’re giving the variable. We must follow these rules when we name a variable:

  • The name should be a sequence of upper-case letters, lower-case letters, numbers, and underscores
  • The first character in the name must be either a letter or an underscore

Here are some examples of variable declarations:

int num;
double val1;
char _letter;
String Name;
boolean check_done;

Variables in Java are case-sensitive. This means that if you change the capitalization in a variable name, then it does NOT refer to the same variable. For example, we can do this:

int num;
int Num;

and num and Num will be two different variables. (Note: this practice is not recommended because it causes confusion.)

Assignments

After we have declared a variable, we can assign it a value. We CANNOT use a variable in any way before it has been declared. A variable assignment looks like this:

name = value;

Here, name is the name of the variable that you declared, and value is the value you’re giving it. The value you assign a variable must have the same type as the variable. (For example, if the variable is an int, then you can’t store a number like 2.34 in it.)

Here are some examples of valid assignments using the variables declared above:

num = 42;
val1 = 3.21;
_letter = 'A';
Name = "Fred";
check_done = true;

Notice that single characters (chars) must be encolosed in single quotes, while strings must be enclosed in double quotes. We can also declare a variable and assign it a value at the same time. For example, we can do:

double pi = 3.14159;

Type casting

We cannot give a value to a variable if it does not have the appropriate type. For example, we cannot do:

//This is illegal!
int num = 2.3;

However, sometimes we want to convert a value to have the appropriate type. We can do this by putting the type we want in parentheses in front of the value. For example:

int num = (int) 2.3;

This statement converts 2.3 into an int, so that num now has the value 2. We can do a similar thing to convert the types of variables:

double x = 6.75;
int y = (int) x;

Now y has the value 6. This conversion of one type to another is called type casting.