I’ve been thinking about this for a long time. Now i decided to put it to the test and see if anyone finds it useful or not. This series will actually be a Java tutorial, from the basics to the more advanced, from ground up. You can call it a “for dummies” tutorial. These tutorials will be written in the most simple way with nothing taken for granted. We will start from the very basics and advance one step at a time. The goal is to help anyone eager to learn Java, on the process of doing so. Of course, this series, will not, for no reason, substitute a good book. This is a way to learn Java through, ahm, a crash course. This is going to be a long ride so, bare with me people!

Starting from the very beginning. Java is a programming language, hardly 13-14, years old. It started back in ’95 (there was a previous version called Oak because of a tree outside Gosling’s office) mainly from James Gosling. The evolution was, and still is, spectacular. In these years Java has managed to penetrate many aspects of programming and making her self available for many programming challenges.

Let’s dig a little deeper. Programming languages can be separated by several criteria, one being if they are interpreted or compiled. An excellent example of a compiled language is C. What happens there is that the programmers code goes through a process that checks the syntax and semantical correctness and creates an object file ready to be executed by a specific system. For instance, if we compile a C program with a compiler for windows then the object file created (the .exe file) will be executable by a windows machine on a X86 architecture only. Same goes for every other platform.

When the developer wants to write a program he has to consider the platform that it will run on. This is not bad per ce, for instance if you want to write a driver, but if you want to write a simple address book that can be run by anybody then things get tight enough. Java implements a very mouthful moto “Write once, run everywhere“.

On the other hand, a very well known interpreted language is HTML. When a developer writes an HTML file, he saves it and then tries to “run” it with his browser. The browser then loads the file and executes the code while reading it (interprets it).

Java is somewhere in between. Running between your system and the program is, what is called, the Java Virtual Machine (JVM). This offers a transparent “system” that is universal for all java platforms. Your program goes into that “system” and that is responsible on translating your code to the specific code of the machine running the JVM. But the language is not interpreted because the program that goes into the JVM is not your code. It’s a result of the compilation of your code. But why is it then not called a compiled language? Well the product of the compiler is an intermediate executable code for the JVM that is then interpreted by it to the native system’s code. The image illustrates the steps that the code goes through.

One more characteristic of the Java programming language is that it is an Object Oriented language. This means that everything in your code is an Object. You need to make two things clear before you go on with these tutorials. Objects and Instances are two different things. Let’s see what they are by example.

Let’s say you are walking on a nice Hawaiian beach and stumble uppon a Coconut. You take it in your hands, examine it, and see that it’s colour is brown, it’s size is big and the hardness is medium. You keep walking and then you stumble upon another one. It’s colour is brownish, it’s size is medium and it is very hard. Now in this example a Coconut is an object and the two coconuts you examined are the instances. Moreover, the colour, size and hardness are the attributes.

One more example. When you play need for speed, you have cars. One car’s brand is ford focus, it’s yellow and it can reach 300Km/h. Another car is Porche, it’s red and the top speed is 350Km/h. Now in this example Car is an Object, the two cars are instances and brand, colour and top speed are attributes. Hope this makes it clear.

Now let’s proceed to writing some code and see it in action. In order to write a Java program you need the Java Developer Kit (JDK). You can download it freely here. Once you install it you need to add the folder containing the binaries of java to your path. To do that in windows right click on “My Computer”, go to properties. Then go to “Advanced” and then to the “Environment Variables”. You should see something like that:

Go to “New” and then on the variable name type “PATH”. On the content type “%PATH%;C:\Program Files\Java\jdk1.6.0\bin” (or wherever you installed the JDK). Submit your changes. To make sure it works fine open a comand line prompt (Start -> Run -> CMD) and type “java” and “javac”. If you get “‘javac’ is not recognized as an internal or external command,operable program or batch file.” then something definitely went wrong. If everything is ok you should get the command line arguments that are availiable. If you made it through this step then you are ready to write your first Java program!

So, all you need is the simple Notepad (Start -> Run -> notepad). Now here is the “Hello world” in Java:

public class Hello {
    public static void main(String args[]){
        System.out.println("Hello world!");
    }
}

Now save this file as “Hello.java”. After that open a CMD and navigate to the folder you save it and type “javac Hello.java“. Then, after the compilation is complete type “java Hello”. Voila!

Now i want to make a few pointers here.

  1. Java is a case sensitive language, which means “String” is way different than “string” (so watch it when you are typing).
  2. Java programmers mostly use the camel notation. That means that if we have a class of two words, they will be concatenated to one, with the second word’s first letter capital. For instance “HelloWorld”. If it is a variable, method etc then the first letter will be lower case, “helloWorld”.
  3. Every public class, on the above example, the class “Hello”, should be stored on a file without any other public classes and the file name should be the same with the class name. Thus, we saved the file as “Hello.java” and not any other fancy name (about public and other modifiers we will see them in detail on another tutorial).
  4. Now, for the first tutorials “public static void main(String args[])” should be taken for granted. We will explain later what it means and how it works. For now you need to know that the JVM invokes this method to start your program.

If you made it through here then you have a great deal of patience and secondly you know how to write your first Java program. On the next tutorial we will try to explain scope and a few more basics so you will start to understand better the language.

For now i need you to leave me a comment on how you liked the first tutorial (honest!) and if you are interested in seeing more on the series. Moreover, if you bump into any problem i’d be more than happy to help you out. Just contact me with any problems.