Primitive types, variables, and expressions
> Click or hit Control-Enter to run the code above
You’re Learning a New Language
And your computer is a very eager but unforgiving conversant
> Click or hit Control-Enter to run the code above
Our Approach
-
Start small: with just a few lines of code
-
Work up to slightly more complicated examples
-
Then to complete functions and algorithms
-
And eventually to understanding entire files of Java code
Quantity v. Quality
The ceramics teacher announced on opening day that he was dividing the class into two groups. All those on the left side of the studio, he said, would be graded solely on the quantity of work they produced, all those on the right solely on its quality. His procedure was simple: on the final day of class he would bring in his bathroom scales and weigh the work of the "quantity" group: fifty pound of pots rated an "A", forty pounds a "B", and so on. Those being graded on "quality", however, needed to produce only one pot—albeit a perfect one—to get an "A".
Well, come grading time and a curious fact emerged: the works of highest quality were all produced by the group being graded for quantity. It seems that while the "quantity" group was busily churning out piles of work—and learning from their mistakes—the "quality" group had sat theorizing about perfection, and in the end had little more to show for their efforts than grandiose theories and a pile of dead clay.
What Are Computers Good At?
-
Basic math
-
Simple decision making
-
Doing things over and over again very, very fast
-
Storing data
-
And communicating
Variable Declaration
// I declare a variable named c of type char
char c;
// I'm going to use an int and call it first
int first;
// I hereby create a boolean and shall name it isSet
boolean isSet;
-
In Java every variable has a name and a type
-
The name is for you, the programmer
-
The type is both for you and for Java
Variable Names Must Be Unique
// I declare a variable named c of type char
char c;
// This doesn't work. How will I tell them apart?
int c;
// Even this doesn't work if c has already been declared.
char c;
-
There are rules allowing the same name to be used in multiple places in the same program
-
We’ll learn about them soon
Good Variable Names
Choosing good variable names will make your life a lot easier as a programmer.
Good variable names are:
-
Descriptive
-
Indicative of the variable’s function
-
As succinct as possible…
-
But see #2 above
-
This will make more sense as we go along
Variable Naming Example
Just so that you know that I practice what I preach:
for (int i = startingPoint; i < doubles.length; i++) {
if (doubles[i] > maximum || i == startingPoint) {
maximum = doubles[i];
}
}
(This is code from the Spring 2018 MP0 solution set.)
Poetic Programming
Code is speech; speech a human utters to silicon, which makes the machine come to life and do our will.
Comments
// Also: this is a single-line comment.
// It starts with a //
/*
* Here is another one. Multiline comments start with /* and end with
*/
// Comments are ignored by the computer, but can be
// some of the most important parts of your code
// They're for you and other humans
Variable Types
Java has eight primitive data types.
All other data in Java is represented by combinations of these building blocks. You can break them into four categories:
-
Integers:
byte
,short
,int
,long
-
Floating point numbers:
float
,double
-
Character:
char
-
True or false:
boolean
This is About Data
And how computers represent information.
Variable Initialization
// I declare a variable named mine of type float
// and initialize it to 0.1
float mine = 0.1;
// Let there be a boolean called isItSnowing
// and initialize it to false
boolean isItSnowing = false;
// Declare timeSince1979 of type long
// and initially set it to 1204209
long timeSince1979 = 1204209;
Experimenting With Initialization
> Click or hit Control-Enter to run the code above
Literals
A literal is a number or other value that appears directly in the source code.
// 1000 is a long literal. Note the L suffix.
long big = 1000L;
// 'g' is a character literal.
char one = 'g';
// true and false are boolean literals.
boolean itsEarly = true;
boolean iSleptWell = false;
Variables Can Be Modified
> Click or hit Control-Enter to run the code above
Variables Must Maintain the Same Type
> Click or hit Control-Enter to run the code above
Variables Can Be Modified Using Other Variables
> Click or hit Control-Enter to run the code above
This is Not Algebra
> Click or hit Control-Enter to run the code above
This is Not English
> Click or hit Control-Enter to run the code above
What Makes Primitive Types Primitive?
-
They can all be stored by the computer as a single number.
But wait… what about char
?
Our First Example of a Convention
There’s no law of the universe that says that the number 97 should represent 'a'.
It’s just what we’ve all agreed on.
Why Are There Multiple Numeric Types?
-
Integers:
byte
,short
,int
,long
-
Floating point numbers:
float
,double
Different types take up different amounts of computer memory and so can store different values.
Don’t worry too much about how things are stored yet. But the limits are important to be aware of.
Type Limitations
> Click or hit Control-Enter to run the code above
An Analogy
Variables are like a container that can hold data. Types are rules about what you can store in each container.
Questions About Variables or Types?
Cheating
I take academic integrity extremely seriously. Cheaters will be caught and punished.
EMP
CS 199 EMP (Even More Practice) is a chance for you to get (even) more practice.
-
It’s held Thursday nights from 5–7PM in Everitt 2310.
-
You can register for it for one credit. If you do you’ll need to attend regularly.
-
You can also not register and show up when you want. It’s open to all.
-
More details on the website.
After Class
Please don’t bum rush the stage after class.
-
I teach until 10:50, but that means I have to scoot immediately after class
-
I’ll meet anyone with questions during my office hours.
Announcements
-
We’ll start limited 12–4PM office hours next Monday, and full 12–8 office hours on Monday 2/3/2020 1.
-
Homework continues today.
-
Monday we will continue with the basics of imperative programming, including conditionals and loops.
-
Take our intro survey! 1% extra credit for anyone who does by Sunday 2/2/2020.