Georgia Institute of Technology Declaring Variables – Mod 4 Barb Ericson Georgia Institute of Technology August 2005.

Slides:



Advertisements
Similar presentations
ManipulatingPictures-Mod6-part21 Manipulating Pictures, Arrays, and Loops part 2 Barb Ericson Georgia Institute of Technology.
Advertisements

10-Jun-15 Just Enough Java. Variables A variable is a “box” that holds data Every variable has a name Examples: name, age, address, isMarried Variables.
Georgia Institute of Technology Introduction to Java part 2 Barb Ericson Georgia Institute of Technology May 2006.
04-Intro-Object-Oriented-In-Java1 Barb Ericson Georgia Institute of Technology Sept 2009 Introduction to Object-Oriented Programming in Java.
01- Intro-Java-part1 1 Introduction to Java, and DrJava Barb Ericson Georgia Institute of Technology June 2008.
Georgia Institute of Technology Introduction to Java, and DrJava Barb Ericson Georgia Institute of Technology Aug 2005.
Georgia Institute of Technology Barb Ericson Georgia Institute of Technology May 2006 Teaching Java using Turtles part 1.
CPSC1301 Computer Science 1 Chapter 11 Creating Classes part 1.
CreatingClasses-part11 Creating Classes part 1 Barb Ericson Georgia Institute of Technology Dec 2009.
Methods in Java CSC1401. Overview In this session, we are going to see how to create class-level methods in Java.
Georgia Institute of Technology Creating Classes part 1 Barb Ericson Georgia Institute of Technology Oct 2005.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
Georgia Institute of Technology Barb Ericson Georgia Institute of Technology May 2006 Teaching Java using Turtles part 2.
Chapter 2. C++ Program Structure C++ program is a collection of subprograms Subprograms in C++ are called FUNCTIONS Each function performs a specific.
Arithmetic Operations. Review function statement input/output comment #include data type variable identifier constant declaration.
CSC1401 Classes - 1. Learning Goals Computing concepts Identifying objects and classes Declaring a class Declaring fields Default field values.
Georgia Institute of Technology Introduction to Java, and DrJava Barb Ericson Georgia Institute of Technology June 2005.
Java Introduction part 2 CSC1401. Overview In this session, we are going to examine some of the instructions from the previous class in more detail.
Georgia Institute of Technology Manipulating Pictures, Arrays, and Loops Barb Ericson Georgia Institute of Technology August 2005.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Georgia Institute of Technology Declaring Variables – Mod 4 Barb Ericson Georgia Institute of Technology August 2005.
Data Tonga Institute of Higher Education. Variables Programs need to remember values.  Example: A program that keeps track of sales needs to remember.
Variables and Functions Alice. Naming is Important If you get a new pet one of the first things you do is name it Gives you a way to refer to the new.
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
Georgia Institute of Technology Creating Classes part 2 Barb Ericson Georgia Institute of Technology June 2006.
Memory Management in Java Computer Science 3 Gerb Objective: Understand references to composite types in Java.
Georgia Institute of Technology Barb Ericson Georgia Institute of Technology Aug 2006 Introduction to Object-Oriented Programming in Alice and Java.
1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Georgia Institute of Technology Workshop for Programming And Systems Management Teachers Chapter 3 Introduction to Media Computation, Java, and DrJava.
04-ManipulatingPictures-part21 Manipulating Pictures, Arrays, and Loops part 2 Barb Ericson Georgia Institute of Technology June 2008.
Georgia Institute of Technology Barb Ericson Georgia Institute of Technology Sept 2006 Introduction to Object-Oriented Programming in Alice and Java.
Georgia Institute of Technology Introduction to Java, and DrJava part 1 Dr Usman Saeed Assistant Professor Faculty of Computing and Information Technology.
Topics for today: 1.Comments 2.Data types 3.Variable declaration.
Georgia Institute of Technology Dr Usman Saeed Assistant Professor Faculty of Computing and Information Technology North Jeddah Branch King Abdulaziz University.
Georgia Institute of Technology More on Creating Classes Barb Ericson Georgia Institute of Technology June 2006.
Chapter 3 Using Variables, Constants, Formatting Mrs. UlshaferSept
Manipulating Pictures, Arrays, and Loops part 2
Introduction to Java part 2
BASIC ELEMENTS OF A COMPUTER PROGRAM
Declaring Variables – Mod 4
Introduction to Media Computation
Data types and variables
Barb Ericson Georgia Institute of Technology Dec 2009
Introduction to Object-Oriented Programming in Alice and Java
Introduction to Object-Oriented Programming in Java
Building Java Programs Chapter 2
Barb Ericson Georgia Institute of Technology August 2005
Introduction to Java part 2
Introduction to Java, and DrJava part 1
Declaring Variables – Mod 4
Unit-1 Introduction to Java
Teaching Java using Turtles part 2
Teaching Java using Turtles part 3
Building Java Programs
Declaring Variables – Mod 4
Barb Ericson Georgia Institute of Technology Oct 2005
Building Java Programs Chapter 2
Manipulating Pictures, Arrays, and Loops
Introduction to Java, and DrJava
Introduction to Java part 2
Tutorial 10: Programming with javascript
More on Creating Classes
Manipulating Pictures, Arrays, and Loops
Building Java Programs Chapter 2
Introduction to Java, and DrJava
IAT 800 Foundations of Computational Art and Design
Introduction to Java, and DrJava part 1
Barb Ericson Georgia Institute of Technology Oct 2005
Just Enough Java 17-May-19.
Teaching Java using Turtles part 2
Presentation transcript:

Georgia Institute of Technology Declaring Variables – Mod 4 Barb Ericson Georgia Institute of Technology August 2005

Georgia Institute of Technology Learning Goals Understand at a conceptual level –Declaring variables: what is allowed and what isn’t –When variables can be garbage collected –How to change the value of a variable –The difference between primitive and object variables

Georgia Institute of Technology Working with Variables You must declare a variable before you can use it Declare a variable once and only once –You will get an error if you declare it more than once You can reuse variables –That you have declared

Georgia Institute of Technology Declaring Variables To declare a variable –Specify the type and a name String name; World world1; Turtle turtle1; int x; –You can also assign a value to the variable when you declare it String fullName = “Susan Dorda”; World world1 = new World(); Turtle turtle1 = new Turtle(world1); int x = 0;

Georgia Institute of Technology Variable Name Rules Should start with a lowercase letter –Not a number World 1world; // results in a syntax error Uppercase the first letter of each word –To make it easy to read World myWorld; // works fine No spaces allowed in the name World my world; // results in a syntax error Can’t use any of the reserved words Class class; // results in a syntax error

Georgia Institute of Technology Object and Primitive Variables Primitive variables allocate space based on their size –The contents of the space is set to the variable’s value Object type variables cause space allocation for a reference to an object –The contents of the space is a way to calculate the address of the object –Or null if it isn’t referring to an object yet. int a = 3 String str =“Hi”; 3 a str “Hi” reference

Georgia Institute of Technology Memory Exercise Draw the memory used for the following: int x = 2; int y = 7; int z = x + y; Draw the memory used for the following: String fullName = null; String firstName = “James”; String lastName = “Clark”; fullName = firstName + lastName;

Georgia Institute of Technology Variables are Temporary Hold values until –Reassigned int x = 2; x = 3; // read ‘=‘ as takes on the value of –DrJava is exited –The interactions pane is reset You can force this by clicking the “Reset” button Or by right click and choose “Reset Interactions” Happens automatically when you compile

Georgia Institute of Technology Objects can be Garbage Collected When objects that have been created have no reference to them –They can be garbage collected This means the memory they are using can be freed and reused –String name1 = “Bill”; –String name1 = new String(“Mary”); –The string object created by “Bill” can be garbage collected

Georgia Institute of Technology Variable Practice Given the following code –What is the value of z at each step? int z = 0; int x = 3; int y = 2; z = x * y; z = z + x; z = z – y; –How many variables where declared? –How much space do they take?

Georgia Institute of Technology Variable Practice What is the value of str1 at each step? –String str1 = “Hello World”; –str1.toLowerCase(); –str1 = str1.toUpperCase(); –str1 = “Goodbye World”; How many variables where declared? How many objects were created? –How many can be garbage collected?

Georgia Institute of Technology Variable Practice What is the value of string1 after the following code has executed? –String string1 = “HELLO”; –System.out.println(string1.toLowerCase()); What type of variable is string1 (primitive or object)? –How can you tell? What is the value of string2 and string3 after the following code has executed? –String string2 = “Mary”; –String string3 = “Clark”; –string2 = string2 + string3;

Georgia Institute of Technology Variable Practice Declare variables for each of the following –The number of kids in a family –The cost of a pair of shoes –The name of a pet –The score of a soccer game –The hourly wage of a worker –The answer to do you want insurance on a purchase –The color of your eyes –Your weight –The distance you drive on a trip –Your test score –The number on a lottery ball

Georgia Institute of Technology Variable Practice Which turtle does turtle1 refer to after the following code? World myWorld = new World(); Turtle tommy = new Turtle(myWorld); Turtle mary = new Turtle(myWorld); Turtle turtle1 = tommy; turtle1.forward(); turtle1 = mary; turtle1.turnLeft(); Which turtle will go forward? Which turtle will turn left?

Georgia Institute of Technology Variable Practice Which of these statements will cause an error in the interactions pane and why? World1 world = new World(); World world2 = new World(); World world3 = newWorld(); World world1 = new World1(); World world5 = new World; World world2 = new World(); World my World = new World(); World earth = new World();

Georgia Institute of Technology Allowed Types in Java The type for a variable declaration can be –A primitive type: int, double, char, or boolean –The name of a Class in the Java language –String, JFrame, BufferedImage, etc or the name of a class created by you or someone else –Picture, Sound, FileChooser Class names start with an uppercase letter!

Georgia Institute of Technology Summary You can declare variables by specifying a type and a name –You can also assign a value to the variable when you declare it Variable names –Variables start with a lowercase letter (not a number) –A variable name can’t be any of the reserved words –Variable names can’t have spaces in them Primitive variables reserve space based on the type –And store the value in the space –Object variables reserve space for a reference to an object A way to find the space the object is in An object can be garbage collected if there is no reference to it