Today Quiz solutions are posted on the Grading page. Assignment 1 due today, 7pm. Arrays as Pointers, Cont. Aliasing & Passing by Reference null Winter.

Slides:



Advertisements
Similar presentations
Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
Advertisements

CSCI 1100/ , 6.2, 6.4 April 12, 15, 17.
CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
Today Quiz solutions are posted on the Grading page. Assignment 2 is posted. Due the first Friday after Reading Week. All about null Start File Input/Output.
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
Strings and Arrays The objectives of this chapter are:  To discuss the String class and some of its methods  To discuss the creation and use of Arrays.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
ECE122 L13: Arrays of Objects March 15, 2007 ECE 122 Engineering Problem Solving with Java Lecture 13 Arrays of Objects.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Chapter 9 Introduction to Arrays
Java Unit 9: Arrays Declaring and Processing Arrays.
Presented by: Mojtaba Khezrian. Agenda Object Creation Object Storage More on Arrays Parameter Passing For Each VarArgs Spring 2014Sharif University of.
Comp 248 Introduction to Programming Chapter 6 Arrays Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
Copyright © 2002, Systems and Computer Engineering, Carleton University a-JavaReview.ppt * Object-Oriented Software Development Unit.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
ARRAYS Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall
2D-Arrays Quratulain. Learning Objectives Two-dimensional arrays Declaration Initialization Applications.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
12-CRS-0106 REVISED 8 FEB 2013 CSG2H3 Object Oriented Programming.
Peyman Dodangeh Sharif University of Technology Fall 2013.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
Fall 2015CISC124 - Prof. McLeod1 CISC124 Have you filled out the lab section survey? (As of last night 54 left to fill out the survey.) TA names have been.
August 6, 2009 Data Types, Variables, and Arrays.
CMSC 202 Arrays 2 nd Lecture. Aug 6, Array Parameters Both array indexed variables and entire arrays can be used as arguments to methods –An indexed.
Memory Management in Java Computer Science 3 Gerb Objective: Understand references to composite types in Java.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
Today… “Hello World” ritual. Brief History of Java & How Java Works. Introduction to Java class structure. But first, next slide shows Java is No. 1 programming.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 1 due Friday, 7pm. RAD due next Friday. Presentations week 6. Today: –More details on functions,
Peyman Dodangeh Sharif University of Technology Spring 2014.
Aside: Running Supplied *.java Programs Just double clicking on a *.java file may not be too useful! 1.In Eclipse, create a project for this program or.
Chapter 5: Arrays in Java. The objectives of this chapter are:  1. To discuss the creation and use of Arrays.   2. To continue to use the String class.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Reminders: Have you filled out the questionnaire in Moodle? (3 left – as of last night). Office hours.
ARRAYS Multidimensional realities Image courtesy of
Chapter 9 Introduction to Arrays Fundamentals of Java.
Today… Preparation for doing Assignment 1. Invoking methods overview. Conditionals and Loops. Winter 2016CMPE212 - Prof. McLeod1.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 next week. See next slide. Both versions of assignment 3 are posted. Due today.
Winter 2006CISC121 - Prof. McLeod1 Last Time Reviewed class structure: –attributes –methods –(inner classes) Looked at the effects of the modifiers: –public.
Today Review passing by reference and pointers. null pointers. What is an Object? Winter 2016CMPE212 - Prof. McLeod1.
Today Encapsulation. Build a fully encapsulated Halloween class, going from Halloween1 to Halloween6 (eventually!): –The final version will have overloaded.
Objects and Memory Mehdi Einali Advanced Programming in Java 1.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
Summer 2007CISC121 - Prof. McLeod1 CISC121 – Lecture 3 Last time: –Lots of fundamental Java: Console I/O Conditionals Loops For reading: –File I/O –Exceptions.
Today… Continue Building Expressions: –Literals –Constants –Casting –Array Declaration –Operators and Operator Precedence –Keywords Winter 2016CMPE212.
Recap Resizing the Vector Push_back function Parameters passing Mechanism Primitive Arrays of Constants Multidimensional Arrays The Standard Library string.
Today… StringTokenizer class. Method Overloading. Catching Exceptions (and what they are!). Start Pointers and Aliasing. Winter 2016CMPE212 - Prof. McLeod1.
Last Time Math class Style and Documentation Loops And Exercises!
CISC/CMPE320 - Prof. McLeod
Pointers Revisited What is variable address, name, value?
Java Review: Reference Types
Winter 2018 CMPE212 9/18/2018 CMPE212 – Stuff…
Fall 2017 CISC124 9/18/2018 CISC124 First onQ quiz this week – write in lab. More details in last Wednesday’s lecture. Repeated: The quiz availability.
Fall 2017 CISC124 9/21/2018 CISC124 First onQ quiz this week – write in lab. More details in last Wednesday’s lecture. Repeated: The quiz availability.
Advanced Programming Behnam Hatami Fall 2017.
CMPE212 – Stuff… Exercises 4, 5 and 6 are all fair game now.
CISC101 Reminders Slides have changed from those posted last night…
CISC101 Reminders Assn 3 due tomorrow, 7pm.
Fall 2018 CISC124 2/22/2019 CISC124 Quiz 1 This Week. Topics and format of quiz in last Tuesday’s notes. The prof. (me!) will start grading the quiz.
Fall 2018 CISC124 2/24/2019 CISC124 Quiz 1 marking is complete. Quiz average was about 40/60 or 67%. TAs are still grading assn 1. Assn 2 due this Friday,
Winter 2019 CMPE212 4/7/2019 CMPE212 – Reminders
Winter 2019 CMPE212 4/17/2019 CMPE212 – Reminders
CMPE212 – Reminders Course Web Site:
Last Time Some discussion of program “efficiency”. Arrays – 1D and 2D
CMPE212 – Reminders Quiz 1 marking done. Assignment 2 due next Friday.
Winter 2019 CMPE212 5/10/2019 CMPE212 – Reminders
CISC101 Reminders Assignment 3 due today.
CMPE212 – Reminders Assignment 2 due next Friday.
Presentation transcript:

Today Quiz solutions are posted on the Grading page. Assignment 1 due today, 7pm. Arrays as Pointers, Cont. Aliasing & Passing by Reference null Winter 2015CMPE212 - Prof. McLeod1

Winter 2015CMPE212 - Prof. McLeod One-Dimensional Arrays - Declaration, Cont. 0180ff … int[] testArray 0480ff 10.length As a “pointer”, testArray points to an area of memory that contains a series of int values as well as the attribute length.

Winter 2015CMPE212 - Prof. McLeod3 Multi-Dimensional Arrays Consider: int[][] exArray = new int[3][5]; int[][] exArray 0480ff int[] exArray[0] 1002fc int[] exArray[1] 1010fc int[] exArray[2] 1201ab fc 1201ab 1010fc

Winter 2015CMPE212 - Prof. McLeod4 Multi-Dimensional Arrays - Cont. So exArray points to three one dimensional arrays: exArray[0] exArray[1] exArray[2] Each of these arrays has the same length: exArray[2].length // returns 5 Yes, you can refer to these arrays in code, just like this.

Winter 2015CMPE212 - Prof. McLeod5 Multi-Dimensional Arrays - Cont. int[][] twoDArray = new int[10][20]; The above is equivalent to: int[][] twoDArray; twoDArray = new int[10][]; for (int i = 0; i < 10; i++) twoDArray[i] = new int[20]; As shown above: twoDArray.length// gives 10 twoDArray[0].length // gives 20

Winter 2015CMPE212 - Prof. McLeod6 Multi-Dimensional Arrays - Cont. “Ragged Arrays” are not “square”, and are legal in Java: int[][] raggedArray = new int[5][]; raggedArray[0] = new int[5]; raggedArray[1] = new int[3]; raggedArray[2] = new int[2]; raggedArray[3] = new int[8]; raggedArray[4] = new int[12];

Winter 2015CMPE212 - Prof. McLeod7 Multi-Dimensional Arrays - Cont. Array initializer for two-D array, for example: int[][] twoDArray = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12}}; System.out.println(twoDArray.length); // 4 System.out.println(twoDArray[0].length); // 3

Winter 2015CMPE212 - Prof. McLeod8 Aliasing Objects - Array Example int[] first 0480ff.length int[] second 0960ff.length 7 int[] first = {1, 2, 3, 4, 5}; int[] second = {10, 20, 30, 40, 50, 60, 70};

Winter 2015CMPE212 - Prof. McLeod9 Aliasing Objects - Array Example, Cont int[] first 0480ff.length int[] second 0480ff 0960ff.length 7 second = first; // Aliasing!

Winter 2015CMPE212 - Prof. McLeod10 Aliasing Objects - Array Example, Cont int[] first 0480ff.length 5 int[] second 0480ff // after garbage collection

Winter 2015CMPE212 - Prof. McLeod11 Aside – “Garbage Collection” in Java Some computer programming languages require you to indicate when you are done with variables so the memory they are occupying can be released back to the OS. Called “Garbage Collection”. (Fortunately!) Java has an automatic Garbage Collection system: –Variables are garbage collected once you move outside their scope. –Object contents are garbage collected when there are no pointers pointing to the contents.

Winter 2015CMPE212 - Prof. McLeod12 Aliasing Objects - Array Example, Cont int[] first 0480ff.length 5 int[] second 0480ff first[4] = 500; // second[4] is also 500

Winter 2015CMPE212 - Prof. McLeod13 Aliasing Objects - Array Example, Cont. So, setting one array to equal another as in: array1 = array2; sets array1 to point to the same data memory location that was (and still is) pointed to by array2. Now, changing the value of an element in array2 will change that same element in array1, or visa- versa - this makes sense since both array Objects point to the same set of data values in memory!

Aliasing Objects Passing an Object into a method results in the method’s parameter being aliased to the Object passed. Called “Passing by Reference”! Winter 2015CMPE212 - Prof. McLeod14

Passing Parameters by Reference For example, in main : int[] arrayA = {1, 2, 3, 4, 5}; passArray(arrayA);// invoke passArray The passArray method: public static void passArray(int[] arrayB) { // arrayB is aliased to arrayA from main // making elemental changes to arrayB will // also change elements in arrayA in main arrayB[3] = 400; } // end passArray // arrayA[3] is 400 in main Winter 2015CMPE212 - Prof. McLeod15

Passing Parameters by Reference, Cont. The rule for parameter passing into methods is: –Objects are passed by reference, primitive types are passed by value. See PassingDemo.java –Has a method with two parameters - an array and an int - which one(s) will stay changed? –Instead of going element by element, if you re-assign the array to another array within the method, what happens? –Does this rule apply to Strings, as well? Winter 2015CMPE212 - Prof. McLeod16

Passing Arrays by Reference Summary of PassingDemo.java: –Primitive types are passed by value. –Only element by element changes in arrays will “stick”. –Re-assigning the array to a pointer that has local scope in a method will not “stick”. –If you make element by element changes using an aliased local pointer (like the parameter), changes will “stick”. –Strings are immutable, so this does not apply. You cannot make elemental changes inside a String, even though a String is passed by reference. Winter 2015CMPE212 - Prof. McLeod17

Passing Arrays by Reference, Cont. So, mutable Objects (like arrays) can be passed into and out of a method through the parameter list. If a method changes the contents of a mutable Object passed into it – those changes “stick” even when the method is complete. Winter 2015CMPE212 - Prof. McLeod18

Aside - Comparing Objects Testing arrays and Objects for equality (with the “==“ boolean operator) is also interesting: –This test will only give a true when both objects have been aliased, using the assignment operator “=“. –So, even if both arrays have identical contents, “==“ will return false, unless both arrays point to the same location. –This means that comparing Objects with “==“ only compares pointers, not contents. Winter 2015CMPE212 - Prof. McLeod19

Pointers – A Question So, which way is better to declare a 3 by two-dimensional array?: int[][] wayOne = new int[3][10000]; int[][] wayTwo = new int[10000][3]; Or, it makes no difference? Winter 2015CMPE212 - Prof. McLeod20

How Much Memory Does a Pointer Use? String aString = “Hello!”; How much memory does aString consume? Not the string itself, just the pointer to the string. 32 bits for 32 bit Java, 64 bits for 64 bit Java (unless you are using compressed pointers…) Winter 2015CMPE212 - Prof. McLeod21

null Pointer or null Reference null is not a keyword in Java – more like a literal constant. What is a null pointer? What is a null pointer error? Does null have a type? Can you test a pointer to see if it is null ? How? Why would you want to? Winter 2015CMPE212 - Prof. McLeod22

null Pointer or null Reference, Cont. What is the difference between string1, string2 and string3 ?: String string1 = ""; String string2 = null; String string3; See TestNull.java. Is an unassigned primitive type variable null ? What are the contents of an uninitialized array? Winter 2015CMPE212 - Prof. McLeod23

null References, Cont. The idea of a null reference was first introduced into ALGOL W back in 1965 by C.A.R. Hoare (also known as the inventor of Quicksort). See: References-The-Billion-Dollar-Mistake-Tony-Hoare Winter 2015CMPE212 - Prof. McLeod24

Using null in Java You can test to see if a pointer is null (See file input code, for example. This is how you detect the end of the file.) Sometimes, to widen the scope of a variable you need to declare it before you can instantiate it. In this case you must assign the variable pointer to null. “Bad things” will happen if you do not! A NullPointerException is probably the most frustrating error to encounter in Java! Winter 2015CMPE212 - Prof. McLeod25