| 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) |
Last time we looked at two types of numbers:
- integers (such as
-5,23,10345and so forth) and- floating-point numbers (examples include:
5.7,12.0,-3.14, etc.)We will use
intto denote the first category of numbers.(Occasionally
longwill also be used and explained.)
We will use
doubleas the label for the second category.(Although
floatwill 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:
-(unary minus, as in-5)+(addition for numbers, concatenation forStrings)*(multiplication)/(division)%(remainder)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
doublethe result will be adouble.So, some examples are:
1/2evaluates to01.0/2evaluates to0.5
Some operators have precedence over others:
- unary minus has the highest precedence (must be done first)
- multiplication and division (and remainder too) come next
- addition and subtraction have the lowest precedence
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:
1 + 2 - 3evaluates to01 - 2 + 3evaluates to21 / 2 * 3evaluates to03 * 1 / 2evaluates to11.0 / 2 * 3evaluates to1.51 / 2 * 3.0evaluates to0This last one is (hopefully) clear.
Some more examples:
2 + 3 * 4evaluates to142 * 3 + 4evaluates to102 * (3 + 4)evaluates to14
Math.sqrt(...) and Math.pow(...)Instead of operators we use:
Math.sqrt(x)to calculate the square root ofxMath.pow(x, y)to raisexto the power ofyWhat 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:
int(for integers) anddouble(for floating-point numbers)
Here's how you declare and initialize a variable of typeint:int n; // declaration n = 1; // initialization to 1n = 1;is an assignment statement:
Here's how you declare and initialize a variable of type
- it has two parts separated by the equal sign
- on the left is the name of the variable whose value is being changed
- on the right there's an expression that must be of compatible type with the variable
- the expression is evaluated first, then the value is assigned to the variable
int:You will notice that in the last exampledouble m; // declaration m = 1.0; // initialization to 1would have worked just as well.m = 1;
1is compatible withdoubleandmis now1.0as a matter of fact.In the previous example
would have also been correct, butn = 6 / 3;would give an error. There's no room for the decimal part inn = 6.0 / 3.0;neven if it's0(zero).A variable should not be declared more than once.
A variable can changed its value any number of times.
If types are compatible they can also be converted:The last assignment uses casting.m = 6; n = (int)2.3 // n contains 2 nowIn this case we drop the decimal part on purpose.
java.util.Scanner) The rest of this lecture will introducejava.util.Scanner, which is documented here (and in your books!).
You only need to know how to use it.
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)); } }
The last thing we need to make clear is that strings of characters are different.You can't say:
If you really want something like that you'd have to convert:int n = "12"; // won't workIf the conversion fails you will find out, the program will crash.int n = Integer.parseInt("12"); // now n is set to 12
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
nindicates 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.
Sun Apr 06 16:05:28 EST 2008