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

Post on 13-Jan-2016

222 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Variables, Primitives, and ObjectsVariables, 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

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?

This is a VariableThis is a Variable

Variable (cup)

you can put stuff

in it!(but what kind of

stuff?)

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

An ExampleAn Example

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

int bob;

VariableType: int

Name: bob

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

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.

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…

Primitive VariablesPrimitive Variables

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

byte short int long

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*

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

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!

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;

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’

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

Reference VariablesReference Variables

• Wrong! Reference variables never, ever contain objects!

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

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!

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…

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

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

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();

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

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

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();

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.

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

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

top related