Java Programming/Getting started
From Wikibooks, the open-content textbooks collection
Programming in any computer language is an art – one that requires the programmer to break problems down into smaller chunks to better understand the whole. Like mathematics or any other discipline that addresses the solution to problems, computer programming can only be improved upon by practice, more practice, and then some more.
By now, you’d have already used a computer in some way or the other. Be it to watch a film, surf the internet for e-mail or social networking or writing a word processing document. If so, then you probably have the right amount of experience to delve into the world of Java programming. The only things you would most likely need are: a computer, an internet connection (in the first part only) and a sound mind.
If you have these at your disposal, we can formally begin by explaining a few basics. In this section, we will try our best to examine the world we would be working in as a programmer.
Contents |
[edit] A programmer’s world
As we said earlier, everyone has once used a computer to do something. Anything we do on a computer is facilitated by a certain program. When you watch a film, the program you use is called the media player. When you surf or browse the internet, the program you use is the browser. When you write that important homework sheet, a thesis or a novel, the program you use is a word processor.
What do all of these programs have in common? Well, apart from the fact that they sit on your computer, these programs have a special purpose and they fulfil that purpose by working on various instructions given to them: a word processor, for instance, works on your text inputs and a media player plays your media files, be it video or audio. Consider this example: you are tired from a day’s work and you want to listen to your favourite song once you are sitting near your computer. You press play once the song is selected and the song then plays. To a programmer, this scenario can be written down into tiny chunks of instructions called code.
Listing 1.1: A few lines in a Java program.
if(playButton.isPressed()) playSong(); else doNothing();
If you translate the above code into plain English, it would actually make sense.
If the play button is pressed, then play the song. Otherwise (if the play button is not pressed) do nothing.
This is just a part of what a media player has to do. Add a few million more lines of code and you see a complete program. Don’t worry! Once you start programming, a million sounds pretty small.
If we wish you’d learnt one thing from the previous example, that would be the code written is but just a very small part of the whole program and nothing fancy. This tells something about programmers. Programmers have this keen sense of outlook. They can break down various problem into simple steps and can write code to define instructions on how to carry on with those steps.
To write these steps, we use a programming language, in this case the Java programming language.
[edit] Few words about choice
Even though the purpose of this book is to explore the Java programming language, it is best to show how other programming languages do the same. Programming languages aren’t that much different from each other. If you know how to program in one language, you’d definitely know how to program in another. The program as listed in Listing 1.1 can be illustrated as such in Visual Basic:
Listing 1.2: A few lines in a Visual Basic program.
If PlayButton.IsPressed Then PlaySong Else DoNothing End If
Although the code in Listing 1.2 looks and behaves the same as in Listing 1.1, it seems more like the English language. It is just a matter of preference as to what language is used to build a program. The same code in PHP would look like this:
Listing 1.3: A few lines in a PHP program.
if($playButton->isPressed()) playSong(); else doNothing();
[edit] Computers do not understand code
Yes, that’s absolutely right! Try looking at the code we provided you with. You can understand the code because it resembles English to some extent. But computers do not understand English. The only language that a computer can speak is of 1s and 0s. Quite literally, a computer can only understand these two digits. For this very purpose, your code needs to be further translated for a computer to understand.
Take for instance this example. You are stranded on an island off the Pacific shores. A yet undiscovered island. The natives seem happy to see you there. They are preparing food for you. All is well until you realize that what you are sitting in is not a jacuzzi. You’d wish you had a translator who could translate the ritualistic singing of the tribesmen.
Fear not, because for this very purpose, programming languages uses a special program called a compiler. You have to pass your code (the source code) through a compiler and voila, out comes a special version of your code that is only understood by a computer. This version of your code is called byte code or machine code (the differences are explained just below).
In languages like C or C++, the source code is compiled into low-level machine code and is usually optimised for the machine it has been built upon. So, for instance, if some code was built on say the Windows OS, it would only target that exact operating system. In Java however, this is not the case. Java source code is compiled into an intermediary state called the byte-code. Now the byte-code is not something that the computer architecture can understand purely as it not the native code, and neither is it something humans can understand. It is something intermediate and does not target any one computer architecture (Windows, Macintosh, etcetera). This is where the Java virtual machine (JVM) comes into the picture. Every computer architecture has its own JVM, a JVM for Windows, another for Macintosh, and so on. When the JVM reads the byte-code, it interprets the code into the native code of the operating system it serves. In essence, you don't have to produce code for every single computer architecture and just write code once and be assured that it would run anywhere. The marketing people at Sun Microsystems call this Write Once, Run Anywhere™ (written in short as WORA).
[edit] What you need
Now that we are familiar with the basic workings of the Java programming language, we must proceed with the next step: getting the tools we need to properly program in Java. Henceforth, you would need the following:
- A text editor: Java files are plain text files and any text editor would do. Any text editor that helps you create files with text (not necessarily formatted; you don't need formatting for Java source code) would do. Notepad can be a very good option. A freeware program called Notepad++ is also a very good tool as it will help color code and better organize the code you write for you.
- Java compiler: You would need a Java compiler to compile your source code into byte-code for you. See the section entitled, "Installation" for more on that.
- Java virtual machine: You need a Java virtual machine to interpret the byte-code for your computer's native architecture. See the section entitled, "Installation" for more on that.