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

Slides:



Advertisements
Similar presentations
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Advertisements

 Variables  What are they?  Declaring and initializing variables  Common uses for variables  Variables you get “for free” in Processing ▪ Aka: Built-in.
23-Apr-15 Abstract Data Types. 2 Data types I We type data--classify it into various categories-- such as int, boolean, String, Applet A data type represents.
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
Lecture 1: Comments, Variables, Assignment. Definitions The formal (human-readable) instructions that we give to the computer is called source code The.
CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
Week 2: Primitive Data Types 1.  Programming in Java  Everything goes inside a class  The main() method is the starting point for executing instructions.
10-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
Road Map Introduction to object oriented programming. Classes
Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.
Constants Variables change, constants don't final = ; final double PI = ; … area = radius * radius * PI; see Liang, p. 32 for full code.
CS102 Data Types in Java CS 102 Java’s Central Casting.
22-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
Object References. Objects An array is a collection of values, all of the same type An object is a collection of values, which may be of different types.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Adapted from Dr. Craig Chase, The University of Texas at Austin.
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
Binary “There are 10 types of people in the world… those that understand binary and those that don’t.”
While Loops and Do Loops. Suppose you wanted to repeat the same code over and over again? System.out.println(“text”); System.out.println(“text”); System.out.println(“text”);
Week 2 - Friday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
What is RobotC?!?! Team 2425 Hydra. Overview What is RobotC What is RobotC used for What you need to program a robot How a robot program works Framework.
Programming Principles Data types and Variables. Data types Variables are nothing but reserved memory locations to store values. This means that when.
Week 2 - Monday.  What did we talk about last time?  Software development  Lab 1.
VARIABLES AND TYPES CITS1001. Types in Java the eight primitive types the unlimited number of object types Values and References The Golden Rule Scope.
Java programs are built from classes. There is nothing else, but classes.
Java Data Types Data types, variable declaration, and initialization.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education.
JAVA BASICS: Variables and References SYNTAX, ERRORS, AND DEBUGGING.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 15: More-Advanced Concepts.
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
Announcements  If you need more review of Java…  I have lots of good resources – talk to me  Use “Additional Help” link on webpage  Weekly assignments.
CS107 References and Arrays By Chris Pable Spring 2009.
12-CRS-0106 REVISED 8 FEB 2013 CSG2H3 Object Oriented Programming.
1 CIS 2168 Data Structures and Algorithms in JAVA Brief Introduction to JAVA.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
C# C1 CSC 298 Elements of C# code (part 1). C# C2 Style for identifiers  Identifier: class, method, property (defined shortly) or variable names  class,
Elementary C++. Procedural Programming Split your problem into simpler parts then solve each part separately Recognize common parts and solve them only.
Java Basics Opening Discussion zWhat did we talk about last class? zWhat are the basic constructs in the programming languages you are familiar.
In Java.... Variables contain the values of primitive data types Value Types.
Constructors & Garbage Collection Ch. 9 – Head First Java.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
Data Tonga Institute of Higher Education. Variables Programs need to remember values.  Example: A program that keeps track of sales needs to remember.
Memory Management in Java Computer Science 3 Gerb Objective: Understand references to composite types in Java.
Basic Java Syntax Comments Basic data types Operators and assignment.
Week 10 - Wednesday.  What did we talk about last time?  Method example  Roulette simulation  Types in Java.
1.1: Objects and Classes msklug.weebly.com. Agenda: Attendance Let’s get started What is Java? Work Time.
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)
Primitive Data Types. int This is the type you are familiar with and have been using Stores an integer value (whole number) between -2,147,483,648 (-2.
Coding – Week 2 Functions, Arrays, and Objects. Functions  Functions are not a new concept – you’ve been using them already.  void setup() {} and void.
Lecture 11 Instructor: Craig Duckett Instance Variables.
CSE 110: Programming Language I Matin Saad Abdullah UB 1222.
April 13, 1998CS102-02Lecture 3-1 Data Types in Java CS Lecture 3-1 Java’s Central Casting.
Week 2 - Wednesday CS 121.
Values vs. References Lecture 13.
Lecture 2 D&D Chapter 2 & Intro to Eclipse IDE Date.
Intro to ETEC Java.
Introduction to Computer Science / Procedural – 67130
Strings, Line-by-line I/O, Functions, Call-by-Reference, Call-by-Value
Lesson 2: Building Blocks of Programming
Object Oriented Programming (OOP) LAB # 8
Chapter 3 Introduction to Classes, Objects Methods and Strings
Chapter 2 Edited by JJ Shepherd
Value Types and Reference Types
Sridhar Narayan Java Basics Sridhar Narayan
Introduction to Primitives
Object Oriented Programming in java
Java Programming Language
Java’s Central Casting
Presentation transcript:

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

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 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 Variable Variable (cup) you can put stuff in it! (but what kind of stuff?)

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

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

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

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.

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…

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

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*

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

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!

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;

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 f 10 ‘c’

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

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

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

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!

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…

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

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

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

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

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

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

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.

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