Java Data Structures

Peter Kitson

ISBN : -

Order a printed copy of this book from Amazon --UNAVAILABLE--


Cover Design - Java Data Structures
 

For your free electronic copy of this book please verify the numbers below. 

(We need to do this to make sure you're a person and not a malicious script)

Numbers

 




Sample Chapter From Java Data Structures
     Copyright © Particle



Introduction


In contrast to what most people think about Java, it being a language with no pointers, data structures are quite easy to implement. In this section, I\'ll demonstrate few basic data structures. By learning how easy they are to implement in Java, you\'ll be able to write any implementation yourself.

    I also think that this document is a pretty good introduction to Data Structures in general. All these concepts can be applied in any programming language. Incidentally, most of these programs are ported from their C++ counterparts. So, if you want to learn Data Structures in C/C++, you\'ll still find this document useful! Java is an Object Oriented language, but more so than C++, so, most data structure concepts are expressed and illustrated \'more naturally\' in Java! (try not to raise your blood pressure from all the caffeine)

    I suggest that you be familiar with Java format, and know some other programming language in advance. Coincidentally, I and a couple of my friends are in the process of writing a C language book, which deals with all that \'start up\' stuff.

    The way most examples are executed is through the JDK\'s command line Java interpreter. (at the prompt, you just type \'java\' and the name of the class to run.)

Variables...

    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!