Objects and Object References - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

Objects and Object References

Description:

There is only one cookie cutter, but can be used to make many cookies. ... Example of one object and two reference variables. String strA = new String ('Java' ... – PowerPoint PPT presentation

Number of Views:112
Avg rating:3.0/5.0
Slides: 12
Provided by: ocwKfu
Category:

less

Transcript and Presenter's Notes

Title: Objects and Object References


1
Objects and Object References
  • Lecture 9

2
Objectives
  • Java was designed to do object-oriented
    programming, abbreviated OOP. OOP is a
    programming technique that uses Objects.
  • Objectives
  • What is an object?
  • What makes an object?
  • Software Objects
  • Using a Class to make Objects
  • Constructors
  • Primitive data types versus Objects
  • Object References

3
What is an Object?
  • Think about the things in the world that are
    objects?
  • A pen is an object.
  • A computer keyboard is an object.
  • Bank account is an object
  • The class room is an object
  • So, what all objects have in common?
  • An object has identity (it acts as a single
    whole).
  • An object has state (it has various properties,
    which might change).
  • An object has behavior (it can do things and can
    have things done to it).
  • Example
  • you can think of your bank account as an object.
    Your account has properties (the balance,
    interest rate, owner) and you can do things to it
    (deposit money, cancel it) and it can do things
    (charge for transactions, appreciate interest.)

4
Software objects
  • It is convenient to have "software objects" that
    are similar to "real world objects." This makes
    the program and its computation easier to think
    about.
  • Software objects will have identity, state, and
    behavior just as do real world objects. Of course
    software objects exist entirely within a computer
    system and don't directly affect real world
    objects.
  • Software objects have identity because each is a
    separate chunk of memory.
  • Software objects have state. Some of the memory
    that makes a software object is used for
    variables which contain values.
  • Software objects have behavior. Some of the
    memory that makes a software object is used to
    contain programs (called methods) that enable the
    object to "do things." The object does something
    when one of its method runs.
  • Both variables and methods are called members
    of the object.

5
Using a Class to make Objects
  • To create an object, there needs to be a
    description of it. A Class is a description of a
    kind of object.
  • A class is like a cookie cutter that can be used
    many times to make many cookies. There is only
    one cookie cutter, but can be used to make many
    cookies.
  • Different cookies may have different
    characteristics, even though they follow the same
    basic pattern.
  • Cookies can be created. And cookies can be
    destroyed (just ask Cookie Monster). But
    destroying cookies does not affect the cookie
    cutter.

6
Example Program
class StringTester public static void main
( String args ) String str1 // str1 is
a variable that refers to an object, //
but the object does not exist yet. int len
// len is a primitive variable of type int
str1 new String("Salam Shabaab")//create an
object of type String len str1.length() //
invoke the object's method length()
System.out.println("The string is " len "
characters long")
  • When a programmer wants to create an object the
    new operator is used with the name of the class.
    Creating an object is called instantiation.
  • Once the object has been created (with the new
    operator). The variable str1 is used to refer to
    this object.
  • Java uses "dot notation" to access any of its
    members as follows
  • referenceToAnObject.memberOfObject
  • Method names have "()" at their end. Often there
    is additional information inside the "()", but
    they are required even if they contain nothing.

7
Constructors
  • A constructor has the same name as the class. The
    line from the above program
  • str1 new String("Salam Shabaab")
  • creates a new object of type String. The new
    operator says to create a new object. It is
    followed by the name of a constructor. The
    constructor String() is part of the definition
    for the class String.
  • Constructors often are used with values (called
    parameters) that are to be stored in the data
    part of the object that is created. In the above
    program, the characters "Salam Shabaab" (not
    including the quote marks) are stored in the new
    object.
  • There are usually several different constructors
    in a class, each with different parameters.
    Sometimes one is more convenient to use than
    another, depending on how the new object's data
    is to be initialized.

8
Primitive data types Objects
  • In Java, a piece of data either is of a primitive
    data type or is an object data type.
  • The only type of data a programmer can define is
    an object data type (a class). Every object in
    Java is an instance of a class. The class
    definition has to exist first before an object
    can be constructed.
  • A programmer may define a class using Java, or
    may use predefined classes that come in class
    libraries (like String).

9
Object References
  • An object reference is information on how to find
    a particular object. The object is a chunk of
    main memory a reference to the object is a way
    to get to that chunk of memory.
  • Objects are created while a program is running.
    Each object has a unique object reference, which
    is used to find it. When an object reference is
    assigned to a variable, then that variable says
    how to find that object.
  • In Java, there are only primitive variables and
    object reference variables, and each contains a
    specific kind of information

10
Object References - Examples
  • Example of two objects and two reference
    variables
  • Example of one object and two reference variables

String strA new String (Java) String strB
new String (C)
String strA new String (Java) String strB
strA
strB
11
Equality of Reference Variable Contents
  • The operator does NOT look at objects!   It
    only looks at references (information about where
    an object is located.)
Write a Comment
User Comments (0)
About PowerShow.com