variables, primitives, and objects a visual learner’s guide

28
Variables, Primitives, Variables, Primitives, and Objects and Objects A Visual Learner’s Guide

Upload: erick-atkins

Post on 13-Jan-2016

222 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Variables, Primitives, and Objects A Visual Learner’s Guide

Variables, Primitives, and ObjectsVariables, Primitives, and Objects

A Visual Learner’s Guide

Page 2: Variables, Primitives, and Objects A Visual Learner’s Guide

What Exactly ARE Variables?What Exactly ARE Variables?

• We all know what “variables” are. They’re those letters in math that represent numbers.

x – 3 = 2y - 4

variables

Page 3: Variables, Primitives, and Objects A Visual Learner’s Guide

Variables in JavaVariables in Java

• Well, surprise surprise! Variables in Java aren’t too different.

• When you think of Java variables, think of cups. Coffee cups, tea cups, giant souvenir cups from Carowinds, the big cups the popcorn comes in at the movies, and the cups with the metallic trim that you learned can never, ever go in the microwave.

• Variables are cups. Got it?

Page 4: Variables, Primitives, and Objects A Visual Learner’s Guide

This is a VariableThis is a Variable

Variable (cup)

you can put stuff

in it!(but what kind of

stuff?)

Page 5: Variables, Primitives, and Objects A Visual Learner’s Guide

Things Variables Must HaveThings Variables Must Have

• Variables must have:– Type– Name

• Let’s instantiate (a.k.a. make) a variable and see.

int bob;Type

Name

Page 6: Variables, Primitives, and Objects A Visual Learner’s Guide

An ExampleAn Example

• Here’s what bob the variable looks like after he’s been instantiated:

int bob;

VariableType: int

Name: bob

Page 7: Variables, Primitives, and Objects A Visual Learner’s Guide

An ExampleAn Example

• There’s nothing inside! He’s just an empty cup. Let’s put something in.

• Making sense?

bob = 4;

4Variable

Type: intName: bobContents: 4

Page 8: Variables, Primitives, and Objects A Visual Learner’s Guide

Variable NamesVariable Names

• What can you name your variables?– Anything you like, so long as you steer clear of the

reserved words… which are keywords the compiler looks for, like public static void etc.

• Why does it need a name?– You’ll want to be able to get to this variable from

wherever you are. You’ll want to have something to call it.

Page 9: Variables, Primitives, and Objects A Visual Learner’s Guide

Variable TypesVariable Types

• What are types a variable can take?– In short, Java variables come in two flavors:

primitive and reference. Let’s talk about the first.

• You’ve learned that primitives are the most basic Java data types, and that there are eight of them: boolean, char, byte, short, int, long, float, and double…

Page 10: Variables, Primitives, and Objects A Visual Learner’s Guide

Primitive VariablesPrimitive Variables

• Here’s what they look like. In cup terms.

byte short int long

Page 11: Variables, Primitives, and Objects A Visual Learner’s Guide

Primitive VariablesPrimitive Variables

• Think what would happen if you tried to pour the contents of a small cup into a large cup. You’d probably be okay, wouldn’t you? Not much chance of spillover. So that’s why this compiles:

byte b = 3;short s = b; 3

b

s

*pour*

Page 12: Variables, Primitives, and Objects A Visual Learner’s Guide

Primitive VariablesPrimitive Variables

• And we end up with:

• Yes, even though we “poured” b into s, b still retains its value. That probably has to do with Java being “pass-by-value.”

3

b s

3

Page 13: Variables, Primitives, and Objects A Visual Learner’s Guide

Primitive VariablesPrimitive Variables

• Don’t try this the other way around though! Pouring from a larger into a smaller variable, even though it may “fit,” will always get you a compile-time error.

short s = 3;byte b = s;

Spillover!

Page 14: Variables, Primitives, and Objects A Visual Learner’s Guide

Primitive VariablesPrimitive Variables

• If you really want to do that, though, you can use casting. Casting trims the excess off your values and makes them fit into variables of another data type.

• To do this, put the desired data type in parenthesis before the variable, like so:

• Keep in mind, this works for comparable data types, so don’t try to convert a boolean to an int.

short s = 3;byte b = (byte) s;

Page 15: Variables, Primitives, and Objects A Visual Learner’s Guide

Reference VariablesReference Variables

• What about the other type, reference variables, were they called?

• Reference variables refer to objects, whereas primitive variables refer to primitive values.

primitive reference

truefalse

3.1415f 10

‘c’

Page 16: Variables, Primitives, and Objects A Visual Learner’s Guide

Reference VariablesReference Variables

• So, given what we’ve learned about variables so far, this should be easy.

• Right?

Apple bill = new Apple();

VariableType: AppleName: billContents: an Apple

Type Name

Calls the constructor

Page 17: Variables, Primitives, and Objects A Visual Learner’s Guide

Reference VariablesReference Variables

• Wrong! Reference variables never, ever contain objects!

Page 18: Variables, Primitives, and Objects A Visual Learner’s Guide

ObjectsObjects

• Objects live on the heap. This is the heap:

• This is where the Java Virtual Machine puts all the objects it makes and everything it needs to run. (In literal terms, it’s a section of cordoned-off memory.)

Object I live here!

Heap

Page 19: Variables, Primitives, and Objects A Visual Learner’s Guide

ObjectsObjects

• Objects live on the heap from the moment they are created until the moment they are garbage collected.

• Garbage collection happens when an object is no longer being used, when it has no more references. Any object that doesn’t want to die a nasty death by garbage collection had better have a reference variable on the outside.

Reference

I’m toast.

I’ve got a reference;

I’ll be okay!

Page 20: Variables, Primitives, and Objects A Visual Learner’s Guide

Reference VariablesReference Variables

• Back to reference variables. Since reference variables are responsible for representing objects, but they can’t actually hold objects, they would have to hold something that could manipulate the object from a distance. Something like…

Page 21: Variables, Primitives, and Objects A Visual Learner’s Guide

Reference VariablesReference Variables

• A Remote Control! This is what a reference variable looks like

• Notice, it still has a type and a name.• Its type is the type of the object it

refers to.• It contains what amounts to a remote

control. This variable can be used to call methods and access data fields of its object

VariableType: ObjectName: tedContents: reference to an object on the heap

Page 22: Variables, Primitives, and Objects A Visual Learner’s Guide

Reference VariablesReference Variables

• So, back to our old problem. Here’s what it really looks like:

Apple bill = new Apple();

VariableType: AppleName: billContents: a

reference to an Apple

Page 23: Variables, Primitives, and Objects A Visual Learner’s Guide

Object CreationObject Creation

• Let’s look at this in closer detail. There are three steps in object creation:

1. Declaration2. Creation3. Assignment

Apple bill = new Apple();

Page 24: Variables, Primitives, and Objects A Visual Learner’s Guide

1. Declaration1. Declaration

• First, you must declare a reference variable.

Apple bill;

• You’ve effectively made a cup with a remote control, but the remote’s not programmed to anything yet.

makes

Page 25: Variables, Primitives, and Objects A Visual Learner’s Guide

2. Creation2. Creation

• Then, you create the object by calling the class’s constructor with the keyword new.

new Apple();

• Now the heap looks like so:

newly-born Apple

Page 26: Variables, Primitives, and Objects A Visual Learner’s Guide

3. Assignment3. Assignment

• This is just like programming a remote control. It links the reference variable to the object on the heap. You do this using the = operator.

Apple bill = new Apple();

Page 27: Variables, Primitives, and Objects A Visual Learner’s Guide

In ReviewIn Review

• Variables are cups.• Variables have name and type.• Variables can be either primitive or reference

type.• Reference variables are cups that contain a

remote control, not an object.• Objects live on the heap.

Page 28: Variables, Primitives, and Objects A Visual Learner’s Guide

System.exit(0);System.exit(0);

I hope this made things more clear and didn’t utterly confuse you! Best of luck in APCS!