week10 - Week 10: Objects and Classes CS 177 1 Java types...

Info iconThis preview shows pages 1–14. Sign up to view the full content.

View Full Document Right Arrow Icon
CS 177 Week 10: Objects and Classes 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Java types There are two classifications of types in Java Primitive types: int double boolean char Object types: 2
Background image of page 2
Characteristics of primitive types Primitive types: Have a fixed amount of storage Think of them as a box designed to hold a particular kind of data item Have basic operations used to manipulate them int , double ( + , - , * , / , % ) boolean ( || , , ^ , ! ) 3
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Characteristics of object types Object types: Hold arbitrarily complex kinds of data of any kind Do not have a prespecified amount of storage Think of them as arrows pointing to some place in memory that holds primitive data Use methods for interaction instead of operators 4
Background image of page 4
Reference types are different Variables that hold object types are called references References do not work the same as primitive variables A primitive variable holds a value A reference variable merely points to the location of the object 5
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
How does this affect you? Picture a ham… Imagine that this ham is actually a Java object You may want a reference of type Ham to point at this ham Let’s call it pork pork 6
Background image of page 6
How many hams? Now, what if we have another Ham reference called bacon What happens if we set bacon to have the same value as pork using the following code? pork Ham bacon = pork; bacon n 7
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
There is only one ham! When you assign an object reference to another reference, you only change the thing it points to This is different from primitive types When you do an assignment with primitive types, you actually get a copy int x = 37; int y = x; y 37 x 37 8
Background image of page 8
Reference vs. primitive variables Since reference variables are only pointers to real objects, an object can have more than one name These names are called aliases If the object is changed, it doesn’t matter which reference was used to change it 9
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
A bite is not a byte Thus, if we tell bacon to take a bite away, it will affect the ham pointed to by pork Remember, they are the same ham pork bacon.bite(); bacon 10
Background image of page 10
Remember that primitives make copies We have int s x and y , both with value 37 If we change x , it only affects x If we change y , it only affects y int x = 37; int y = x; x++; y--; y 37 x 37 38 36 11
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
A reference is just an arrow If you declare a lot of references, you have not created any objects, just lots of arrows Planet mercury; DumpTruck truck; Idea thought; mercury truck thought 12
Background image of page 12
Where do those arrows point? When you first declare a reference
Background image of page 13

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 14
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/23/2012 for the course CS 177 taught by Professor Staff during the Spring '08 term at Purdue University.

Page1 / 40

week10 - Week 10: Objects and Classes CS 177 1 Java types...

This preview shows document pages 1 - 14. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online