Jump to content
xisto Community
Sign in to follow this  

A Complete Java Tutorial

Recommended Posts

The Java Programming Manual



Chapter 0: Introduction.

What Java is and what it is not


Java is an object oriented programming language that has evolved from and to substitute the C and C++ programming languages.


But why?

The main reason for Java to have evolved is "platform independence" we shall come to this part later.


When a programming language is designed, certain trade offs have to be made.

1) Ease of use versus Power

2) Efficiency versus Safety

3) Rigidity versus Extensibility


This also gives a more or less accurate comparison of C++ versus Java

Java has chosen all the latter parts of the trade offs- in other words, it is Powerful, Safe and highly Extensible.


Does it mean that Java is difficult?

Hardly. Once you get the hang of it, no programming language is more difficult than the other.


Is Java Inefficient.

This is difficult to answer. Immediately it might seem that java is slightly inefficient, especially in the smaller applications. Where C and C++ take seconds to get results, Java seems to be taking ages. But then, to gain something, something else has to be lost. However for scalable applications especially those designed for the web, with good performance tuning, Java can easily displace C++ as an alternative.


What is all this commotion about safety?

Well how would it be if anybody would be able to enter your house and get away with whatever you liked? Eat your favorite apple pie and leave the remains for you to weep about? Thats just the way it is here.

Java is very particular about what belongs to whom and how much permission anybody else has to your property. You, the programmer get to decide and restrict the access of every object.


Why not C++?

Well, C++ is notorious for two of its virally pervasive programming problems.

1) Memory Leak: This occurs when an application dynamically allocates storage and does not bother to free it. This thing is very easily overlooked by programmers and they end up clogging the memory and getting the system to a hangover.


2)Deadly Diamond Deadlock: This is another trauma of object oriented programming. We shall explain this after we have discussed the concepts of object oriented programming.


So what does Java do about these?

Not so fast. Let us just know that java overcomes these, and don't tempt yourself into finding out the answers immediately, allow me to preserve the suspense so that at the time of revelation, it will come as a beautiful surprise.

When the time is ripe, everything will be answered.


Whats so great about Java?

If its edge over C++ hasn't sufficiently impressed you, java has become synonymous with the world wide web. Why? Everybody seems to have a computer but does everyone have the same operating system? of course not, and that is where java comes in with its revolutionary "WRITE ONCE, RUN ANYWHERE" technology, so anyone with a java virtual machine (which is available free of cost from http://www.oracle.com/technetwork/java/index.html and is very small size-wise) can run your module and see it more or less just as you did (didn't we talk about platform independence?)

Anyway, here are a few Buzzwords that java attracts










(you can see what the creators had in mind here http://www.oracle.com/technetwork/java/index.html )

None of these would make much sense without a context and without a problem, so tell you what, let us get on with the language, and as and when the context arises, I shall pose the problem and give you insights on each of these.


And Please, Java is not enhanced html. Html is a markup language- meaning it is just a way of presentation, java on the other hand is a full fledged programming language, with extremely powereful features especially networking capabilities.


In the process of learning you shall really appreciate the power of java.


I have tried to make further reading a fun and enjoyable experience without trading off on the serious side of programming.


This manual is intended as a step by step guide for the basic and advanced users and will build and strengthen the fundamentals of the languae and go one step ahead to unleash the power of the language.


Suggested Books

Java complete reference- Herb Shuldt

Head first java- Sierra & Bates

Java, how to program- Deitel & Deitel

Core Java- sun microsystems press.

Thinking in java- Bruce Eckel


The next tutorial will teach you how to setup the java software development kit and other related topics so that you can be ready to start programming


OpaQue : CHANGED THE FONT : It was hurting..


Chapter 1

Setting up your system for java software development.


Step 1: Get the Java software development kit

Download the latest version of J2SDK (Java 2 Software Development Kit) Version 1.5.0 update 4 (suitable for your operating system) from the website http://www.oracle.com/technetwork/java/index.html or get it from a CD especially those that come free with your computer magazine or with a Java text.

Note: You need to get the Java SDK and not the Java Runtime Environment (JRE). The JRE is bundled within the SDK so don't worry about that.

And another thing. Download the corresponding API specification docs ( in zip format) from the same site. This is an indispensible reference guide to your whole software development. This is avaible only on the sun site and will most probably not be packaged with the J2SDK on any free distribution CD.


Step 2: Installing the SDK and Configuring your PC for the development.

In this course I shall be using the SDK on a windows machine and therefore all the configuring is described for a windows machine only. A linux/mac os user should have enough information to do it himself. In any case, the whole produre is properly described in the Installation notes section of the Readme when you install the SDK.


Firstly run the sdk installation file and ask the program to install the into the folder c:\j2sdk5.0 or d:\j2sdk5.0 or whichever drive you wish to install it. It is better if you install it in the first level itself. For example installing it in c:\xyz\j2sdk5.0 etc will cause certain inconveniences at a later point of time.

Let us assume we have installed it into the c:\j2sdk5.0 directory.

Now look into the directory and you'll see folders like


bin <- this is where the compiler, and other utilities are stored

demo <- you will find some demo goodies here

include <- the native library files

jre <- the runtime environment is here

lib <- the java library files


and files like


readme.htm <- read this for everything you need to know about using the sdk

readme.txt <- a text version of the same

src.zip <- this is where all the source files are kept, or "classes" as they are called in java ( The bricks of a java program.) Unzip the contents to a folder called src in your j2sdk5.0 folder. We will need to take a peek into this sometime later.


and other files.


Another thing you must do is unzip the API specification document's zip file into a folder docs in the j2sdk5.0 folder.


Now that we are done with the installation, we shall configure the pc.


Open the command prompt and type the following EXACTLY

we shall denote the command prompt as $ (omit typing this at the prompt)


$SET PATH=%PATH%;C:\j2sdk5.0\bin;

This tells the OS to look for executables in the directory c:\j2sdk\bin

and now

$SET CLASSPATH=%CLASSPATH%;c:\j2sdk5.0\lib;.;

And this tells the OS to look for classfiles in the lib directory and also the current directory which is "." . This step is called Setting the Classpath and the above, as is obvious is called Setting the path.


Restart the computer or atleast the DOS for these changes to take effect.


Getting yourself some text editors.

During the stage of development I suggest the we use the "EditPlus" text editor which is a good environment to write code, not fanciful, fast and free to use. You can download it from the editplus site. It is suggested not to use Integrated Development Environments (IDEs) like NetBeans or SunOne Studio, they make complex things easy and easy things complex and guzzle on your system resources.

Another text editor is the textpad available at textpad.com

You can if you feel comfortable use the edit utility of DOS, but its a bit too stark with no mouse navigation or GUI.

I shall use the editplus text editor in all the further coding we are going to do.


So now we're done, ready to start coding, but before that we shall get acquainted with concepts of object oriented programming for we will be referring to most of the OOP jargon in the rest of the tutorials.


So tomorrow, an introduction to OOP.


Have a nice day!

Share this post

Link to post
Share on other sites

Chapter 2: Introduction to Object Oriented Programming


There is a famous story about a professor who goes to meet a zen master inquiring about zen.

The zen master welcomes the professor to his house and they sit to have tea. The master starts pouring tea into the professor's tea cup and keeps pouring long after it has filled up. The professor watches it at first with curiousity but the master oblivously keeps pouring and then no longer able to restrain himself the professor speaks out "the cup is overfull. No more will go in" and then the master says "it is the same with your mind. You have filled it with prejudices and preconception, how then is it possible for me to show you zen?"


Similar is the case with OOP. When we learn this style of programming, we must give up all that we had learnt in the procedural languages like C, fortran etc. OOP is a refreshingly new style of programming and requires a whole new way of thinking to be implemented. What this new way is, we shall approach shortly.


Now consider the following scenario.

We have the following people from a community

Jerry- a team leader

Kate- a negotiator, and resource manager

Chirs- a software programmer

Dan- a senior software consultant

Mira- an accountant and steno

Zoe- an automobile engineer

Mike- an electronics expert


Of course there are other people in the community but these are the ones who shall be in our view for the present.

Excepting Zoe and Mike, all the rest make up a software firm that provides high end software solutions (including certain hardware) and of course as you might have guessed, Jerry leads the team.


Now what happens is that an automobile company apporaches Jerry and asks him if they would design and program a chip that would make their latest automobile a versatile and artificially intelligent, robust beast.


Of course Jerry is all too glad and consents. The company then hands over the blueprints of the car and all the design and implementation details.


Jerry gets in touch with Dan and Chris and hands over the details to them. He further asks them to hire the people they will require. He then goes to Kate and asks her to meet up with Chris and Dan and get them what they need at the best price.


Meanwhile Dan and Chris decide to hire (of course) Zoe and Mike (who are their friends) and call them up. The four team up and have a brainstorming session on how to go about with it. With Zoe and Mike's expert advice, Dan gets a good insight into the software and hardware requirements to resolve the problem at hand and suggests them to Chris.


Now Kate comes to Dan and Chris and ask them about their requirements. The two tell Kate about Zoe, Mike and all the stuff they'll need. Kate then goes to Zoe and Mike, negotiates for a good price and hires them. She then fetches the rest of the requirements from the company's regular store.


Now the four intellectuals sit together and in a months time, most of the desiging and planning is done, in another month, Mike gives the specially designed chip to the other three and leaves. His work is complete for now. The rest of them now go deeper into the intricate mapping between the software programming that needs to be done and the actual physical effect it has to have on the car. In a couple of months, the Mapping is ready and it is Zoe's turn to leave. The two are left with all they require to start programming the chip and with expert advices from Dan, Chris does a great job of it using certain detailed implementations of technologies X, Y and Z. In four months, the whole thing is ready. Now Chris and Dan sit together and make a detailed report of their product. They then call Mike and Zoe to help them complete the documentation especially things like why they chose the design/technologies they chose and about how the chip must be integrated with the automobile. And one fine day, its all ready.


The whole thing is then rechecked and submitted to Jerry. Jerry then asks Kate to sit with Mira and get the whole accounts updated and get him the final bill statements. Kate does this and gives Jerry the final bill. Jerry then goes through the thing, adds in some things like development charges, consultation fees, and all those things that will actually make up for their salary.


He then goes to the automobile company with the programmed chip and the documentation (which includes the manual) and submits them along with the bill. The engineers go through the whole thing, approve it and Jerry is paid. What goes on after that is more or less inconsequential to our interests here.


Ok, thats the whole thing. Now I'll ask you a few questions

1. When Jerry gives the blueprints to Chris and Dan, what does he ask them to do?

He asks them to design the required chip and hire the people they wanted right? Now tell me did he know what the final thing was going to be like? did he have any idea about what the guys were gonna do and give him? nopes, he just knew that all he was going to get was the "REQUIRED CHIP" and he didn't know what kind of people he was going to hire either. He just asked them to hire "REQUIRED PEOPLE"

Look at that! the leader doesnt know these things, but he still gets the work done.

What Jerry has done is in essence ABSTRACTION and things like "required chip" and "required people" are abstract items or quantities, for without specific information, they do not exist physically.

For example- there is no chip called required chip until I specify the brand and processor speed or put down all the configurations or make it so specific that there is no amibguity about what I want.


The main thing to remember about abstraction is, it gives a definition to what we are looking for- like a "dream ship" or a "perfect girl" but it never bothers to be specific because there are many things that would fit into what you are looking for and many of those would satisfy you. In the above scenario, there could have been several other chips that could suit the purpose, other than what Chris and Dan designed.


In fact the main PURPOSE of abstraction is to carve out a space within which we shall go about finding solutions for the problem at hand.


Let us take a deeper look into this

Alan feels something is missing in his life and wants to find the solution, so he meditates upon it and realises that he needs "PEACE OF MIND" <-there we got our abstract quantity

So basically, Alan figures that the problem has a solution in "PEACE OF MIND"

He then writes on a piece of paper.










Then he writes










and so for the others

Can you see what is happening? Starting from an abstract quantity, Alan is moving to a CONCRETE solution to his problems.

After two to three levels further going down, Alan catches the points in his life where things were not going according to his definition of PEACE OF MIND, and could immediately correct it.


And that is the major use of abstraction. It makes solutions come to us with ease, and more importantly in a well-defined and structured manner.


And remember you can only ASK for an ABSTRACT QTY. and NEVER CREATE it. This principle we shall be using lots of times later, so keep this in mind.


Now lets get back to the first scenario.

Did Jerry KNOW how the thing was going to be done and that Chris and Dan had hired Mike and Zoe? Of course not, why would Chris and Dan bother Jerry (who's basically a management guy) about all the software, electronic and automobile engineering jargon? Jerry didn't need to know (period) Neither do the automobile company guys. And Zoe and Mike know nothing about how Chris and Dan programmed the chip.


That is called ENCAPSULATION- or in other words HIDING DATA & IMPLEMENTATION. But why? Firstly to keep things as independent as possible and secondly to avoid unnecessary butting in. In more software like words, to preserve reusability (by making it independent) and avoid data corruption and prevent illegal actions.


Consider tha fan regulator in your room. It is actually much more than that sleek knob that you see, instead it is a pretty complex circuit within and you get to see none of it. It exposes only two interfaces (here interface just means something that can interact) for manipulation. The two wires behind the board for power input and the knob for regulating output. Thats it. You don't need anything more to use it. This is a classic example of encapsulation. Imagine if the whole regulator were open, any tom would be able to tamper with it and maybe get you an electric shock or short circuit the whole power supply! Meditate on this, and you'll know why you need encapsulation.


I'm reminded of an incident in my life when I had to submit a presentation at school and I was taken ill. So I had asked my dad to do it for me just so that I could escape the fury of the teachers. And my dad was kinda busy but he said he'd do it. And sometimes I'd get delirious about whether the thing got completed or not. And one day when I caught my dad, I asked him how far is it complete? and he twitched his lips and said "Why do you worry? I've told you I'll do it and I'll do it, how I do it, and when, just don't bother about these." And I set my mind at rest.and sure enough, the thing was ready on time. Another example of encapsulation, and another use- it prevents unnecessary worry, just trust things to know how to take care of themselves.


The above two concepts were explained mainly as an introduction, the real thing in java, we shall when we get down to writing code.


Two other concepts that are part of OOP are Inheritance and Polymorphism.


These are very easy concepts to understand, the names themselves speak a good deal. We shall suffice ourselves with just an general understanding of the two, the more detailed and precise definitions we shall give later.


Isn't there something common between your dad and you? or your mom and you? if yes, then you probably Inherited those features/qualities from your parent.


Consider a Shape.

A Rectangle is a Shape

So is a Circle


Let us draw an abstract to less abstract chart of Shape


SHAPE (something with a boundary)















Can you see whats going on? A square is a rectangle and also a quadrilateral and also a polygon and also a shape. Any figure down the hierarchy is a more concrete shape- more importantly, they are all shapes. And since they are all shapes, everything that a shape has, they also have. Since a shape can be filled with color, so can any of polygon, circle etc. In other words, since these things are DERIVED from the basic BASE called SHAPE, they have ALL the properties of a shape. This is essentially INHERITANCE.


INHERITANCE is when less abstract classes (called the DERIVED CLASS) of a BASE class DERIVE ALL the properties and functionality of the Base class


A shape can also be rotated, therefore so can a rhombus or a triangle. But when we want to rotate a shape, we will have to do it depending on what shape it is exactly right? A circle is rotated about the axis and a rhombus around the diagonal. BUT, the BASIC OPERATION is ROTATE, and rotate is what we shall do. But again, it DEPENDS on what exact shape it is. Therefore a circle has a different WAY to ROTATE and so does a rhombus.


When different derived classes behave differntly when the same operation is performed, it is called POLYMORPHISM- i.e it is morphing (changing) in many ways (poly).


We shall stop with a funny kind of polymorphism but this is just for fun, the accurate version is the one above.


Every morning I go out for a walk and it happens that I come across quite a few people I know. What happens when you come across people you know? they probably greet you- now this is the way each of them greet me.


Uncle- hello sonny, how're you doing

Girlfriend- Heyyy (with a lil honey in her voice, and a wink) whats up dude

Friend- whats up buddy, whats your plan for today?

Close friend- Whats up *#$%& ( with a whack on the back - no offence intended)

Angry Classmate- (venom in his eyes) grrrr

The Colony Grouch- -sNoRt-


Well, they're all people, but because they're all different, they do the same thing (greet) in different ways. Thats polymorphism too!


Thats all for today,

Tomorrow, Thinking in Objects and Java language basics.


Have a great day, and watch that softnote in your voice when you meet that special person :D polymorphic man.

Share this post

Link to post
Share on other sites

A couple of additional words:


1. JVM (Java Virtual Machine) hides OS & Hardware differences: compile once, run anywhere!!! ;)

It means, java byte-codes can be executed on any operational system with preinstalled JVM!


First java programs had problems with performance (they worked slowly), that's why developers used sometimes to compile java source code by so-named JIT compilers, that allow to turn java code into platform-dependable executable files (.exe etc.). Today this practice is used very rarely comparing to byte-code compilation, because last versions of JVM are optimized well for all popular OS, including Windows and Linux. Java programs have performance comparable to many C++ programs.


2. Java language was created in 1991, having experience with C++. Many operators look like advanced and simplified C++ operators. That's way Java is more suitable for rapid software development.

Java does not contain pointers and virtual functions, but you can implement the same functionality using Java interfaces.

On another hand, missing pointers and virtual functions makes java more secure then C or C++.

Also, security is effectively controlled by Java class loader and bytecode verificator.

Only one security problem: byte-code is NOT protected protected against decompilation. So, source code of classes can be easy restored from byte-code. To resolve this problem, some advanced techniques (like obfuscation and bytecode encryption) were developed.

Edited by java-area (see edit history)

Share this post

Link to post
Share on other sites

Java database

A Complete Java Tutorial


How can I create a GUI window that let me connect and interact with my database in MS Access 2003?


-reply by Silas

Share this post

Link to post
Share on other sites
java programmingA Complete Java Tutorial

Write a programe that does the following mathematical operations on the complex numbers [55]


a) Addition


B) Subtraction


c) Multiplication


d) division


-reply by Relebohile

Share this post

Link to post
Share on other sites

Another interesting fact is that Java applications usually have a much larger memory footprint compared to c++ applications and when it comes to raw processing power, it's still slower as c++ (however, as mentioned earlier, it is also safer compared to c++).It's also true that java applications can run on every system that has a JRE, but that's not always true for java libraries. Accessing your webcam for example can't be done with a single library, you'll need one for every operating system.@iGuest1: well, there are enough tutorials online that explain how to write a java gui for manipulating databases. No doubt that there's on available for access databases (tough I think it's a lot more easy to just write an Access form to do this).@iGuest2: it can't be that hard to figure this out by yourself. Create a struct or class that contains the real and complex part of your number, then write the functions. It's probably not more than 20 rules of Java code :)

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.