Presentation is loading. Please wait.

Presentation is loading. Please wait.

AKA the birth, life, and death of variables.

Similar presentations


Presentation on theme: "AKA the birth, life, and death of variables."— Presentation transcript:

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

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

3 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?

4 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?

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

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

7 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?

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

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

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

11 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?

12 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?

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

14 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?

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

16 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?

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

18 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?

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

20 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?

21 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?

22 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?

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

24 Noteworthy case For loop

25 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?

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

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

28 Noteworthy case Function parameters

29 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 );

30 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 );

31 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!!

32 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

33 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

34 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

35 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

36 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

37 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

38 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

39 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

40 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

41 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

42 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

43 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


Download ppt "AKA the birth, life, and death of variables."

Similar presentations


Ads by Google