Things, types and names -- discussion - Rose

Objects and Names
•
There are two types of things in Java:
– Objects
•
•
the button that the user just pressed
the URL of your home page
– Primitive-type things
•
•
•
The integer that represents the number of times you have visited Alaska
The character that represents your middle initial
We use names (also called variables) to refer to objects
buttonUserPressed
homeURL
mobyDick
and to primitive-type things:
numberOfTimesVisitedAlaska
middleInitial
Fundamentals of Software
Development I
Slide 1
What can you do with names?
1.
Declare the type of the name
The next few slides contain
details of each of these ideas
JButton buttonUserPressed;
char middleInitial;
2.
Give (assign) a value to the thing to which the name refers
buttonUserPressed = new JButton(“press me”);
yourMiddleInitial = ‘C’;
3.
Refer to the thing to which the name refers, in an expression
buttonUserPressed.isEnabled()
numberOfTimesVisitedAlaska < 5
System.out.println(yourMiddleInitial)
Fundamentals of Software
Development I
These dot
notation
references
are where
the power of
an OOP
language
lies!
Slide 2
Giving a name a type
• Java is a strongly typed language
– This means that every name has a type
• Declare names as follows:
Can you guess WHY
Java (and many other
languages) choose to
be strongly typed?
JButton buttonUserPressed;
Book mobyDick;
The
int weight;
double temperature;
char middleInitial;
boolean isPressed;
Fundamentals of Software
Development I
type name
pattern appears often in Java
These are the most common of the 8 primitive types.
The others are: long, short, float, and byte. What do
you notice about the names of all 8 primitive types?
Slide 3
Assigning values to names
• Given:
JButton buttonUserPressed;
int weight;
char middleInitial;
double temperature;
boolean isPressed;
• Assign values with the = operator:
userButtonPressed = new JButton(“press me”);
middleInitial = josephinesMiddleInitial;
weight = 560;
temperature = 98.627;
middleInitial = ‘W’;
isPressed = true;
Fundamentals of Software
Development I
Read the = operator as
“gets” or “gets the value”.
It is NOT a test for equality;
we use == for that.
Slide 4
Definition = Declaration +
Assignment
• Declare names by type-of-thing
name-of-thing:
– char firstLetterOfMyName;
– Dog myPet;
• Assign values with = [read it as "gets"]:
– firstLetterOfMyName = 'C';
– myPet = new Dog("Labrador", "Larken", 11);
• Define names by combining these:
– char firstLetterOfMyName = 'C';
– Dog myPet = new Dog("Labrador", "Larken", 11);
Questions so far?
Fundamentals of Software
Development I
Slide 5
Refer to things in expressions inside
statements
if (buttonUserPressed.isEnabled()) {
userCanStartGame = true;
}
numberOfTimesVisitedAlaska =
numberOfTimesVisitedAlaska + 1;
Much more on expressions and
statements in the next several sessions
Fundamentals of Software
Development I
Slide 6
Object-type Names
• An object-type name (aka reference, label) is just
a label that can be stuck onto (an appropriatelytyped) object
• Objects are always given object-type names.
– myPet = new Dog("Labrador", "Larken", 11);
– familyDog = myPet;
– uglyDog = null;
myPet
uglyDog
?
familyDog
Fundamentals of Software
Development I
Slide 7
Object-type versus primitive-type
myPet
• An object-type name is just
a label stuck onto an object
– Objects are always given label names
Dog familyDog = myPet;
familyDog
• A primitive-type name is like a dial: it has exactly one
value.
– Primitive types (int, double, char, boolean and a few others)
always have primitive-type names associated with them
int count = 9;
char grade;
boolean amISleepy = true;
Fundamentals of Software
Development I
Questions on Things, Types and Names?
Slide 8
Things, Types and Names: Exercise
• Find a partner who is sitting near you
• Each of you: do the Angel quizzes (per next items on
the schedule)
– Things, types and names -- Quiz on Types
– Things, types and names -- Quiz on Assignment
• Work as partners:
– Both partners: work together, problem by problem
• If you disagree on an answer or are unsure, ask an assistant
– Both partners: write your own answers
• You’ll finish the exercise as homework and turn in your own set of
answers individually
– Don’t hesitate to ask questions!
Fundamentals of Software
Development I
Slide 9