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