Tuesday, June 10, 2008

The basics of Java

The list of guides and tutorials describing the basic features of Java is endless (not literally of course). Instead of making yet another complete tutorial covering the basics of Java, this lecture will only briefly introduce the basics of Java. If you are a somewhat experienced programmer this short introduction might very well be all you are looking for. If not, that is, you are new to programming in general, it is recommended that you read one of the more careful tutorials available online covering the basics of Java, for instance Suns tutorials.

Assumptions

In the following I will assume your knowledge about common programming terms, for instance like pointers, variables, methods, constructors, classes and objects, and their attributes. How declaration of these are done in Java will on the other hand be shown.

Installing an IDE
An integrated development environment (IDE) is, simply put, a software combining the process of editing, compiling and executing. In an IDE the editor is typically a graphical text editor with spell checking, syntax highlighting and code folding, and the IDE makes compiling and executing the application effortless.

For beginners not known to the basics of compiling and execution it is not recommended to use an IDE, but instead only download the Java SE Development Kit (JDK) from here. The JDK includes Java Runtime Environment (JRE), which is required to run Java applications. For other users it is recommended to download the JDK bundled with NetBeans IDE from the same site. Alternative IDEs for Java is Eclipse and JCreator. It is interesting to note that both NetBeans and Eclipse is mainly written in Java.

Installing the JDK or the IDE is like installing any other application, so I trust you are able to do this without any guidance.

If you have decided to install only the JDK you need to add "installation/location/bin" to your global path and, if it is not already present, add "." to your class-path (the "." will make a Java-class able to use other classes in the same directory). The coding itself is the same as if you had installed an IDE, only you will be writing in a simple text editor and should save the code file as plain text, for instance as a text file named "MySource.java". Compiling a Java source code is done by writing "javac MySource.java" in a command line prompt positioned in the same directory as your saved source code, and execution can then be performed by writing "java MySource".

Creating the your first Java application
If you installed the NetBeans IDE you can create a new project by choosing "New Project..." in the "File"-menu. Select "Java" in field below "Categories:" and "Java Application" below "Projects:". Proceed to the next step by selecting "Next >". Here write the name of the project. By default NetBeans will create one package, a collection of classes, by the name of the project. The main class, the class that will be executed by the runtime environment, is by default simply named "Main". Normally this is not anything to fuss about, so simply click the "Finish"-button after typing the name of the project.

NetBeans will now display the initial class named "Main" (or whatever you decided to name your main class). The code between "/*" and "*/" are comments to the programmer. The code between "/**" and "*/" are also comments, but in addition these comments will be included in the automatically generated Java documentation if it is present just prior to protected or public classes, methods or object/class variables. If only the rest of a line is to be commented, one simply writes "//", these comments will not be included in the documentation. Viewing the code in light of these facts, there are only five lines of code:

1   package javaapplication1;
2 public class Main {
3 public static void main(String[] args) {
4 }
5 }

The first line simply states that this class is a part of a package, here "javaapplication1". The second line creates a class, here named "Main", by the statement "class Main", and that this class is set to be visible to the entire system, that is, "public". Line three creates a method named "main". This will be the entry point of your application, that is, the runtime environment will always provide control to this method in your main class. It should be declared "public", meaning that it is visible to the system, "static", that is, being a method bound to the class itself, and "void", meaning that it does not return anything. The parameter "String[] args" states that the invoker should provide a (possibly empty) list of multiple elements, called an array, of the type "String", that is a standard text object of possibly multiple characters. The elements of this array will be the arguments the executor provides, if you are executing the program from a command line prompt, the elements is the text written after "java MySource", for instance "java MySource argument1 argument2" will result in a list of two elements. The list can be accessed by the variable "args", which is a pointer. Line four and five indicates the end of the "main"-method and the "Main"-class, respectively.

Lets make the application display "Hello world!". This is simply done by adding the following code between line three and four above:
3.1           System.out.println("Hello world!");

"System" is here a class, like "Main", "out" is a class (static) variable representing a "PrintStream"-object, while "println" is a method in the class "PrintStream".

You can now execute the application in NetBeans by pressing "F6" or by choosing "Run Main Project" from the "Run"-menu. In the output field "Hello world!" should be displayed along with some information about the compiling and execution.

Essential notes about Java-codes
In Java all code must be placed inside classes. The variables declared at the root of a class is unique for each instance of the class if it is not declared "static", then it is bound to the class and common for all its instances.

Inside a class, a static method may only invoke another static method (or itself). This is because the static method does not belong to a object, but to the class itself, so it would not have known in which object the non-static method should be invoked. On the other hand, non-static methods can invoke both static and non-static methods.

To access members of a different class, one needs to inform uniquely the environment in which the member should be accessed. To access a static variable "var" in the class "MyClass", one simply writes "MyClass.var". In order to access a non-static variable "var2" in the same class, one needs to inform in which class instance, that is, in which object, the variable should be accessed. A object pointer to the class is then used, name it for instance "obj", and access is then granted by writing "obj.var2". This object pointer can be declared as following
MyClass obj = new MyClass(...)

where "..." indicates the arguments to the constructor of the class.

The same applies to the methods. To invoke a static method "myMethod()" in "MyClass" one simply writes "MyClass.myMethod()". The non-static method "anotherMethod()" can be invoked by the use of an object pointer "obj" of the class, and write "obj.anotherMethod()". Arguments to the method is placed within the brackets. Multiple methods can be declared with the same name as long as the parameters differ, in number or type. Note that two methods can not both have the same parameters, thought they return different types.

Both variables declared at the root of a class and methods (who must be declared at the root of a class) have access level modifiers. These modifiers determine whether other classes can use these variables or invoke these methods. There are four types of modifiers; "public", "protected", "package" and "private". If declared public "public" it is visible to all classes (as long as the system know where to look for the class). On the other end "private" indicates that only the class itself may access this member. "package", a access level obtained by not using any modifier, states that all classes inside this package should be able to access this member, while "protected" also includes any subclass of this class, even if it is not inside the same package.

Classes may be declared without a modifier or with the modifier "public". The effect of these options are the same as for variables and methods as described above.

Inside a method one may declare new variables. These can not have access level modifiers and are only visible before the closuring bracket corresponding to the first previous starting bracket. The interval a variable is visible is called the scope of the variable. If a variable of the same name is already declared, the use of this name will refer to the first previously declared variable of that name, that is, the variable latest declared.

If you were able to keep up with this introduction you should be able to start programming in Java, thought no-one expects you to be an expert at the time, if you have a half-shaky grip of the syntax, you are probably only a couple of hours of test-programming away from being able to code most applications with the use of the Suns application program interface (API), provided that you know an algorithm for each challenge in question. If you feel not smart at all on Java, it is recommendable that you read a more careful introduction tutorial to Java before proceeding to the next lectures. These lectures will be more advanced and not explain any basics of programming in general, but only the subjects in question, which can be program-technical, like multi-threading, or implementation of efficient algorithms for solving specific problems.

4 comments:

Ze Brosowic said...

"The list of guides and tutorials describing the basic features of Java is endless (not literally of course)."

That's the proof right there. You are a pedantic man. And being such, you think everyone else is.
So, you take care not to paint your pretty little behind into a corner.
Wouldn't want an argument about the endlessnes of the list of guides and tutorials describing the basic features of Java...

I got carried away writing english.. Don't take me to seriously:D

Andreas said...

It was ment as a minor joke. I don't believe I'm pedantic, but I might have a strange sort of humor, thought I believe I'm not the only one :)

Ze Brosowic said...

He he, I'm just saying you covered yourself, in case someone (like me) would say "Hi! the list isn't endless!".

I just thought it funny, that's all:P

Andreas said...

Nice to see another with the same good humor as me :) Somethings remain intact.