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

AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
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.
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.
CS324e - Elements of Graphics and Visualization A Little Java A Little Python.
Methods. int month; int year class Month Defining Classes A class contains data declarations (static and instance variables) and method declarations (behaviors)
Java Basics M Taimoor Khan
Access to Names Namespaces, Scopes, Access privileges.
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
Variables, Data Types, & Arithmetic Expressions CSC 1401: Introduction to Programming with Java Lecture 3 Wanda M. Kunkle.
 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.
Hello, world! Dissect HelloWorld.java Compile it Run it.
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.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
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.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Methods (a.k.a. Functions)
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
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.
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.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
Java Nuts and Bolts Variables and Data Types Operators Expressions Control Flow Statements Arrays and Strings.
1 Chapter 2: Java Fundamentals cont’d Spring Lory Al Moakar.
Building java programs, chapter 3 Parameters, Methods and Objects.
1 09/03/04CS150 Introduction to Computer Science 1 What Data Do We Have.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
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];
OOP Basics Classes & Methods (c) IDMS/SQL News
Chapter 9 Introduction to Arrays Fundamentals of Java.
Topics for today: 1.Comments 2.Data types 3.Variable declaration.
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
Java 5 Class Anatomy. User Defined Classes To this point we’ve been using classes that have been defined in the Java standard class library. Creating.
Review by Mr. Maasz, Summary of Chapter 2: Starting Out with Java.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
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 7 User-Defined Methods.
Java Memory Management
Examples of Classes & Objects
AKA the birth, life, and death of variables.
Department of Computer Science
Data types and variables
Introduction to Programming in Java
Arrays.
An Introduction to Java – Part I, language basics
Variables and Their scope
CS2011 Introduction to Programming I Arrays (II)
CS2011 Introduction to Programming I Methods (II)
CS2011 Introduction to Programming I Arrays (I)
BBIT 212/ CISY 111 Object Oriented Programming (OOP)
Expressions and Assignment
Scope of variables class scopeofvars {
Programming Language C Language.
Classes, Objects and Methods
Scope of Identifier The Scope of an identifier (or named constant) means the region of program where it is legal to use that.
ENERGY 211 / CME 211 Lecture 8 October 8, 2008.
Methods (a.k.a functions)
Variables and Constants
Scope Rules.
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Coming up Variables Quick look at scope Primitives Objects
Presentation transcript:

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

What are identifiers? The names that we make up for variables, function names, and class names. Valid identifiers follow the rule: {a..z,A..Z,_,$}{a..z,A..Z,_,$,0..9}* But not language reserved/keywords for, if, int, class, … What else can you think of?

What are identifiers? Keywords we have used for far: char class do double else float if int for public short static void while

Valid identifiers? Valid identifiers follow the rule: {a..z,A..Z,_,$}{a..z,A..Z,_,$,0..9}* Area_triangle A.2 2 a _ _1 a2 while a_2 2a 1_ System.out.println

Valid identifiers? Valid identifiers follow the rule: {a..z,A..Z,_,$}{a..z,A..Z,_,$,0..9}* Area_triangle A.2 2 a _ _1 A2 while a_2 2a 1_ System.out.println

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.)

Indentifiers 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 ); } 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 ); } 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 ); } 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 (“rules”) 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 ); } 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 ); } 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 ( String args[] ) { int i=1; while (i <= 10) { double d = 52.0; System.out.println( “i has a value of ” + i ); } 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 ); } 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 ); } 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 ); } 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 ); } 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 ); } 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 ); } 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 ); } 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; 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 ); } 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( square(12) ); System.out.println( square(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) ); System.out.println( square(kSquared) ); square x result main args k 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) ); System.out.println( square(kSquared) ); square x 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) ); System.out.println( square(kSquared) ); square x 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) ); System.out.println( square(kSquared) ); 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) ); System.out.println( square(kSquared) ); 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) ); System.out.println( square(kSquared) ); square x 10 result 100 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) ); System.out.println( square(kSquared) ); square x 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) ); System.out.println( square(kSquared) ); square x 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) ); System.out.println( square(kSquared) ); 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) ); System.out.println( square(kSquared) ); 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) ); System.out.println( square(kSquared) ); 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) ); System.out.println( square(kSquared) ); square x result main args … k 10 kSquared 100

A simpler square function class MyClass { public static double square ( double k ) { k = k * k; return k; } public static void main ( String args[] ) { … double k = in.nextDouble(); double kSquared = square( k ); System.out.println( square(12) ); System.out.println( square(kSquared) ); square k main args … k kSquared

A simpler square function class MyClass { public static double square ( double k ) { k = k * k; return k; } public static void main ( String args[] ) { … double k = in.nextDouble(); double kSquared = square( k ); System.out.println( square(12) ); System.out.println( square(kSquared) ); square k main args … k 9 kSquared

A simpler square function class MyClass { public static double square ( double k ) { k = k * k; return k; } public static void main ( String args[] ) { … double k = in.nextDouble(); double kSquared = square( k ); System.out.println( square(12) ); System.out.println( square(kSquared) ); square k main args … k 9 kSquared

A simpler square function class MyClass { public static double square ( double k ) { k = k * k; return k; } public static void main ( String args[] ) { … double k = in.nextDouble(); double kSquared = square( k ); System.out.println( square(12) ); System.out.println( square(kSquared) ); square k 9 main args … k 9 kSquared

A simpler square function class MyClass { public static double square ( double k ) { k = k * k; return k; } public static void main ( String args[] ) { … double k = in.nextDouble(); double kSquared = square( k ); System.out.println( square(12) ); System.out.println( square(kSquared) ); square k 81 main args … k 9 kSquared

A simpler square function class MyClass { public static double square ( double k ) { k = k * k; return k; } public static void main ( String args[] ) { … double k = in.nextDouble(); double kSquared = square( k ); System.out.println( square(12) ); System.out.println( square(kSquared) ); square k 81 main args … k 9 kSquared

A simpler square function class MyClass { public static double square ( double k ) { k = k * k; return k; } public static void main ( String args[] ) { … double k = in.nextDouble(); double kSquared = square( k ); System.out.println( square(12) ); System.out.println( square(kSquared) ); square k 81 main args … k 9 kSquared 81