Coming up Variables Quick look at scope Primitives Objects

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

JAVA Revision Lecture Electronic Voting System Marina De Vos.
IT 325 OPERATING SYSTEM C programming language. Why use C instead of Java Intermediate-level language:  Low-level features like bit operations  High-level.
08/2012Tanya Mishra1 EASYC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817.
Animation Mrs. C. Furman. Animation  We can animate our crab by switching the image between two pictures.  crab.png and crab2.png.
Week 2: Primitive Data Types 1.  Programming in Java  Everything goes inside a class  The main() method is the starting point for executing instructions.
1 Java Basics. 2 Compiling A “compiler” is a program that translates from one language to another Typically from easy-to-read to fast-to-run e.g. from.
Looking inside classes Choices Week 4. Class bodies contain fields, constructors and methods. Fields store values that determine an object’s state. Constructors.
1 Chapter 7 User-Defined Methods Java Programming from Thomson Course Tech, adopted by kcluk.
Objects Interaction and Source Code Week 2. OBJECT ORIENTATION BASICS REVIEW.
Introduction to Computers and Programming Strings Professor: Evan Korth New York University.
Understanding class definitions – Part II –. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 7 User-Defined Methods.
Chapter 7: User-Defined Methods
1 Reflecting on the ticket machines Their behavior is inadequate in several ways: –No checks on the amounts entered. –No refunds. –No checks for a sensible.
Understanding class definitions Looking inside classes.
A virtuous cycle for Programming Principles Su White and Teresa Binks, University of Southampton.
VARIABLES AND TYPES CITS1001. Types in Java the eight primitive types the unlimited number of object types Values and References The Golden Rule Scope.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
CPS120: Introduction to Computer Science Variables and Constants Lecture 8 - B.
Java means Coffee Java Coffee Beans The name “JAVA” was taken from a cup of coffee.
Classes CS 21a: Introduction to Computing I First Semester,
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William.
Understanding class definitions
Java Programming Java Basics. Data Types Java has two main categories of data types: –Primitive data types Built in data types Many very similar to C++
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Copyright Curt Hill Variables What are they? Why do we need them?
C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope.
ITEC 109 Lecture 7 Operations. Review Variables / Methods Functions Assignments –Purpose? –What provides the data? –What stores the data? –What type of.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Memory Management in Java Computer Science 3 Gerb Objective: Understand references to composite types in Java.
Looking inside classes Conditional Statements Week 4.
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
Object Oriented Programming and Data Abstraction Rowan University Earl Huff.
CSC 142 F 1 CSC 142 References and Primitives. CSC 142 F 2 Review: references and primitives  Reference: the name of an object. The type of the object.
Comp1004: Programming in Java I Variables - Primitives, Objects and Scope.
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
Understanding class definitions Exploring source code 6.0.
Coming up Implementation vs. Interface The Truth about variables Comparing strings HashMaps.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Content Programming Overview The JVM A brief look at Structure – Class – Method – Statement Magic incantations – main() – output Coding a Dog Programming.
Topics introduced today (these topics would be covered in more detail in later classes) – Primitive Data types Variables Methods “for” loop “if-else” statement.
Computer Organization and Design Pointers, Arrays and Strings in C
Objects as a programming concept
Chapter 7 User-Defined Methods.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 7 User-Defined Methods.
Content Programming Overview The JVM A brief look at Structure
Engineering Innovation Center
Understanding class definitions
Understanding class definitions
Principles of Computer Programming (using Java) Chapter 2, Part 1
An Introduction to Java – Part I, language basics
Variables Numbers can be stored and retrieved while a program is running if they are given a home. The way that integers and decimal numbers are stored.
Variables Title slide variables.
AKA the birth, life, and death of variables.
Understanding class definitions
F II 3. Classes and Objects Objectives
COS 260 DAY 4 Tony Gauvin.
Chapter 2 Programming Basics.
CS150 Introduction to Computer Science 1
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
References Revisted (Ch 5)
ITM 352 Functions.
Variables and Constants
Presentation transcript:

Coming up Variables Quick look at scope Primitives Objects defined in Java defined in classes stored in variables directly references stored in variables Pass a copy Pass a reference

Chucking variables around OO Lecture 3 Chucking variables around

So far we know that variables store ‘things’ like ints, booleans and elephants

What is a variable? it’s also known as a field it can store a primitive type it can store an object reference

What is a primitive? OO Not one of these or these Primitive types are defined in Java Primitive types are stored in variables directly Primitive types are “passed by value” eg: int, char, boolean

What is an object type? OO Object types are those defined in classes Variables store references to objects Passing a reference is not passing a copy eg Dog, Array, Connection Are you a little confused yet?

Primitives Objects The Big Picture defined in Java defined in classes stored in variables directly references stored in variables Pass a copy Pass a reference

Defined in the Language Data types like: int whole numbers float floating point numbers (i.e. decimals) char a single character boolean true or false and some others are taken care of by the Java language. We just use them

Primitives Objects The Big Picture defined in Java defined in classes stored in variables directly references stored in variables Pass a copy Pass a reference

Defined in Classes OO 1 is a primitive of type int nellie is an object of type Elephant Objects can be of a type defined by: a class you write a class someone else writes Sun have a library of useful classes, ones that can read files on your computer or output sounds and many other tasks. We use these a lot later on.

Primitives Objects The Big Picture defined in Java defined in classes stored in variables directly references stored in variables Pass a copy Pass a reference

Cups A variable is a space in memory that the computer uses to store a value It’s like a cup int myNumber; myNumber = 7; A primitive ‘fits into’ a cup myNumber 7 int

Primitives Objects The Big Picture defined in Java defined in classes stored in variables directly references stored in variables Pass a copy Pass a reference

Objects and cups OO An object does not fit into a cup... Elephant nellie; nellie = new Elephant(); So what does Java do? nellie Elephant?!?!

Remote controls Java leaves the elephant object in memory somewhere... And references it – like using a remote control

Memory Elephant nellie; nellie = new Elephant(); nellie.eat(); Eat Sleep Trumpet Memory nellie Elephant Nom Nom Nom

Primitives Objects The Big Picture defined in Java defined in classes stored in variables directly references stored in variables Pass a copy Pass a reference

Pass a copy a int int a; a = 10; int b; b = 5; int c; c = a; c = 2*c; b = a*c; 10 b int 200 5 c int 20 10

Primitives Objects The Big Picture defined in Java defined in classes stored in variables directly references stored in variables Pass a copy Pass a reference

OO Object assignment Memory Student a; a = new Student(); Student b; b = new Student(); Student c; c=a; a=b; c=b; c=null; a b c

A word about Local variables Fields are one sort of variable. They store values through the life of an object. They are accessible throughout the class. Methods can include shorter-lived variables. They exist only as long as the method is being executed. They are only accessible from within the method. This is called scope. It is covered in detail later in the course.

The rule of thumb... ...is that a variable can be seen anywhere within the {} that it was declared in

What happens when this is run? public class canYouSeeMe{ int number = 5; public void doodah(){ String word = “hello” } public void dahdoo(){ number =10; public void printer(){ doodah(); dahdoo(); System.out.print(word + “ “ + number); What happens when this is run?

Local variable example from BlueJ Book A local variable public int refundBalance() { int amountToRefund; amountToRefund = balance; balance = 0; return amountToRefund; } No visibility modifier

Primitives Objects Summary defined in Java defined in classes stored in variables directly references stored in variables Pass a copy Pass a reference a variable can be seen anywhere within the {} that it was declared in