Docsity

Local Variables in Java: Creation, Destruction, Scope, and Naming, Study notes of Computer Science

Toggle sidebar
bg1
1
CS101J, Cornell 0
Local variables
Chapter
Local variable: A variable declared in a method
body.
/** = smallest of x, y, and z */
public void m(int x , int y, int z) {
if (x > y) {
// Swap x and y.
int temp;
temp= x; x= y; y= temp;
}
if (x > y) {
// Swap x and z.
int temp;
temp= x; x= z; z= temp;
}
return x;
}
CS101J, Cornell 1
Local variables
Chapter
Local variable: A variable declared in a method
body.
Form of declaration:
<type> <variable-name> ;
or
<type> <variable-name> = <expre ssion> ;
// Swap x and z.
int temp= x;
x= z;
z= temp;
1. When a local variable is created and destroyed.
2. The scope of a local variable.
3. Guidelines for naming a local variable.
CS101J, Cornell 2
Creation & destruction of local variables
/** = smallest of x, y, and z */
public void m(int x , int y, int z) {
if (x > y) {
// Swap x and y.
int temp;
temp= x; x= y; y= temp;
}
if (x > y) {
// Swap x and z.
int temp;
temp= x; x= z; z= temp;
}
return x;
}
call: m(5, 7, 3)
All pars & local variables created, and arg
values stored in pars, before execution of
method body
Exist as
long as
body is
executed
Destroyed
when
execution
terminates
temp
?
temp
?
x
5
z 3
y 7
CS101J, Cornell 3
Scope of local variables
/** = sum of values in range m..n.
Precondition: m <= n+1. */
public void p(int m , int n) {
int s= m;
int k;
// inv: s = sum of m..k–1
for (k= m; k <= n; k= k+1) {
s= s + k;
}
return s;
}
call: p(5, 7)
Scope of local variable: from just after its dec-
laration to end of block in which it is declared.
m
5
n 7 s ? k ?
Scope
of s Scope
of k
k= 10; illegal
CS101J, Cornell 4
Scope of for-loop counter
/** = sum of values in range m..n.
Precondition: m <= n+1. */
public void p(int m , int n) {
int s= m;
// inv: s = sum of m..k–1
for (int k= m; k <= n; k= k+1) {
s= s + k;
}
return s;
}
call: p(5, 7)
Scope of local variable: from just after its dec-
laration to end of block in which it is declared.
m
5
n 7 s ? k ?
Scope
of k
k= 10; illegal
CS101J, Cornell 5
Local-variable names
/** = sum of values in range m..n.
Precondition: m <= n+1. */
public void p(int m , int n) {
int s= m;
// inv: s = sum of m..k–1
for (int k= m; k <= n; k= k+1) {
s= s + k;
}
return s;
}
pf2

Partial preview of the text

Download Local Variables in Java: Creation, Destruction, Scope, and Naming and more Study notes Computer Science in PDF only on Docsity!

CS101J, Cornell 0 Local variables Chapter Local variable : A variable declared in a method body. /** = smallest of x, y, and z */ public void m( int x, int y, int z) { if (x > y) { // Swap x and y. int temp; temp= x; x= y; y= temp; } if (x > y) { // Swap x and z. int temp; temp= x; x= z; z= temp; } return x; } CS101J, Cornell 1 Local variables Chapter Local variable : A variable declared in a method body. Form of declaration : < type > < variable-name > ; or < type > < variable-name > = < expression > ; // Swap x and z. int temp= x; x= z; z= temp;

  1. When a local variable is created and destroyed.
  2. The scope of a local variable.
  3. Guidelines for naming a local variable. CS101J, Cornell 2 Creation & destruction of local variables /** = smallest of x, y, and z / public void m( int x, int y, int z) { if (x > y) { // Swap x and y. int temp; temp= x; x= y; y= temp; } if (x > y) { // Swap x and z. int temp; temp= x; x= z; z= temp; } return x; } call: m(5, 7, 3) All pars & local variables created, and arg values stored in pars, before execution of method body Exist as long as body is executed Destroyed when terminates^ execution temp ? temp ? x 5 y 7 z 3 CS101J, Cornell 3 Scope of local variables /* = sum of values in range m..n. Precondition: m <= n+1. / public void p( int m, int n) { int s= m; int k; // inv: s = sum of m..k– 1 for (k= m; k <= n; k= k+1) { s= s + k; } return s; } call: p(5, 7) Scope of local variable : from just after its dec- laration to end of block in which it is declared. m 5 n 7 s? k? Scope of s Scope of k k= 10; illegal CS101J, Cornell 4 Scope of for-loop counter /* = sum of values in range m..n. Precondition: m <= n+1. / public void p( int m, int n) { int s= m; // inv: s = sum of m..k– 1 for ( int k= m; k <= n; k= k+1) { s= s + k; } return s; } call: p(5, 7) Scope of local variable : from just after its dec- laration to end of block in which it is declared. m 5 n 7 s? k? Scope of k k= 10; illegal CS101J, Cornell 5 Local-variable names /* = sum of values in range m..n. Precondition: m <= n+1. */ public void p( int m, int n) { int s= m; // inv: s = sum of m..k– 1 for ( int k= m; k <= n; k= k+1) { s= s + k; } return s; }

CS101J, Cornell 6 Long parameter names complicate /** = sum of values in range first_value..last_value. Precondition: first_value <= last_value+1. / public void p( int first_value, int last_value) { int s= first_value; // inv: s = sum of first_value..k– 1 for ( int k= first_value; k <= last_value; k= k+1) { s= s + k; } return s; } CS101J, Cornell 7 Short parameter names simplify /* = sum of values in range m..n. Precondition: m <= n+1. */ public void p( int m, int n) { int s= m; // inv: s = sum of m..k– 1 for ( int k= m; k <= n; k= k+1) { s= s + k; } return s; } Short parameter names are better —as long as the specification mentions the parameters appropriately. Short local-variables names are better —as long the local variables are appropriately described. — as do short local-variable names