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
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
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
Questions About Variables or Types?
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 Siebel 0216.
-
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 10: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 outside or head back to Siebel
Announcements
-
I have office hours today from 11AM–1PM in Siebel 2227. Please come by and say hi!
-
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.
-
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 02/03/2019.