| CSCI A201/A597 Lecture Notes Five
Second Summer 2000
|
This is the one line summary of lecture five.
|
What is a number?
|
That much we know.
|
|
Let's say: an integer or a floating point number.
|
We know all about integers and floating point numbers.
|
|
What is an operator?
|
You want a long answer or a short one?
|
|
Short.
|
+, -, *, / are operators.
|
|
What is an expression?
|
A number, is a (very simple) expression. A symbolic name is
also a (very simple, atomic) expression.
|
|
What is an expression followed by an
operator followed by another expression?
|
That would also be an expression, wouldn't it?.
|
|
Yes, indeed. What do we have so far?
|
We have this table:
|
| Term defined | Composition |
| expression | number |
| | variable |
| | (expression operator expression)
|
|
|
Do we need the parentheses?
|
Not really, but we want to emphasize the structure.
|
Correct. Now, is ((3 + 4) * a) an expression?
|
It is. Do you want to show why with a diagram?
|
|
I sure do.
|
Very well, there you go:
|
|
That's interesting that we can define a concept
(such as expression) in terms of itself.
|
This is called a recursive definition. An
important part of a recursive definition is specifying
a set of fixed points,
|
|
... without which defining
something in terms of itself could go on for ever.
|
Exactly. Thank goodness for numbers and
variable names without which our definition
would be irreversibly circular.
|
|
What is an assignment statement?
|
Its structure is as follows: variable name, followed
by an equal sign, followed by an expression,
and then by a semicolon.
|
|
What are we trying to get at?
|
The structure of all Java programs.
|
|
What is a declaration?
|
It is a type, followed by a variable name and a
semicolon, or
|
|
... a type, followed by an assignment statement
(as defined above).
|
You should be building a few examples following these definitions,
|
|
... or take some statements and analyze them the way we analyzed the simple
expression a few lines above.
|
Are we really going to be extremely precise and cover all possible cases?
|
|
Not really. For that we'd have to sacrifice some of the intuitive structure
of all these.
|
But we'll go far enough for you to get a good grasp of the general
structure.
|
|
Are you ready for something really deep?
|
Go ahead.
|
|
What is a function call?
|
A function name followed by open parenthesis, followed by zero or
more arguments separated by commas, followed by closed parenthesis.
|
|
What is an argument?
|
I suppose any expression would work as one.
|
|
OK, let's summarize what we have so far:
|
|
| Term defined | Composition |
| expression | number |
| | variable |
| | (expression operator expression)
|
| funCall | funName ( ) |
| | funName ( arguments ) |
| arguments | expression |
| | expression, arguments
|
|
|
Any questions about function names?
|
Not really, I suppose they're basically
of the "absolute path" kind, like
System.out.println
|
|
Good assumption.
|
Are function calls expressions themselves?
|
|
I'm glad you asked. The answer is "yes" if the function returns a value.
|
Then we are now dealing with an even bigger infinity of expressions.
|
|
Note though that not all functions return values.
|
Yes, println from System.out does not
return a value, but sqrt from class Math
returns the square root of the argument, so it can be used in an
expression.
|
|
Let's update our table. We'll put a star (*) next to
remind ourselves that it's a logic error to use a function
call in an expression if the function does not return a value.
|
The update is minimal, but I think I need to
look at some examples before I become dizzy.
|
| Term defined | Composition |
| expression | number |
| | variable |
| | (expression operator expression) |
| | funCall * |
| funCall | funName ( ) |
| | funName ( arguments ) |
| arguments | expression |
| | expression, arguments
|
|
|
Here are some examples of expressions, as defined by the table above.
|
It's a table of syntactic categories described in terms of other syntactic
categories, an enterprise aimed at describing the (grammatical structure of
this) language.
|
|
Here are some more examples of expressions:
|
Take them apart, fit them in the table, don't just accept them and then move on.
|
Don't worry. Here they are:
-
Math.sqrt(a + Math.sqrt(b))
-
Math.sqrt(Math.sqrt(Math.sqrt(Math.sqrt(a))))
-
Math.pow(a, (1.0 / 16.0))
-
Math.sqrt(Math.pow(Math.sqrt(Math.pow(Math.sqrt(2)), 2)), 2)
-
(6 + (5 + (4 + (3 + (2 + 1)))))
|
|
Neat. What is a statement?
|
|
A statement is a declaration or an
assignment statement.
|
|
public class Template
{ public static void main(String[] args)
{
<methodBody>
}
}
It's the template we're using, and methodBody
is composed of one or more statements.
|
That is, declarations and statements
in any order, with the following final table describing the
entire structure of the language (so far).
|
| Term defined | Composition |
| statement | declaration |
| | assignment |
| declaration | type variable ; |
| | type variable = assignment ; |
| assignment | variable = expression ; |
| expression | number |
| | variable |
| | (expression operator expression) |
| | funCall * |
| funCall | funName ( ) |
| | funName ( arguments ) |
| arguments | expression |
| | expression, arguments
|
|
|
This table covers the syntax of the Java programs that we
are going to be writing for a while.
|
Note that not all syntactically correct programs are logically correct.
|
For example declaring a variable a twice is
a semantic error, although having two declarations
in a program is not a syntactic error...
|
... but if the variable is one and the same the semantic part
of the compiler will signal that.
|
|
All right, let's move on. Have
you heard of the latest late policy on assignments and take home
quizzes?
|
If you're late with an assignment you can get up to 80% of it if you
go and take an oral exam with the lab coordinator or the grader of
take home quizzes on the assignment you missed.
|
|
The oral exam is closed
book and it's subject to instructor's availability.
|
I guess it's better than nothing at all, if I miss an assignment.
But I like 100% better than 80% so I will keep turning things on time.
|
Fine. What is an int?
|
It's an integer number between -231 (which is about -2 billion)
and (231 - 1). If you need to refer to these boundaries in your program,
use the constants Integer.MIN_VALUE and Integer.MAX_VALUE
|
... which are defined in a class called Integer like Math.PI is
defined in the Math class.
|
Convention says: name constants using all caps
for the name of the constant.
|
|
How do you define constants?
|
Just use keyword final in front of type.
|
|
Here's a program, what does it produce:
|
Overflow.
|
public class Test
{ public static void main(String[] args)
{ int n = Integer.MAX_VALUE;
System.out.println(n);
n = n + 1;
System.out.println(n);
}
}
|
Have you run it?
|
Yes, it's an eye-opening experience.
|
|
Why does it happen?
|
Representation is finite.
|
|
What if we need bigger integers?
|
Use long.
|
What's a long?
|
It's a type that allows for the representation of bigger integers. The range is
now -263 (which is about -9 billion billions) to (263 - 1).
|
|
What if we need bigger integers?
|
Then work with objects of class BigInteger.
|
|
How do I do that?
|
We'll see that in a minute. What is a floating-point number?
|
A double or a float.
|
float spans the range from -3.4E38 to 3.4E38. double
is much wider: from -1.7E308 to 1.7E308 but both suffer from the same problem:
|
|
... precision.
|
Yes, what's the output of this program?
|
public class Test
{ public static void main(String[] args)
{ double a = 30000000000000000000000.0;
System.out.println(a - (a - 0.5));
}
}
It should be 0.5 by algebra.
|
Yes, but is that what the program prints?
|
|
Try it.
|
Also, try to initialize the double variable
with a value that doesn't contain the decimal point.
|
|
In that case the compiler catches the overflow before it happens.
|
Overflow happens even for double type.
|
The following program produces Infinity.
|
You're kidding me!
|
public class Test
{ public static void main(String[] args)
{ double a = 1.5E308;
System.out.println(a * 10);
}
}
For most of the programming projects in this book, the limited
range and precision of int and double
are acceptable.
|
Just bear in mind that overflow or loss of precision occur.
|
|
Another kind of loss of precision occurs in what is known as a
roundoff error.
|
What's that?
|
|
In the processor hardware, numbers are represented in the binary number system,
not in decimal.
|
You get roundoff errors when binary digits are lost, they just may
crop up at different places than you might expect.
|
|
Here's an example:
|
Another eyeopener.
|
System.out.println(4.35 * 100);
|
What do we do?
|
Keep a cool head. For example in this last case first round
then cast to an int...
|
... if you want an int, ...
|
... and especially if you want the right one.
|
|
How do you use big numbers?
|
If you want to compute with really large numbers, you can use
big number objects.
|
Big number objects are objects of the BigInteger and BigDecimalclasses in the java.math package.
|
Unlike int or double,
big numbers have essentially no limits on their size and precision,
|
|
... however, computations with big number objects are much slower than those
that involve primitive types. To perform operations you need to use methods
|
-
add
-
subtract
-
multiply and
-
divide
|
|
... that big number objects have.
|
Here's
a + b * c
with big numbers.
|
import java.math.*;
public class Test
{ public static void main(String[] args)
{ BigInteger a = new BigInteger("100000000000000000000000000000000000000");
BigInteger b = new BigInteger("200000000000000000000000000000000000000");
BigInteger c = new BigInteger("300000000000000000000000000000000000000");
BigInteger d = a.add(b.multiply(c));
System.out.println(d);
}
}
|
This is good exercise in object notation.
|
You bet. We've seen rectangles, strings, and now
big numbers. We're getting even better.
|
|
So what should we have achieved by now?
|
I think we understand integer and floating point numbers.
|
|
We're able to write arithmetic expressions in Java.
|
We can appreciate the importance of comments and good code layout.
|
|
We can define and initialize variables and constants.
|
We know of the limitations of int and double types,
|
|
... and the overflow and roundoff errors that result.
|
We can read program input with ConsoleReader.
|
|
We know how to print program output.
|
We understand the structure of a method body,
|
|
... which contains statements, such as declarations, assignments,
and function calls.
|
We've also talked expressions.
|
We have used the String class to define and manipulate strings.
|
By the way can you draw a picture for me for the following situation:
|
String a = "Hello!";
String b = a.toUpperCase();
I sure can:
|
Thanks. It is the same with substring, isn't it?
I know it is: String objects are called immutable objects.
|
That's right: toUpperCase on a String works
like add on a big number
|
... or like intersection on a Rectangle.
|
Last updated: June 26, 2000 by Adrian German for A201