AKA the birth, life, and death of variables.

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.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 3: Flow Control I: For Loops.
Introduction to Computer Science Robert Sedgewick and Kevin Wayne Recursive Factorial Demo pubic class Factorial {
Programming Methodology (1). Implementing Methods main.
Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
1 Calling within Static method /* We can call a non static method from a static method but by only through an object of that class. */ class Test1{ public.
Topic 10 Java Memory Management. 1-2 Memory Allocation in Java When a program is being executed, separate areas of memory are allocated for each class.
METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
CS324e - Elements of Graphics and Visualization A Little Java A Little Python.
Basic Algorithms on Arrays. Learning Objectives Arrays are useful for storing data in a linear structure We learn how to process data stored in an array.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Methods. int month; int year class Month Defining Classes A class contains data declarations (static and instance variables) and method declarations (behaviors)
Memory Management & Method Calls in Java Program Execution © Allan C. Milne v
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 10: 1 TEST!!
Java An introduction. Example 1 public class Example1 { public static void main (String [] args) { System.out.println (“This is the first example”); int.
Introduction to Computers and Programming Strings Professor: Evan Korth New York University.
COMP More About Classes Yi Hong May 22, 2015.
Methods Review. 2. Class-wide vs. local variables. 3. Why C# bans global variables. 4. Nested blocks. 5. Scope of identifiers.
Classes and Methods Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2014.
More arrays Primitive vs. reference parameters. Arrays as parameters to functions.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Methods (a.k.a. Functions)
1 Variables. 2 Receipt example What's bad about the following code? public class Receipt { public static void main(String[] args) { // Calculate total.
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
C/C++ 3 Yeting Ge. Static variables Static variables is stored in the static storage. Static variable will be initialized once. 29.cpp 21.cpp.
Writing Static Methods Up until now, we have been USING (calling) static methods that other people have written. Now, we will start CREATING our own static.
String and Scanner CS 21a: Introduction to Computing I First Semester,
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Method Overloading  Methods of the same name can be declared in the same class for different sets of parameters  As the number, types and order of the.
Methods. Overview Class/Library/static vs. Message/non-static return and void Parameters and arguments.
More loops while and do-while. Recall the for loop in general for (initialization; boolean_expression; update) { }
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
FOR LOOP WALK THROUGH public class NestedFor { public static void main(String [] args) { for (int i = 1; i
Building java programs, chapter 3 Parameters, Methods and Objects.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
Computer Science A 1. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated editor,
Declaring console static and global import java.util.*; public class Test { static Scanner console = new Scanner (System.in); public static void main(String[]
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
Staples are our staple Building upon our solution.
Variable scope. Variable Scope Variables do not live forever. Failing to take that into account leads to problems. Let's look at an example. Let's write.
Functions + Overloading + Scope
Java Memory Management
Chapter 2 Clarifications
Chapter 7 User-Defined Methods.
Java Memory Management
Examples of Classes & Objects
Department of Computer Science
CSC 253 Lecture 8.
Class Structure 16-Nov-18.
CSC 253 Lecture 8.
Arrays.
Classes & Objects: Examples
Variables and Their scope
Class Structure 7-Dec-18.
Class Structure 2-Jan-19.
CS2011 Introduction to Programming I Arrays (II)
CS2011 Introduction to Programming I Methods (II)
CS2011 Introduction to Programming I Arrays (I)
AKA the birth, life, and death of variables.
BBIT 212/ CISY 111 Object Oriented Programming (OOP)
© A+ Computer Science - OOP Pieces © A+ Computer Science -
Class Structure 25-Feb-19.
Scope of variables class scopeofvars {
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.
Methods (a.k.a functions)
Corresponds with Chapter 5
Scope Rules.
Presentation transcript:

AKA the birth, life, and death of variables. Scope of Identifiers AKA the birth, life, and death of variables.

Recall: What are identifiers? The names that we make up for variables, function names, and class names.

class MyClass { public static void main ( String args[] ) { int i=1; while (i <= 10) { System.out.println( “i has a value of ” + i ); i = i + 2; } Where are the identifiers?

class MyClass { public static void main ( String args[] ) { int i=1; while (i <= 10) { System.out.println( “i has a value of ” + i ); i = i + 2; } Where are the identifiers?

class MyClass { public static void main ( String args[] ) { int i=1; while (i <= 10) { System.out.println( “i has a value of ” + i ); i = i + 2; } Where are the identifiers? (Actually, System, out, and println are identifiers as well.)

Identifiers Declaration When identifiers “come alive” (i.e., begin to exist or are allocated memory in the computer) As opposed to when identifier are used (i.e., variables are assigned values, functions are called, variables are referred to)

class MyClass { public static void main ( String args[] ) { int i=1; while (i <= 10) { System.out.println( “i has a value of ” + i ); i = i + 2; } Where are the identifiers declared?

class MyClass { public static void main ( String args[] ) { int i=1; while (i <= 10) { System.out.println( “i has a value of ” + i ); i = i + 2; } Identifiers are made up (i.e., declared) as indicated.

public static void main ( String args[] ) { int i=1; class MyClass { public static void main ( String args[] ) { int i=1; while (i <= 10) { System.out.println( “i has a value of ” + i ); i = i + 2; } Identifiers are made up (i.e., declared) as indicated. (System is defined by Sun as part of the System class and contains out. Out is of class type PrintStream which is also declared by Sun. Println is a method in the PrintStream class.)

Using identifiers 1. Must be declared before they can be used. System.out.println( i ); int i = 12; vs. Which is incorrect?

Using identifiers (Typically) must be within a block (or associated with a block). class MyClass { public static void main ( String args[] ) { int i=1; while (i <= 10) { double d = 52.0; System.out.println( “i has a value of ” + i ); i = i + 2; } What block is associated with each identifier?

Using identifiers (Typically) must be within a block (or associated with a block). class MyClass { public static void main ( String args[] ) { int i=1; while (i <= 10) { double d = 52.0; System.out.println( “i has a value of ” + i ); i = i + 2; } What block is associated with main?

Using identifiers (Typically) must be within a block (or associated with a block). class MyClass { public static void main ( Strings args[] ) { int i=1; while (i <= 10) { double d = 52.0; System.out.println( “i has a value of ” + i ); i = i + 2; } This block is associated with main.

Using identifiers (Typically) must be within a block (or associated with a block). class MyClass { public static void main ( String args[] ) { int i=1; while (i <= 10) { double d = 52.0; System.out.println( “i has a value of ” + i ); i = i + 2; } What block is associated with args?

Using identifiers (Typically) must be within a block (or associated with a block). class MyClass { public static void main ( String args[] ) { int i=1; while (i <= 10) { double d = 52.0; System.out.println( “i has a value of ” + i ); i = i + 2; } This block is associated with args.

Using identifiers (Typically) must be within a block (or associated with a block). class MyClass { public static void main ( String args[] ) { int i=1; while (i <= 10) { double d = 52.0; System.out.println( “i has a value of ” + i ); i = i + 2; } What block is associated with i?

Using identifiers (Typically) must be within a block (or associated with a block). class MyClass { public static void main ( String args[] ) { int i=1; while (i <= 10) { double d = 52.0; System.out.println( “i has a value of ” + i ); i = i + 2; } This block is associated with i.

Using identifiers (Typically) must be within a block (or associated with a block). class MyClass { public static void main ( String args[] ) { int i=1; while (i <= 10) { double d = 52.0; System.out.println( “i has a value of ” + i ); i = i + 2; } What block is associated with d?

Using identifiers (Typically) must be within a block (or associated with a block). class MyClass { public static void main ( String args[] ) { int i=1; while (i <= 10) { double d = 52.0; System.out.println( “i has a value of ” + i ); i = i + 2; } This block is associated with d.

Using identifiers 3. (Typically) lives within a block – dies at end of block. class MyClass { public static void main ( String args[] ) { int i=1; while (i <= 10) { double d = 52.0; System.out.println( “i has a value of ” + i ); i = i + 2; } System.out.println( “d has a value of ” + d ); What happens?

Using identifiers 4. Cannot be redefined within a block. class MyClass { public static void main ( String args[] ) { int i=1; while (i <= 10) { double d = 52.0; System.out.println( “i has a value of ” + i ); d = i; i = i + 2; double d; } What happens?

Using identifiers 4. Cannot be redefined within a block. class MyClass { public static void main ( String args[] ) { int i=1; while (i <= 10) { double d = 52.0; System.out.println( “i has a value of ” + i ); i = i + 2; } double d = 22; What happens?

Summary of rules for using identifiers Must be declared before they can be used. (Typically) must be within a block (or associated with a block). (Typically) lives within a block – dies at end of block. Cannot be redefined within a block.

Noteworthy case For loop

Noteworthy case: for loop for (int i=0; i<10; i++) { System.out.println( “i is ” + i ); } What is the scope (in what block does i exist) of i?

Noteworthy case: for loop for (int i=0; i<10; i++) { System.out.println( “i is ” + i ); } Here is the scope (in what block does i exist) of i.

Noteworthy case: for loop for (int i=0; i<10; i++) { System.out.println( “i is ” + i ); } What happens?

Noteworthy case Function parameters

Noteworthy case: function parameters class MyClass { public static double square ( double x ) { double result; result = x * x; return result; } public static void main ( String args[] ) { … double k = in.nextDouble(); double kSquared = square( k ); System.out.println( k ); System.out.println( kSquared );

Noteworthy case: function parameters class MyClass { public static double square ( double x ) { double result; result = x * x; return result; } public static void main ( String args[] ) { … double k = in.nextDouble(); double kSquared = square( k ); System.out.println( x ); System.out.println( result );

Noteworthy case: function parameters class MyClass { public static double square ( double x ) { double result; result = x * x; return result; } public static void main ( String args[] ) { … double k = in.nextDouble(); double kSquared = square( k ); System.out.println( x ); //x died at the end of square method! System.out.println( result ); // so did result!!

Function parameters & memory maps class MyClass { public static double square ( double x ) { double result; result = x * x; return result; } public static void main ( String args[] ) { … double k = in.nextDouble(); double kSquared = square( k ); System.out.println( square(12) ); main args

Function parameters & memory maps class MyClass { public static double square ( double x ) { double result; result = x * x; return result; } public static void main ( String args[] ) { … double k = in.nextDouble(); double kSquared = square( k ); System.out.println( square(12) ); main args … k 10

Function parameters & memory maps class MyClass { public static double square ( double x ) { double result; result = x * x; return result; } public static void main ( String args[] ) { … double k = in.nextDouble(); double kSquared = square( k ); System.out.println( square(12) ); main args … k 10 kSquared

Function parameters & memory maps class MyClass { public static double square ( double x ) { double result; result = x * x; return result; } public static void main ( String args[] ) { … double k = in.nextDouble(); double kSquared = square( k ); System.out.println( square(12) ); square x 10 result main args … k 10 kSquared

Function parameters & memory maps class MyClass { public static double square ( double x ) { double result; result = x * x; return result; } public static void main ( String args[] ) { … double k = in.nextDouble(); double kSquared = square( k ); System.out.println( square(12) ); square x 10 result 100 main args … k 10 kSquared

Function parameters & memory maps class MyClass { public static double square ( double x ) { double result; result = x * x; return result; } public static void main ( String args[] ) { … double k = in.nextDouble(); double kSquared = square( k ); System.out.println( square(12) ); square x 10 result 100 main args … k 10 kSquared

Function parameters & memory maps class MyClass { public static double square ( double x ) { double result; result = x * x; return result; } public static void main ( String args[] ) { … double k = in.nextDouble(); double kSquared = square( k ); System.out.println( square(12) ); main args … k 10 kSquared 100

Function parameters & memory maps class MyClass { public static double square ( double x ) { double result; result = x * x; return result; } public static void main ( String args[] ) { … double k = in.nextDouble(); double kSquared = square( k ); System.out.println( square(12) ); main args … k 10 kSquared 100

Function parameters & memory maps class MyClass { public static double square ( double x ) { double result; result = x * x; return result; } public static void main ( String args[] ) { … double k = in.nextDouble(); double kSquared = square( k ); System.out.println( square(12) ); square x 12 result main args … k 10 kSquared 100

Function parameters & memory maps class MyClass { public static double square ( double x ) { double result; result = x * x; return result; } public static void main ( String args[] ) { … double k = in.nextDouble(); double kSquared = square( k ); System.out.println( square(12) ); square x 12 result 144 main args … k 10 kSquared 100

Function parameters & memory maps class MyClass { public static double square ( double x ) { double result; result = x * x; return result; } public static void main ( String args[] ) { … double k = in.nextDouble(); double kSquared = square( k ); System.out.println( square(12) ); square x 12 result 144 main args … k 10 kSquared 100

Function parameters & memory maps class MyClass { public static double square ( double x ) { double result; result = x * x; return result; } public static void main ( String args[] ) { … double k = in.nextDouble(); double kSquared = square( k ); System.out.println( square(12) ); main args … k 10 kSquared 100