
Arrays and Reference Semantics in Programming
Dive into the world of arrays and reference semantics, exploring the differences between value semantics and reference semantics when working with primitive types and objects. Discover how variables and parameters hold values or references, and get ready to test your knowledge with interactive quizzes and exercises.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
CSE 121 Lesson 14: More Arrays and Reference Semantics Matt Wang & Brett Wortzman Autumn 2024 Abby Afifah Ailsa Alice Aliyan Arohan Chlo Christopher Dalton Derek Elizabeth Ethan TAs: Hanna Hannah Heather Hibbah Janvi Jasmine Judy Julia Kelsey Lucas Luke Mahima Maitreyi Maria Merav Minh Neha Ronald Ruslana Sahej Sam Samrutha Sushma Vivian sli.do #cse121 Today s playlist: 121 24au lecture tunes Yijia Zachary Lesson 13 - Autumn 2024
Reminders & Announcements C3 released tonight, due Tuesday, November 19th R4 closes tomorrow (last chance for C1) Quiz 2 next Thursday, November 21st topics: everything up until arrays (incl. today s material) Practice quiz out later this week Bonus resub for PSW coming later this week In the future: Final Exam (Wednesday, December 11thfrom 12:30 2:20 PM) more logistical details coming soon! Lesson 15 - Spring 2024
(PCM) Value Semantics vs. Reference Semantics Applies when working with primitive types Applies when working with objects Variables/parameters hold a copy of the actual value Variables/parameters hold a reference to the object Lesson 15 - Spring 2024
(PCM) Value Semantics vs. Reference Semantics int[] list1 = {4, 8, 15, 16, 23}; int[] list2 = list1; int a = 3; int b = a; a = 99; list1[1] = 99; Lesson 15 - Spring 2024
Poll in with your answer! Without knowing what someMethod does, what are the possible values of num? sli.do #cse121 A.anything! int num = 42; someMethod(num); System.out.println(num); B.just 42 Lesson 15 - Spring 2024
Poll in with your answer! Without knowing what anotherMethod does, what are the possible values of nums[0]? sli.do #cse121 A.anything! int[] nums = {42, 43, 44}; anotherMethod(nums); System.out.println(nums[0]); B.just 42 Lesson 15 - Spring 2024
(PCM) Value Semantics vs. Reference Semantics boolean[] tests = boolean test = true; {true, true, false, true, false, false}; flipValue(test); flipValues(tests); public static void flipValue(boolean b) { public static void flipValues(boolean[] b) { b = !b; for (int i = 0; i < b.length; i++) { } b[i] = !b[i]; } } Lesson 15 - Spring 2024
(PCM) null The absence of a reference! Sort of like a "zero-equivalent" for references! Default value for object types (e.g. Random, Scanner ) NullPointerExceptions are an error that happen when you ask null to do something call .toUpperCase() on null? NullPointerException! get .nextInt() from null? NullPointerException! many, many more Lesson 15 - Spring 2024
null: the billion dollar mistake From Sir Tony Hoare ( inventor of null, Turing award winner): I call it my billion-dollar mistake [ ] But I couldn t resist the temptation to put in a null reference, simply because it was so easy to implement. This has led to innumerable errors, vulnerabilities, and system crashes, which have probably caused a billion dollars of pain and damage in the last forty years. (quote from 2009 talk) Lesson 15 - Spring 2024
(PCM) avoiding NullPointerException if (strs[i] != null) { System.out.println(strs[i].toUpperCase()); } else { System.out.println("element " + i + " is null."); } Lesson 15 - Spring 2024