Java


Java - The future?

When Java was released back in 1995, it was the most hyped thing in the computer world - everybody talking about this cross-platform language, being able to run on virtually any platform. Some1 may claim, that the Java actually is not platform independent, since it runs in a Java Virtual Machine (a non-existing computer), the JVM. Java was originally developed as a way to program embedded microprocessors (stuff like washing machines and cars), but when it was released on the Internet, its cross-platform ability immediately made it popular among those, who wanted to have more interaction on their homepage. This was supported by the leader of the browsers (at the time), Netscape.

With the pre-programmed classes from JavaSoft, a sub-division of Sun responsible for Java development, it is easy to create advanced software rapidly in Java.

IMHO Java is a great language, relatively easy to learn and use and has a wide foundation today - and most important - IT'S (Microsoft) FREE!


Java vs. C++

The Java language is mostly based upon C++ and contains a few new ideas. The first thing you will learn about Java is the fact it contains no functions or procedures, as older languages do, it works solely on objects and methods in objects (this means, that even the entry method has to be placed in an object). The basic syntax reminds to a high degree of C++, probably to make it easier to translate programs from C++ to Java. Unlike C++, the methods has been moved inside the class declaration, making it easier to keep track of the parameters for the functions and one only has to change it in one place. In Java, the concept of inline methods is not publicly visible (I do not know if the Java compiler compiles inline methods within the class). Java compiles each class into its own file, a .class file.

As you probably noticed in my C++ statement, I disliked the switch statement - to my great regrets, it survived Java. Why? To remain C++ compatible, then why move all code into the class declaration.

One major thing, the C++ programmer may/may not like is the garbage collector - why waste time deallocating memory when one could just move that into a background process and forget about it...

Another thing - since all objects are pointers, why bother to write "->" instead of ".", so in Java, only "." is used.

Threads and synchronizing are build straight into the foundation of Java (all objects inherit from java.lang.Object), and the Sun JDK is completely prepared for multithreading.


A short Java course

Lets jump straight into the fun stuff...a sample piece of code (Java IS case-sensitive):

class Secret
{
    public Secret()
    {
    }


    private int mySecretVariable = 0;
    

    public int getSecret()
    {
        return mySecretVariable;
    }
    

    public void setSecret( int newSecret )
    {
        mySecretVariable = newSecret;
    }
}

As you can see from the above example, all classes are surrounded by the class <ident> { } statement. Sun uses a name for each class, that starts with a capital letter. Lets take a look at whats inside the class definition. The class definition contains a list of fields (variables) and methods (functions).

public Secret()
The first thing in our code is the so-called constructor, whose responsibility it is to create an instance of the class, we just defined. An instance is an area in memory, where the data for each object (or instance of the class) is stored. Think of it as a brand new object ready to do all the things, you described in the class definition (between the outer { }). Notice the name is equal to the class name - this is important.

private int mySecretVariable = 0;
Inside this statements, fields (variables) and methods are listed with access modifiers (how visible is the field/method from the outside). In this simple class, we define a private field mySecretVariable of type int (integer) and sets it value to 0. Since the field is private, it cannot be seen from outside the class.
Syntax: <access><type><identifier> [ = <value> ];

public int getSecret()
This declares a function getSecret, that returns an integer. Since the method is declared public, it can be seen outside the class. If we read what is inside the braces, we see, that it returns the value of mySecretVariable to whoever called this method.
Syntax: <access> <return type> <name>(<list of parameters>) { <code> }

public void setSecret( int newSecret )
This is just another method, but unlike the first, it has a parameter. This means that the method requires you to give it some more information, here it needs an integer. One could use an entire list seperated by "," to use more than one parameter. As we read the code, we see, that mySecretVariable is set to contain the newSecret.


Types in Java

This list contains the Java types and their C equivalents (on a 32-bit platform):

The experienced programmer may notice that all Java types are signed, and very much like the C/C++ types.


Access modifiers in Java





If anybody liked this very short intro, please write to me and more will follow on this page...



Back...





1One of these are the Danish author of C++, Bjarne Stroustrup, who in Computer World no. 35A from sept. 29. 1998 makes the following claim: "Java er ren marketing." ["Java is pure marketing"] and later "Java er ikke uafhængig af platformen. Det er bare en ny platform," ["Java is not platform-independent, its just a new platform"].

2Dietel & Dietel: Java - how to program, 2nd edition: Prentice Hall 1998: pg.103, fig.2.18.





Last updated 2001-08-22 FlushedSector