Introductory Track for A290/A590 Spring 2008 - Notes prepared by Adrian German
Additional references from Books24x7: Java 5: A Beginner's Tutorial by Budi Kurniawan (Brainy 2006)
and Java Programming for Engineers by Julio Sanchez and Maria P. Canton (Auerbach 2002)


Lecture Notes Three: Variables and types. Keyboard input.

Types of Numbers (Numeric Types)

Last time we looked at two types of numbers:

We will use int to denote the first category of numbers.

(Occasionally long will also be used and explained.)

We will use double as the label for the second category.

(Although float will also be used from time to time).

We use numbers to create expressions, which is a way of calculating other numbers.

The operators we looked at are:

If the two operands in an expression are both integers the result is also an integer.

If one of the two operands in an expression is a double the result will be a double.

So, some examples are:

Precedence of Operators

Some operators have precedence over others:

Associativity

The order of the operations can be specified by parentheses.

In their absence operations of the same precedence are to be performed left to right.

Here are some examples:

This last one is (hopefully) clear.

Some more examples:

Math.sqrt(...) and Math.pow(...)

Instead of operators we use:

What can we do with these expressions outside of printing their result?

We can store the number they evaluate to, for later use.

For that we use variables, which are:

names for locations that can keep values of a certain type.
The only types we know thus far are:

Declaring Variables

Here's how you declare and initialize a variable of type int:
int n; // declaration 

n = 1; // initialization to 1
n = 1; is an assignment statement:

Here's how you declare and initialize a variable of type int:
double m; // declaration 

m = 1.0; // initialization to 1
You will notice that in the last example
m = 1;
would have worked just as well.

1 is compatible with double and m is now 1.0 as a matter of fact.

In the previous example

n = 6 / 3; 
would have also been correct, but
n = 6.0 / 3.0; 
would give an error. There's no room for the decimal part in n even if it's 0 (zero).

A variable should not be declared more than once.

A variable can changed its value any number of times.

Compatible Types (Casting)

If types are compatible they can also be converted:
m = 6; 

n = (int)2.3 // n contains 2 now
The last assignment uses casting.

In this case we drop the decimal part on purpose.

Reading from the Keyboard (java.util.Scanner)

The rest of this lecture will introduce java.util.Scanner, which is documented here (and in your books!).

You only need to know how to use it.

A Complete Example

So we will discuss this program:
import java.util.*;

/**
 * This program demonstrates console input.
 * @version 1.10 2004-02-10
 * @author Cay Horstmann
 */
public class InputTest
{
   public static void main(String[] args)
   {
      Scanner in = new Scanner(System.in);

      // get first input
      System.out.print("What is your name? ");
      String name = in.nextLine();

      // get second input
      System.out.print("How old are you? ");
      int age = in.nextInt();

      // display output on console
      System.out.println("Hello, " + name + ". Next year, you'll be " + (age + 1));
   }
}

Converting Strings into Numbers

The last thing we need to make clear is that strings of characters are different.

You can't say:

int n = "12"; // won't work
If you really want something like that you'd have to convert:
int n = Integer.parseInt("12"); // now n is set to 12 
If the conversion fails you will find out, the program will crash.

A Review on Strings

Strings are indicated by characters grouped between double quotes.

You need a backslash in front of it (as in \") to indicate a double quote inside a string.

(The backslash is an escape character. It disables the normal meaning of a double quote, as a delimiter.)

The backslash in front of a normal lowercase n indicates a newline.

(You wouldn't otherwise be able to write a newline in the string, since that would break the line.)

To indicate a backslash use the backslash in front of a backslash: \\.

So "\"\\\n" prints as a double quote followed by a backslash and a newline.

Strings could be concatenated using +.

Thus "blue" + "berry" evaluates to "blueberry".

There will be a few more rules to keep in mind.


Last updated by Adrian German for A290/A590 on Sun Apr 06 16:05:28 EST 2008