Objects and Names - PowerPoint PPT Presentation

1 / 9
About This Presentation
Title:

Objects and Names

Description:

myPet = new Dog('Labrador', 'Larken', 11); Define names by combining these: ... Objects are always given label names. Dog familyDog = myPet; ... – PowerPoint PPT presentation

Number of Views:73
Avg rating:3.0/5.0
Slides: 10
Provided by: davidmutch8
Category:
Tags: dog | names | objects

less

Transcript and Presenter's Notes

Title: Objects and Names


1
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

2
What 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!
3
Giving 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?
4
Assigning 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.
5
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?
6
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
7
Object-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
8
Object-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?
9
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)
  • 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!
Write a Comment
User Comments (0)
About PowerShow.com