Mark Fontenot CSE 1341 - Honors Principles of Computer Science I Note Set 7.

Slides:



Advertisements
Similar presentations
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Advertisements

1 Classes and Objects in Java Parameter Passing, Delegation, Visibility Control, and Object Cleanup.
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Arrays as Parameters reading: , 3.3 self-checks: Ch. 7 #5, 8,
CS 106 Introduction to Computer Science I 03 / 08 / 2010 Instructor: Michael Eckmann.
Introduction to Computers and Programming Strings Professor: Evan Korth New York University.
 To be able to write larger programs ◦ By breaking them down into smaller parts and passing data between the parts.  To understand the concepts of Methods.
03/16/ What is an Array?... An array is an object that stores list of items. Each slot of an array holds an individual element. Characteristics.
Pointers CSE 2451 Rong Shi.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 4.
SE-1010 Dr. Mark L. Hornick 1 Defining Your Own Classes Part 3.
Methods (Functions) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
CSE 2341 Honors Professor Mark Fontenot Southern Methodist University Note Set 04.
Arrays (Part 1) Computer Science Erwin High School Fall 2014.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
More arrays Primitive vs. reference parameters. Arrays as parameters to functions.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 10.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 5.
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 6.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
WEEK 2 Introduction to Java II CSE 252 Principles of Programming Languages LAB SECTION.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 11.
Mark Fontenot CSE Honors Principles of Computer Science I Note Set 15.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 7.
CSE 1341 Honors Note Set 2 1. Overview  Java vs. C++  Functions in C++  First Programming Packet  Development Environment 2.
© A+ Computer Science - In Java, any variable that refers to an Object is a reference variable. The variable stores the memory.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 20.
1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.
Strings CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
CSC 142 F 1 CSC 142 References and Primitives. CSC 142 F 2 Review: references and primitives  Reference: the name of an object. The type of the object.
Methods.
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 12.
AP Computer Science A – Healdsburg High School 1 Unit 9 - Parameter Passing in Java.
FUNCTIONS (METHODS) Pascal C, C++ Java Scripting Languages Passing by value, reference Void and non-void return types.
Computer Science A 1. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated editor,
05 Method Calling. 2 What is a Method? Declaring a Method Method Calling Method Call Stack Parameter Passing Pass by Value Outline.
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
Recursion occurs when a method calls itself. Google “recursion”
Recursion occurs when a method calls itself. public class RecursionOne { public void run(int x) { System.out.println(x); run(x+1); } public static void.
Staples are our staple Building upon our solution.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 10.
1 reading: 3.3 Using objects. Objects So far, we have seen: methods, which represent behavior variables, which represent data (categorized by types) It.
Introduction to Computer Science What is Computer Science? Getting Started Programming.
Mark Fontenot CSE Honors Principles of Computer Science I Note Set 6.
using System; namespace Demo01 { class Program
USING ECLIPSE TO CREATE HELLO WORLD
Introduction to Computing Using Java
Haidong Xue Summer 2011, at GSU
CSE 143 Lecture 9 References and Linked Nodes reading: 3.3; 16.1
Lecture 11 C Parameters Richard Gesick.
An Introduction to Java – Part II
An Introduction to Java – Part I, language basics
Classes & Objects: Examples
Parameter Passing in Java
CSE 214 – Computer Science I More on Linked Lists
class PrintOnetoTen { public static void main(String args[]) {
Why did the programmer quit his job?
A Java Application public class Hello { public static void main(String [] args) { System.out.println("Hello, World!"); } } public class.
A Java Application public class Hello { public static void main(String [] args) { System.out.println("Hello, World!"); } } public class.
Scope scope: The part of a program where a variable exists. From its declaration to the end of the { } braces A variable declared in a for loop exists.
Lecture 11 Parameters CSE /26/2018.
Generic Programming.
Lecture 6: References and linked nodes reading: 16.1
Methods/Functions.
CIS 110: Introduction to Computer Programming
Lecture 6: References and linked nodes reading: 16.1
Presentation transcript:

Mark Fontenot CSE Honors Principles of Computer Science I Note Set 7

Note Set 7 Overview Parameter Passing Semantics in Java Pass By Value Implication for Primitive Variables Implication for Object References and for String Objects?

Pass – by – Value Means that the contents of the argument are copied into the parameter Always the way it is done in Java – but other methods exist in other programming languages public class Test1 { public static void foo(int x) { x++; } public static void main (String[]args) { int z = 10; foo(z); } 10 z x

Primitive Variables public class Test1 { public static void foo(int x) { x++; } public static void main (String[]args) { int z = 10; foo(z); } } z x when method is called, value of z is copied into x. We can conclude that…

Passing an Object Reference public class Test2 { public static void bar(Point x) { x.setLocation(10, 20); } public static void main(String [] args) { Point p = new Point(2, 3); System.out.printf("p = %s\n", p.toString()); bar(p); System.out.printf("now p = %s\n", p.toString()); } p = java.awt.Point[x=2,y=3] now p = java.awt.Point[x=10,y=20] p = java.awt.Point[x=2,y=3] now p = java.awt.Point[x=10,y=20] OUTPUT:

Passing an Array public class Test3 { public static void fun(int [] x) { x[0] = 99999; } public static void main(String [] args) { int[] values = new int[3]; values[0] = 1; values[1] = 2; values[2] = 3; System.out.printf("value[0] = %d\n", values[0]); fun(values); System.out.printf("value[0] = %d\n", values[0]); } value[0] = 1 value[0] = value[0] = 1 value[0] = OUTPUT:

What’s going on here? public class Test2 { public static void bar(Point x) { x.setLocation(10, 20); } public static void main(String [] args) { Point p = new Point(2, 3); System.out.printf("p = %s\n", p.toString()); bar(p); System.out.printf("now p = %s\n", p.toString()); } Main: p 2 3 bar: x The object reference stored in p gets copied into x. System Stack

What’s going on here? public class Test2 { public static void bar(Point x) { x.setLocation(10, 20); } public static void main(String [] args) { Point p = new Point(2, 3); System.out.printf("p = %s\n", p.toString()); bar(p); System.out.printf("now p = %s\n", p.toString()); } } Main: p bar: x The object reference stored in p gets copied into x. System Stack

Strings mess it up public class Test4 { public static void main (String [] args) { String myString = "Mark"; System.out.printf("MyString 1.0 = %s\n", myString); function(myString); System.out.printf("MyString 2.0 = %s\n", myString); } public static void function(String s) { s = "Hello”; System.out.printf ("s = %s\n", s); } } MyString 1.0 = Mark s = Hello MyString 2.0 = Mark MyString 1.0 = Mark s = Hello MyString 2.0 = Mark OUTPUT: What’s going on here?