Java Tutorial: Introduction

java
First of all, I’m going to write a couple of tutorials on programming in Java over the summer, starting with this one. So lets skip all formalities and whatnot’s and begin.

Java is an object-oriented programming language. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities. But don’t worry, even if you don’t know any of these two programming languages, Java is easy to learn as a first-time programming language, and as such is being taught at schools around the world.

HelloWorld.java

/**
 * HelloWorld application
 * Outputs Hello Unyttig.INFO and the rest of the WORLD!
 * @author www.unyttig.info
 */
public class HelloWorld {
    public static void main(String[] args) {
        // Display string
        System.out.println("Hello Unyttig.INFO and the rest of the WORLD!");
    }
}

The HelloWorld application consists of three components.

  1. Source code comments
  2. Class definition
  3. Main method
  1. Source code comments
    Source code comments are only meant for documentation purposes, and are a way for the programmer to keep track of what the different classes/methods/objects etc do and what they return (if they do that.. ). As a result of using comments the code is easier to understand. They are not compiled by the compiler. There are three kinds of comments: line comments, block comments and JavaDoc comments. Some of you might recognize these from other programming languages.

    Line comments
    Adding a one line comment is fairly simple, and can be used for small and simple comments. The comment starts with two forward slashes “//” and ends at the end of the line the slashes occur on.

    // Example
    // This is a one line comment
    // This is another one line comment

    Block comment
    When in need of a comment that’s so long it expands over multiple lines, its easier to use block comment instead of multiple single line comments. A block comment starts with a single forward slash and asterisk “/*” and ends when the reversed combination “*/” is given. So anything in between of these two are considered as a comment.

    /* this is a multiple
       line comment
       properly called
       block comment
    */

    JavaDoc comment
    When we want to document non-private methods, attributes and classes, we use JavaDoc comments. These comments are essentially similar to block comments, but are read when generating JavaDoc. A JavaDoc comment can also contain HTML to be used in the documentation. The JavaDoc comment starts with a single forward slash, and two asterisks “/**”, and it ends with a single asterisk and a single forward slash “*/” and everything in between of these are considered as a comment.

    /**
     * Java tutorial: Introduction
     * JavaDoc comment
     * @author www.unyttig.info
     */
  2. Class definition
    To define a class in Java, we use the keyword “class”. After the keyword comes the name of the class (HelloWorld, CarObject, PersonellPayments etc). The code between the curly brackets are the actual code body of the class. { code here }

    public class HelloWorld {
        // code here
    }
  3. Main method
    So you have your application ready, you have tens of classes and methods. How will the compiler know where to begin and such? The answer is simple, the class with the main method. Without a main method, the application doesn’t work (if you have experience with C or C++ its similar to the main function). An application cannot contain more than one main method, and the main method has to be defined in a public class. The default main method looks something like this:

    public static void main(String[] args) {
        // TODO Auto-generated method stub
    }

    The main method accepts an array of elements String, which are specified through the command line. This can be helpful when needing to present the class with a filename and such. Here’s a quick and simple example of how you can provide command line arguments to your class.

    // In your commandline type: java MyClass FirstArg SecondArg
    public class MyClass {
        public static void main(String[] args) {
            for(int i = 0; i < args.length; i++)
                System.out.println("The " + args[i] + " given");
        }
    }

    What this script basically does is it loops through the args array and prints out the String in each row, seeing’s its two arguments given in this example, the args array contains 2 arguments. And the arrays in Java starts at 0 (not 1!), but we’ll get back to for loops later. The output of this example is:
    The FirstArg given
    The SecondArg given

End of introduction
Okay that's all for this time, next time I'll be talking about java installation, choosing a Java editor, how you actually compile and run your class files, some Java basics and introduction to different kinds of primitive (strongly-typed) datatypes.

About Torstein Skulbru

23 year old, Bachelor ICT(Information and Communication Technology) student at University of Bergen (NO)