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

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

4 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

5 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

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

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?

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; } Where are the identifiers? (Actually, System, out, and println are identifiers as well.)

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

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

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

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

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

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 ); } What block is associated with each identifier?

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 ); } What block is associated with main?

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 ); } This block is associated with main.

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 ); } What block is associated with args?

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 ); } This block is associated with args.

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 ); } What block is associated with i?

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

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

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

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

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

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

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

27 Noteworthy case For loop

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

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

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

31 Noteworthy case Function parameters

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

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

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

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) ); System.out.println( square(kSquared) ); square x result main args k 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) ); System.out.println( square(kSquared) ); square x result 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) ); System.out.println( square(kSquared) ); square x result 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) ); System.out.println( square(kSquared) ); square x 10 result main args … k 10 kSquared

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) ); System.out.println( square(kSquared) ); square x 10 result 100 main args … k 10 kSquared

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) ); System.out.println( square(kSquared) ); square x 10 result 100 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) ); System.out.println( square(kSquared) ); square x result 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) ); System.out.println( square(kSquared) ); square x result 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) ); System.out.println( square(kSquared) ); square x 12 result main args … k 10 kSquared 100

44 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

45 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

46 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

47 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

48 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

49 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

50 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

51 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

52 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

53 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


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

Similar presentations


Ads by Google