Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Understanding Parameter Passing, Objects and Object Reference Variables | CS 121, Lab Reports of Computer Science

Material Type: Lab; Professor: Gotel; Class: Computer Programming I; Subject: Computer Science; University: Pace University-New York; Term: Unknown 1989;

Typology: Lab Reports

Pre 2010

Uploaded on 08/09/2009

koofers-user-szl
koofers-user-szl 🇺🇸

10 documents

1 / 35

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS121/IS223 Week 12, Slide 1
CS121/IS223
Object Reference Variables
Dr Olly Gotel
ogotel@pace.edu
http://csis.pace.edu/~ogotel
Having problems?
-- Come see me or call me in my office hours
-- Use the CSIS programming tutors
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23

Partial preview of the text

Download Understanding Parameter Passing, Objects and Object Reference Variables | CS 121 and more Lab Reports Computer Science in PDF only on Docsity!

CS121/IS

Object Reference Variables

Dr Olly Gotel

ogotel@pace.edu

http://csis.pace.edu/~ogotel

Having problems?

-- Come see me or call me in my office hours

-- Use the CSIS programming tutors

Agenda

  • Make sure you have completed lab 1 and the

shopping questions in lab 2

  • Objects & object reference variables - what are these

object things anyway?

  • Understanding parameter passing – passing objects is

not the same as passing primitives

Finish Lab 2 after this week

Object Declaration, Creation, Assignment

Car myCar = new Car();

1 - Declare a reference variable , of class type Car, called myCar

2 - Create a new Car object

3 - Link the new object to the named reference variable

What is going on here? 3 distinct things…

Need to step back a moment…

(Called an object reference variable)

(1)

(3) (2)

What is Going On?

  1. Tells the JVM to allocate space for a reference variable on the

stack; reference variable is called myCar; it is of type Car

  • Stack – smallish area of memory; holds an address in

memory (where to find an object); we know how much

memory we need to hold an address

  1. Tells the JVM to allocate space for a new Car object on the

heap

  • Heap - big area of memory; holds the object because we do

not know how big it will be

  1. Assigns the new Car object to the reference variable myCar;

think of the reference variable as a locator for the object

Variables & Values in Java

  • Variables implemented as memory locations
  • Values implemented as 0’s & 1’s
  • Values placed in memory locations
  • The Java compiler needs to know the type of every

variable - to know the values it can take and

permissible operations

  • In Java, every variable must be declared before it can

be used - so it can allocate space in memory

Types in Java

  • Primitive types:
    • directly represented by typical processors – the

most efficient

  • begin with lowercase letter
  • Class types:
  • a type for objects
  • begin with UPPERCASE

letter

8 primitive types in

Java, others are

represented using

objects – sometimes

called object reference

types

Primitive Types in Java

  • byte – 8-bit integer
  • short – 16-bit integer
  • int – 32-bit integer
  • long – 64-bit integer
  • float – 32-bit floating point
  • double – 64-bit floating point
  • char – 16-bit unsigned Unicode character code
  • boolean – 1 bit true or false

Values are actual integers, floats, characters, etc

Integer data types – no fractions

Floating point data

types –fractions

A Way to Think of Primitives

  • We can predict these!
  • Think Predict/Primitive

Class Types in Java

  • Non primitive types exist & are important
  • Class types are abstractions created from primitive

types (like a bank account, an address, etc)

  • A class type that is widely used in java is String –

to represent a sequence of characters (char)

  • There is a class String that you use to implement

strings, so strings are actually objects, which requires

a bit of extra code to use

  • They are user-defined types

“This is a string”

  • strings are literal values

Variables in Java

  • A container of sorts
  • Must have a type & a name (identifier)
  • Can hold primitives or class types
  • When they hold primitive types they hold the values

directly in the container

  • When they hold class types, they hold references to

objects in the container (these are called object

reference variables), the objects are elsewhere

Pointers

In Java, we are VERY

concerned with references

Your object reference variable

actually points to the object!

Dog myDog = new Dog();

  1. Declare a reference variable:

Dog myDog = new Dog();

  1. Create an object:

Dog myDog = new Dog();

  1. Link the object & the reference:

Dog myDog = new Dog();

Object Declaration, Creation & Assignment

Example from [Sierra & Bates 2003] - best ever!

Dog type

Dog type

myDog

myDog

Dog object

Dog object

Index cards

Type Safety

Can’t do:

Dog myDog = new Cat();

myDog can only point to a Dog object!

But we could do:

Animal myDog = new Dog();

If Dog is defined as a type of Animal. Later!

Ouch!

CS121/IS223 Week 12, Slide 20

Assigning Ref Variables to Ref Variables

Dog myDog = new Dog();

Dog yourDog = myDog;

Dog type

myDog

Dog object

Heap – area of

memory where

you can request

big variable-sized

boxes

Dog type

yourDog

Stack - area of

memory for

storing

variables

(small boxes of

known size)

An alias

myDog.equals(yourDog)

1 object can be referenced by lots of variables