Lecture 3: Numeric Variables
Objectives
- Create and assign numeric variables
- Simplify expressions with constants to single value
- Convert values by casting
Integer variables
Why are numbers and numeric types useful in computation?
- Control systems using mathematical calculations, like SpaceX
- Articial Intellgience and Deep Learning: billions of numeric calculations to train these models
- Other examples?
Integer operations
Binary operators
Java has the four mathematical operators you would expect, plus modulus:
- addition:
+
- subtraction:
-
- multiplication:
*
- division:
/
- modulus:
%
Most of these work the way you would expect them to on integers. However, integer division is a bit tricky, since it’s possible to divide two integers and not have a whole number as the result (such as 5 / 2
). In Java, however, if the compiler sees that that only integers are being used, it will perform integer division. That is, the result is rounded down to the nearest integer. Thus, 1 / 4 = 0
and 21 / 6 = 3
. Later, when we work with real numbers, we will see that, to compute a real-valued expression, at least one of the numbers needs to be real-valued (i.e. 5.0 / 2.0 = 2.5
).
The modulus (%) operator is probably new for you, and it is used to return the remainder of a division. For example, 5 % 2
evaluates to 1
, while 6 % 2
evaluates to 0
, because there is no remainder.
When evaluating complex arithmetical expressions, such as 5 + 3 * 2
, Java uses the same order of operations as you’re used to, and evaluates left-to-right. To be sure, I recommend you use parentheses explicitly to make this order of evaluation and precedence obvious or as needed: (5 + 3) * 2
.
Unary operators
Java also has unary operators that operate on a single variable (as opposed to a pair of them like x + y
):
- addition:
++
- subtraction:
--
Let’s see how these unary operators work in Java with this example.
Real-valued variables
Above we saw that Java has the int
type, but we can also store real numbers. We refer to these as floating point numbers due to how they’re represented in memory.
There are two types in Java for floating point numbers (such as 3.14
, 12.33333333
, and 3.0
): float
and double
. The difference is in how many bits we can store in these values; we will end up using double
because it’s easier.
Casting
Sometimes, we want to convert an integer to a double, or a double to an integer (for example). In Java, casting allows us to do this explicitly. There are also some casts that are implicitly possible.
For example, imagine we would like to actually store a fraction:
int top = 1;
int bottom = 3;
int fraction = top / bottom; //this evaluates to zero, because 3 does not fit inside 1 at all (integer division)
Here, we have to make top
or bottom
in the expression a floating point number in order for it to actually store 0.333333
. One way would be to declare one of these as such explicitly:
double top = 1.0;
int bottom = 3;
double fraction = top / bottom; // now doing floating point division
and now you would get the desired answer. However, you could also use casting:
int top = 1;
int bottom = 3;
double fraction = top / ((double) bottom); // now doing floating point division
Here, (double)
means to convert the value in bottom
from 3
to 3.0
and use 3.0
in the rest of the expression. However, bottom
still remains an integer.
You can also force a floating point number into an integer; this just removes the remainder:
int num1 = 1;
int num2 = (int) 3.7; // will store 3 in num2
int num3 = (int) 3.0; // will store 3 in num3
Some of these casts, when “unsurprising”, Java will even do automatically:
double num1 = 3; // will automatically cast to 3.0
However, when you would be losing values/precision, the compiler will not be happy:
int num1 = 3.3; // will not compile.
Homework 1
Let’s go through the first Homework 1 problems there are a few additional details to how homework problems are scored that we should discuss together now, before you learn those pieces later this semester. Keep in mind that for all homework this semester:
- You must complete your homeworks indivdually – groupwork is not permitted. You are welcome to get help from TAs or Ed, however.
- Do not use ChatGPT (or another LLM) on any homework assignments. You are not learning much from doing that, and worse, you’re not learning to debug at all (which is what you’ll spend most of your career doing as a developer). You will have a tough time on the quizzes this semester if you don’t get enough individual practice writing and debugging your own code.
Next class
We will continue to work on HW1 problems the next class