georgia institute of technology declaring variables – mod 4 barb ericson georgia institute of...
TRANSCRIPT
![Page 1: Georgia Institute of Technology Declaring Variables – Mod 4 Barb Ericson Georgia Institute of Technology August 2005](https://reader035.vdocuments.mx/reader035/viewer/2022072005/56649f425503460f94c615c0/html5/thumbnails/1.jpg)
Georgia Institute of Technology
Declaring Variables – Mod 4
Barb EricsonGeorgia Institute of Technology
August 2005
![Page 2: Georgia Institute of Technology Declaring Variables – Mod 4 Barb Ericson Georgia Institute of Technology August 2005](https://reader035.vdocuments.mx/reader035/viewer/2022072005/56649f425503460f94c615c0/html5/thumbnails/2.jpg)
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
![Page 3: Georgia Institute of Technology Declaring Variables – Mod 4 Barb Ericson Georgia Institute of Technology August 2005](https://reader035.vdocuments.mx/reader035/viewer/2022072005/56649f425503460f94c615c0/html5/thumbnails/3.jpg)
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
![Page 4: Georgia Institute of Technology Declaring Variables – Mod 4 Barb Ericson Georgia Institute of Technology August 2005](https://reader035.vdocuments.mx/reader035/viewer/2022072005/56649f425503460f94c615c0/html5/thumbnails/4.jpg)
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;
![Page 5: Georgia Institute of Technology Declaring Variables – Mod 4 Barb Ericson Georgia Institute of Technology August 2005](https://reader035.vdocuments.mx/reader035/viewer/2022072005/56649f425503460f94c615c0/html5/thumbnails/5.jpg)
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”;
3a
str
“Hi”
reference
![Page 6: Georgia Institute of Technology Declaring Variables – Mod 4 Barb Ericson Georgia Institute of Technology August 2005](https://reader035.vdocuments.mx/reader035/viewer/2022072005/56649f425503460f94c615c0/html5/thumbnails/6.jpg)
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;
![Page 7: Georgia Institute of Technology Declaring Variables – Mod 4 Barb Ericson Georgia Institute of Technology August 2005](https://reader035.vdocuments.mx/reader035/viewer/2022072005/56649f425503460f94c615c0/html5/thumbnails/7.jpg)
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
![Page 8: Georgia Institute of Technology Declaring Variables – Mod 4 Barb Ericson Georgia Institute of Technology August 2005](https://reader035.vdocuments.mx/reader035/viewer/2022072005/56649f425503460f94c615c0/html5/thumbnails/8.jpg)
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
![Page 9: Georgia Institute of Technology Declaring Variables – Mod 4 Barb Ericson Georgia Institute of Technology August 2005](https://reader035.vdocuments.mx/reader035/viewer/2022072005/56649f425503460f94c615c0/html5/thumbnails/9.jpg)
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?
![Page 10: Georgia Institute of Technology Declaring Variables – Mod 4 Barb Ericson Georgia Institute of Technology August 2005](https://reader035.vdocuments.mx/reader035/viewer/2022072005/56649f425503460f94c615c0/html5/thumbnails/10.jpg)
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?
![Page 11: Georgia Institute of Technology Declaring Variables – Mod 4 Barb Ericson Georgia Institute of Technology August 2005](https://reader035.vdocuments.mx/reader035/viewer/2022072005/56649f425503460f94c615c0/html5/thumbnails/11.jpg)
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;
![Page 12: Georgia Institute of Technology Declaring Variables – Mod 4 Barb Ericson Georgia Institute of Technology August 2005](https://reader035.vdocuments.mx/reader035/viewer/2022072005/56649f425503460f94c615c0/html5/thumbnails/12.jpg)
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
![Page 13: Georgia Institute of Technology Declaring Variables – Mod 4 Barb Ericson Georgia Institute of Technology August 2005](https://reader035.vdocuments.mx/reader035/viewer/2022072005/56649f425503460f94c615c0/html5/thumbnails/13.jpg)
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?
![Page 14: Georgia Institute of Technology Declaring Variables – Mod 4 Barb Ericson Georgia Institute of Technology August 2005](https://reader035.vdocuments.mx/reader035/viewer/2022072005/56649f425503460f94c615c0/html5/thumbnails/14.jpg)
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();
![Page 15: Georgia Institute of Technology Declaring Variables – Mod 4 Barb Ericson Georgia Institute of Technology August 2005](https://reader035.vdocuments.mx/reader035/viewer/2022072005/56649f425503460f94c615c0/html5/thumbnails/15.jpg)
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!
![Page 16: Georgia Institute of Technology Declaring Variables – Mod 4 Barb Ericson Georgia Institute of Technology August 2005](https://reader035.vdocuments.mx/reader035/viewer/2022072005/56649f425503460f94c615c0/html5/thumbnails/16.jpg)
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
• Primitive variables reserve space based on the type– Object variables reserve space for a
reference to an object
• An object can be garbage collected if there is no reference to it