Title: Objects and Names
1Objects 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
2What can you do with names?
The next few slides contain details of each of
these ideas
- Declare the type of the name
- JButton buttonUserPressed
- char middleInitial
- Give (assign) a value to the thing to which the
name refers - buttonUserPressed new JButton(press me)
- yourMiddleInitial C
- Refer to the thing to which the name refers, in
an expression -
- buttonUserPressed.isEnabled()
-
- numberOfTimesVisitedAlaska
- System.out.println(yourMiddleInitial)
These dot notation references are where the power
of an OOP language lies!
3Giving a name a type
- Java is a strongly typed language
- This means that every name has a type
- Declare names as follows
- JButton buttonUserPressed
- Book mobyDick
- int weight
- double temperature
- char middleInitial
- boolean isPressed
Can you guess WHY Java (and many other languages)
choose to be strongly typed?
The 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?
4Assigning values to names
- Given
- JButton buttonUserPressed
- int weight
- char middleInitial
- double temperature
- boolean isPressed
- Assign values with the operator
- buttonUserPressed new JButton(press me)
- middleInitial josephinesMiddleInitial
- weight 560
- temperature 98.627
- middleInitial W
- isPressed true
Read the operator as gets or gets the
value. It is NOT a test for equality we use
for that.
5Definition 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?
6Refer 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
7Object-type Names
- An object-type name (aka reference, label) is
just a label that can be stuck onto (an
appropriately-typed) object - Objects are always given object-type names.
- myPet new Dog("Labrador", "Larken", 11)
- familyDog myPet
- uglyDog null
myPet
uglyDog
?
familyDog
8Object-type versus primitive-type
- An object-type name is justa label stuck onto an
object - Objects are always given label names
- Dog familyDog myPet
- 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
Questions on Things, Types and Names?
9Things, 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) - Quiz Things, types and names
- Quiz 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
- Youll finish the exercise as homework and turn
in your own set of answers individually - Dont hesitate to ask questions!