Variables,
constants and literals
We need to store and process data values
when writing computer programs. For object-oriented programming languages like Java, we
need to be able to define and set the values for the variables required to implement
classes and their instance objects.
In the implementation of methods, as well as
class and instance variables, temporary (local) variables are often needed (for counting,
or storing the intermediate results of calculations etc.).
A variable is any named piece of data that can
change during the execution of a program. A non-trivial program will use many variables
— perhaps thousands of them.
There are times when we wish to name some
value, but not have that value ever change. For example, we might wish to define some
mathematical constant, such as pi. Or perhaps decide what the width and height of
an application window will be, and not allow it to change. The term for a named value that
should never changes is a constant.
When we wish to refer to a constant value
itself (rather than a name for the value), we simply write a literal in our Java program.
Examples of literals include:
- 5
- 10.5
- 't'
- "hello there"
- true
- -23.34555
Back
to top

RITSEC - Global Campus
Copyright ?1999 RITSEC- Middlesex University. All rights reserved.
webmaster@globalcampus.com.eg
|