Java Data Structures - Contents
A D V E R T I S E M E N T
Variables are the key to any program. There are
variables called registers inside every CPU (Central Processing Unit). Every program ever
written uses some form of variables. Believe it or not, the way you use variables can
significantly impact your program. This section is a very simple introduction to what
variables are, and how they're used in programs.
Usually, a variable implies a memory location to
hold one instance of one specific type. What this means is that if there is an integer
variable, it can only hold one integer, and if there is a character variable, it can only
hold one character.
There can be many different types of variables,
including of your own type. A sample declaration for different variable types is given
below.
boolean t;
byte b;
char c;
int i;
long l;
I believe the above is straight forward, and doesn't
need much explanation. Variable 't' is declared as boolean type,
and 'b' as of byte type, etc.
The above variables are what's know as primitive
types. Primitive types in Java means that you don't have to create them, they're already
available as soon as you declare them. (you'll see what I mean when we deal with Objects)
It also means that there is usually some hardware equivalent to these variables. For
example, an int type, can be stored in a 32 bit hardware register.
The other types of variables are instances of
classes or Objects. Java is a very Object Oriented language, and everything in
it, is an object. An object is an instance of a class. Your Java programs consist of
classes, in which you manipulate objects, and make the whole program do what you want.
This concept will be familiar to you if you've ever programmed C++, if not, think of
objects as structures. An example of a simple class would be:
public class pSimpleObject{
int i;
public pSimpleObject(){
i = 0;
}
public int get(){
return i;
}
public void set(int n){
i = n;
}
}
As you can see, first we specify that the class is public ,
this means that it can be visible to other objects outside it's file. We later say that
it's a class , and give it's name, which in this case is: pSimpleObject .
Inside of it, the class contains an integer named 'i' , and three functions.
The first function named pSimpleObject() , is the constructor. It is called
every time an object is created using this class. The set() and get()
functions set and get the value of 'i' respectively. One useful terminology
is that functions in objects are not called functions, they're called methods. So, to
refer to function set() , you'd say "method set() ."
That's all there is to objects!
The way you declare a variable, or in this case, an
object of that class, is:
pSimpleObject myObject;
myObject = new pSimpleObject();
or
pSimpleObject myObject = new pSimpleObject();
The first example illustrates how you declare an
object named myObject , of class pSimpleObject , and later
instantiate it (a process of actual creation, where it calls the object's constructor
method). The second approach illustrates that it all can be done in one line. The object
does not get created when you just declare it, it's only created when you do a new
on it.
If you're familiar with C/C++, think of objects as
pointers. First, you declare it, and then you allocate a new object to that pointer. The
only limitation seems to be that you can't do math on these pointers, other than that,
they behave as plain and simple C/C++ pointers. (You might want to think of objects as
references however.)
Using variables is really cool, and useful, but
sometimes we'd like to have more. Like the ability to work with hundreds or maybe
thousands of variables at the same time. And here's where our next section starts, the
Arrays!
Back to Table of Contents
A D V E R T I S E M E N T
|
Subscribe to SourceCodesWorld - Techies Talk |
|