Primitive types, variables, and expressions
> Click or hit Control-Enter to run the code above
Student Group Announcements
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
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 later
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.)
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
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
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.
We’re discussing floating point representation in lab this week—it’s pretty cool.
Questions About Variables?
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 1.
-
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 Lecture
Please don’t bum rush the stage after lecture.
-
I teach until 9:50, but that means I have to scoot immediately after class
-
I’ll meet anyone who has questions in the lobby and then we’ll either hang out on the steps or head back to Siebel
Enrollment Update
If you want to enroll in CS 125, you will probably be able to. Just be patient.
-
We’re opening up a few more seats and lab capacity will match lecture capacity
-
We’ll also lose some students over the next week or two
I Want You Here Until December
Announcements
-
Homework continues today.
-
Friday we will continue with the basics of imperative programming, including conditionals and loops.
-
Our quiz on course policies continues today in the CBTF. Please sign up and take it.
-
MP0 will be Friday. Get your environment set up in lab this week.
-
Office hours will start tomorrow and be posted on the calendar.
-
Please fill out the initial student survey. 1% extra credit for anyone who does by Sunday 09/09/2018.