Presentation is loading. Please wait.

Presentation is loading. Please wait.

Recitation 2 James Wei Professor Peck 1/17/2013. Covered in this Recitation Arrays Variable Scoping Local variables Instance variables Class variables.

Similar presentations


Presentation on theme: "Recitation 2 James Wei Professor Peck 1/17/2013. Covered in this Recitation Arrays Variable Scoping Local variables Instance variables Class variables."— Presentation transcript:

1 Recitation 2 James Wei Professor Peck 1/17/2013

2 Covered in this Recitation Arrays Variable Scoping Local variables Instance variables Class variables References http://goo.gl/LWL0xi L W L (zero) x i

3 Arrays public int uniqueCount(String[] list) { Arrays.sort(list); // sort array in alphabetical order String last = list[0]; int count = 1; // Invariant: count is number of unique words in list[0…k] for (int k=1; k<list.length; k++) { if (!list[k].equals(last)) { last = list[k]; count++; } Answer the “Array” questions on the form

4 Variable Scoping All variables have scope - the areas where the variable is accessible. Variables exist within their scope and are not visible outside of it. Typically can see the scope of a variable by looking at the block of code it is defined in. method, class, for loop, while loop, etc. Three types to cover: Local variables Instance variables Class variables

5 Local Variables public class Frequencies { public static int uniqueCount(String[] list) { Arrays.sort(list); String last = list[0]; for (int k=1; k<list.length; k++) { if (!list[k].equals(last)) { last = list[k]; count++; } return count; public static void main(String[] args) { int count = 1; String[] list = {"apple", "banana", "apple", "pear"}; uniqueCount(list); } Answer the “Scope” questions

6 Local Variables You can identify a variable’s scope by checking the first pair of curly braces surrounding its declaration. Usually this will give you the block of code pertaining to its scope. for-loop example for (int i=0; i<someArray.length; i++) { int j = 1; // some code } // the line below is invalid, i and j are accessed out of scope System.out.println(“i=“ + i + “, j=“ + j);

7 Instance Variables Instance variables have a scope of an entire instance of a class. Example: public class Node { private int value; public int printValue() { System.out.println(“Value is “ + value); } public void changeValue(int newValue) { value = newValue; } Notice that the variable “value” is declared outside of any methods in the Node class, but inside the class itself. This indicates that its scope is the entire class itself.

8 Class Variables Class variables appear to be almost the same as instance variables, except they are declared with a “static” modifier. Example: public class Node { private static int DEFAULT_VALUE = 0; private int value; public Node() { value = DEFAULT_VALUE; } Here “DEFAULT_VALUE” is a class variable because it has the static modifier, whereas “value” is an instance variable. Don’t worry about what static means yet, just know that a class variable is static and that an instance variable is not.

9 References Java accesses objects by reference ArrayList list = new ArrayList<>(); creates: To access variable, look up list variable points to ArrayList object in memory. “list” is a reference to the ArrayList object. ArrayList otherList = list; gives the following: We have TWO references, “list” and “otherList”, both reference the same object in memory! Changing “list” will also change “otherList”, and vica versa. list Empty ArrayList Memory (Heap) otherList

10 References public class Rec2Example { private int myA; private int myB; public Rec2Example(int a, int b) { myA = a; myB = b; } public void setA(int a) { myA = a; } public static void main(String[] args) { Rec2Example ex = new Rec2Example(33, 7); Rec2Example ex2 = ex; ex.setA(44); }

11 References public static void randomFunction() { ArrayList list = new ArrayList<>(); ArrayList otherList = list; list.add(44); otherList.add(55); }

12 References Just remember, Java remembers which objects are in memory by keeping references that point to those objects, and these references are assigned whenever you set something “=“ to something else. You will learn more about how this works under the covers in CS250 if you take it…

13 Have a good weekend! Make sure to submit your answers before next Friday to get credit for today…


Download ppt "Recitation 2 James Wei Professor Peck 1/17/2013. Covered in this Recitation Arrays Variable Scoping Local variables Instance variables Class variables."

Similar presentations


Ads by Google