Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Variables, Primitives, and Objects A Visual Learner’s Guide."— Presentation transcript:

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

2 What Exactly ARE Variables? We all know what “variables” are. They’re those letters in math that represent numbers. x – 3 = 2y - 4 variables

3 Variables 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?

4 This is a Variable Variable (cup) you can put stuff in it! (but what kind of stuff?)

5 Things Variables Must Have Variables must have: – Type – Name Let’s instantiate (a.k.a. make) a variable and see. int bob; Type Name

6 An Example Here’s what bob the variable looks like after he’s been instantiated: int bob; Variable Type: int Name: bob

7 An Example There’s nothing inside! He’s just an empty cup. Let’s put something in. Making sense? bob = 4; 4 Variable Type: int Name: bob Contents: 4

8 Variable 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.

9 Variable 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…

10 Primitive Variables Here’s what they look like. In cup terms. byte short int long

11 Primitive 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*

12 Primitive 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

13 Primitive 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!

14 Primitive 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;

15 Reference 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 true false 3.1415f 10 ‘c’

16 Reference Variables So, given what we’ve learned about variables so far, this should be easy. Right? Apple bill = new Apple(); Variable Type: Apple Name: bill Contents: an Apple Type Name Calls the constructor

17 Reference Variables Wrong! Reference variables never, ever contain objects!

18 Objects 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

19 Objects 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!

20 Reference 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…

21 Reference 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 Variable Type: Object Name: ted Contents: reference to an object on the heap

22 Reference Variables So, back to our old problem. Here’s what it really looks like: Apple bill = new Apple(); Variable Type: Apple Name: bill Contents: a reference to an Apple

23 Object Creation Let’s look at this in closer detail. There are three steps in object creation: 1.Declaration 2.Creation 3.Assignment Apple bill = new Apple();

24 1. 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

25 2. 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

26 3. 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();

27 In 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.

28 System.exit(0); I hope this made things more clear and didn’t utterly confuse you! Best of luck in APCS!


Download ppt "Variables, Primitives, and Objects A Visual Learner’s Guide."

Similar presentations


Ads by Google