0 ratings0% found this document useful (0 votes) 8 views4 pagesVariable in Java 5thsem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here.
Available Formats
Download as PDF or read online on Scribd
Scanned with CamScannerCwwotar ds -vakeinyy en pmaatub tomas? ites?
Her are several examples of variable declarations of vastous type, Noted eo
an initialization.
#)
int a, by cr V7 dectater-thres (bea 8. Pi PEs Sy
int d23, e, £.= 5: // declares three more ints, initializing
~ (1 dana t. own,
byte 2,=/22: // initializes 2.
double pi = 3.14159; 7/ declares an approximation Of Pt
char X = // the variable x has the value ‘X's
their names that indicates
‘The identifiers that you choose have nothing intrinsic in #
their type. Many readers will remember when FORTRAN predefined all identifiers
front I through N to be of type INTEGER while all other identifiers were REAL. Java
allows any properly formed identifier to have any declared type-
Dynamic Initialization
[Although the preceding examples have used only constants as initializers, Java allows
variables to be initialized dynamically, using any expression valid at the time the variable
isdeclared.
For example, here is a short program that computes the length of the hypotenuse oft
a right triangle given the lengths of its two opposing sides:
amic initiaTization*
7/ Demonstrate dyn
class DyniInit {
public static void main(String args{]) ( ‘
double a = 3.0, b= 4.01
4/ ¢ is dynamically initialized
oul e.c):4) Mag. sarh (at) antirbut Phe .
System.out .print1n ("Hypote
nuse is" +.C);
Scanned with CamScannerDBD oso ene sonite ttre i
of Variables
The Scope and Jbles used have been declared at the start of the main()
Pree epi vas .s to be declared w! thin any. block. As explained in
Syesran with an-opening curly brace and ended by a closing cu
‘ r
brace, ioe Uefines 8 ‘scope. Thus, each time you sta
‘As you probably know from your previous
Se orines what objects are visible to other parts of your progra
lifetime of those objects. ; 2 7
the ‘Most other computer languages define two general categorie of scopes: Ba
ete tiorrerer these ditional scopes do not fit well with Java’s strict, object
rented model. While it is possible tocreate what amounts to being a global scope,
it is by far the exception, not the vate Ga Java, the two major scopes are those d
__py-aclass and those defined by a method. Even this distinction is somewhat artific
*, Sopcreetince the class scope has several unique properties and attributes that dong
apply to the scope defined by a method, this distinction makes some sense. Because of
the differences, a discussion of class scope (and variables declared within it) is def
until Chapter 6, when classes are described. For now, we will only examine the
efined by or within a method.
scope defined by a method begin
Sema has parameters. they (00 are ee
this book will look more closely at paramet Chapter 5, for the sake of this
work the same as any other method variable.
Asa rule, variables declared insi c
ined outside that scope. Thus,
fantd
a
you are localizing that variable and protecting it from u rized a
| en PSS Tey ie Scope rules provide the foundation for enca ion.
Bie ested For example, each time you cents slack ote aa
ng a new, nested : ite a block of cods
Thahabjee cri, When this occurs, the outer scope encloses oc
iicope. However, the r ae the outer scope will be visible to code within the
be visible outeide tt ue: Objects declared within the inner sco
e the
Stand the effect of nested Scopes, consider the following program:
Scanned with CamScanneron se eRe
int y = 20; // known tiie
only to 4 ne
© this block
// x and y both known
A
system. out .printin (my We
ee y* 2; uh
eK EPS:
)
PPP TOY PREECE HAO NeaL
ere
// x is still known here
System.out printin("x is «4 x)
’
As the comments indicate, the variable x is d in()’s scope and
Semele al suber code witan at se see
Since a block defines a scope, y is only visible to other code within its block. This is
why outside of its block, the line y = 100; is commented out. If you remove the leading
comment symbol, a compile-time error will occur, because y is not visible outside of its
block. Within the if block, x can be used because code within a block (that is, anested _
scope) has access to variables declared by an enclosing scope.
Within a block, variables can be declared at any point, but are valid only after they
are declared. Thus, if you define a variable at the start of a method, it is available to all
of the code within that method. Conversely, if you declare a variable at the end of a
block, it is effectively useless, because no code will have access to it. For example, this
fragment is invalid because count cannot be used prior to its declaration:
// This fragment is wrong!
count = 100; // oops! cannot use count before it is declared!
int count;
"Here is another important point to remember: variables are created when their
is entered, and destroyed when their scope is left, This means that a variable ”
Inot hold its value once it has gone out of scope, Therefore, variables declared
Within a method will not hold their values between calls to that method. Also, a
_ declared within a block will lose its value when the block is left. Thus, the
iable is confined to its scope- ‘
Be variable is ine judes an initializer, then that variable will be
Scanned with CamScanner