The Technology Vault
Incorporating GeForce FAQ
Please welcome...

Our Latest team member...

thecaretaker

He'll be keeping you up to date with all the latest news and developments from the world of technology!

Where to buy...
Forums
PC broke? Need help building a web site? Want to know what the best spec PC is for your budget? Join our on-line community for all the answers! Click here
What happened...
 ..to [.tv]? They went to the big channel in the Sky (Excuse the pun ;))
Great deals!

Introduction to Java Programming
Online Tutorials - Java by Forum member "turnitonagain"
©Technology Vault.

The Basics

Welcome to the second part of this Java tutorial! If you recall last time we wrote and compiled the "Hello" program as follows:

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

The first thing we will do is have a look at some of the bits of this program - basic as it is; it contains some basics properties that all Java programs have.

The first line: class Hello is an indicator to Java that we are starting a new class called Hello. Its no coincidence that we also named the file Hello.java - the file name must always be the same as the class name. As I mentioned before the case of the lettering also matters, for instance, hello.java is different from Hello.java in the same way that class Hello is different from class hello. Try changing the first line of the above program to read:

class hello {

and try to compile and run it. You should find that it compiles ok, but you get a big error like:

C:\javaprogs>javac Hello.java <- Compiles ok

C:\javaprogs>java Hello <- Big error
Exception in thread "main" java.lang.NoClassDefFoundError: Hello (wrong name: hello)
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:502)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:12
3)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:250)
at java.net.URLClassLoader.access$100(URLClassLoader.java:54)
at java.net.URLClassLoader$1.run(URLClassLoader.java:193)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:186)
at java.lang.ClassLoader.loadClass(ClassLoader.java:299)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:265)
at java.lang.ClassLoader.loadClass(ClassLoader.java:255)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:315)


Most of this error is garbage, but if you look at the first line is says "Wrong name: hello", this is because it should have a capital 'H'. Put it back as before and recompile it and make sure it works again.

The first thing after the Hello in the program is an open curly bracket '{'. This curly bracket matches up with the last curly bracket at the end of the program and Java reads this as being "Every thing between these two brackets is the class Hello". These curly brackets are used to enclose bits of code (also called blocks) and feature quite a lot in Java programs. It might also be good to mention the fact that the '{' can also go on the next line on it's own and doesn't have to be on the same line as the class definition - it's really a matter of taste.

The next bit of the program is the main method. Methods are bits of code that perform a specified task or tasks (sometimes called 'procedures' too). Every executable Java program will have a main method and is the first thing Java runs when the program starts. We will look in detail at the first line of the main method after we have discussed methods a bit more but suffice to say that it always looks the same. Notice the curly brackets again, denoting the start and end of the main method.

Now down to the actual line that does the work…

System.out.println("Hellooooooo!!");

This uses the Java method System.out.println() to output data to the screen. This is achieved by placing a String between the brackets (Strings being denoted by double quotes). Missing out the quotes will generate an error at compile time. Note that this line ends with a semi-colon because it is a line of code in it's own right. It will become obvious over the next few examples which lines should get semi-colon's and which ones shouldn't.

Comments

Good programming practice dictates that 'comments' should be used to annotate programs. Comments are like asides and explain the functionality of bits of code. These are very useful when you come back to a program and can't remember what bits of it do (trust me - that happens!). They can also be used to temporarily remove bits of code from service rather than having to delete them altogether from the program. There are two types of comment in Java; the first is the single line comment:

// Single line comment

As you can see it is simply two forward slashes. Anything on that line (but only that line) after the // will be ignored by Java. The second is the multi line comment:

/* A multi line comment
How very quaint */

Everything from the /* to the */ is again ignored by Java. As the name suggested, this allows comments to straddle across more than one line.

We could therefore re-write the Hello program like this:

class Hello { // Start of Hello class

public static void main(String args[]) {
/* Print Hellooooooo!! To
the screen */

System.out.println("Hellooooooo!!");

} // End of main method

} // End of Hello class

And this will perform exactly the same function as before, but it is now a bit more obvious where things start and end and what the program does.

In the next tutorial we will talk about making the programs more useful with the inclusion of 'variables'. If you want to play about a bit in the mean time, then try changing the text in the double quote marks, or try adding a second System.out.println below the first one. Remember that you will have to re-compile the program (the 'javac' bit above) every time you change it.

If you have any comments about this tutorial (usefulness/clearness/etc., even just tell me if you tried it and it worked!) then please get in touch either in the forums, or IM me.

Previous Java Tutorial

NewsBytes

Friday 22 October, 2004

UK Mobile operators get new watchdog
Mobile phones, the fashion accessory of the younger generation, now get their own independant classification body... Read More

Thursday 7 October, 2004

BPI to sue UK filesharers
Major uploaders to be targeted Read More

Tuesday 5 October, 2004

BT Blocks Internet diallers
1000 premium rate Internet dialler numbers blocked Read More

Tuesday 28 September, 2004

More Delays for Half-life Sequel?
Just when we thought it was getting close... Read More

News Archive

Got News?

Why not submit it to us? You might just see it in our next headlines!

Click here