Wednesday, 20 August 2008

Everything you would ever need to know about Comments and Javadocs!

Yes everything. Once you've finished this post, you will be a Comment and Javadoc guru. Woo hoo!

Comments are pretty straight forward and pretty much everyone knows what they are and how they work. Javadocs on the other hand are not so well known. On my university course in computer science, I was taught what a Javadoc was, but I was never expected to create one! Only after seeing them in a real world application did I realise their importance. (Plus they are well cool and easy to make).

Your Guide to Comments:

What is a comment?

Comments are the part of your code that the compiler does not parse. In other words, comments are not really "in" your program once it's run. There are implementation comments, which ill be explaining first, and documentation comments, which are used to create Javadocs - comments exported into html format to document your code and help others who don't have access to the source code to understand the specification of your code (Like an API).

Why comment?

Comments can be used to explain your code. Either so you can understand it (trust me, you will forget what that method was meant to do), or so another person can understand it. In big projects it is highly recommended to comment your code so that other programmers can understand whats going on without having to dig through it.

Comments can also be used in debugging and testing your code. I always temporarily comment out sections of code to test to see if its the reason I'm getting errors, you can also comment out code that prints out useful run time information. When you need extra information on the contents of an array for example, uncomment out the print statements.

Finally comments can be used to populate your Javadoc. Ill explain what that is later.

Types of implementation comments:

End-of-line comments: An end of line comment "comments" out the whole line after a double forward slash.

So:

// This is an end-of-line comment

// This prints out the first element in the array
// System.out.println(someArray[0]);

Notice that the print statement is commented out. If I needed to know what was in the first element of someArray, I would delete the end-of-line comment!

Single line comments: I actually prefer this type of comment over the end-of-line comment. With this you specify the start and end of your comment. This gives greater control over what you want commented. It starts with /* and ends with */ anything in between is a comment.

So:

/* This is a single line comment */

They are useful for describing what lines of code do. For example I use them with if statements like so:

If(a < b){
/* a is less then b */
}else{
/* a is more then b */
}

Of course in this example the comments are not very useful as its not hard figuring out what the if statement is doing. Comments should be precise and give information that is not easily readable from the code.

Block comments: If your comment is long, or covers more then one line. (you might be summarizing a method, data structure or algorithm) then you will need to use a block comment.

example:

/*
* This is a block comment
* This is a block comment
* This is a block comment
*/

Javadocs - Documentation Comments:

Javadocs or Doc comments enable developers to easily document their code. Most IDE's can automatically generate Javdoc html by parsing through your code and interpreting the doc comments. Doc comments describe Java classes, interfaces, constructors, methods, and fields. Each doc comment is set inside the comment delimiters /**...*/, with one comment per class, interface, or member. This comment should appear just before the declaration:

/**
* Example class provides...

*/
public class Example(){ ...


If you need to have both a Javadoc and a comment explaining the method (one for documentation the other for development) then you should position the Javadoc comment first, then followed by a block comment. Also please remember that doc comments are associated with what immediately follows it (except other comments) and so you cannot write a doc comment after or inside a method declaration!

Now there are certain tags you can use to better document your code. If you are using Eclipse it will create some of the basic ones for you automatically. Just type /** and press enter before a method, class or interface and watch as it inputs the basic outline of your doc. Here are the most useful tags that I use:

@param Give the parameters (inputs) of classes, interfaces, methods or constructors
@return Give the return values of non void methods
@author The author of the class or interface
@see Links to relevant classes

I also use the {@link URL} tag while summarising to link to the class I'm talking about.

To be honest the best way to get used to using Javadocs is to practice creating them in every program you write. Fire up Eclipse, write a quick program and try the tags I've mentioned. If your still a little confused on how to organise them, take a look at the links given at the bottom of this post.

Finally I'd like to quickly explain how to export Javadocs using Eclipse:
  1. After creating your program. Go to file
  2. Click export
  3. Find Java and open list and select Javadoc then click next
  4. If your Javadoc command is empty go to step 5, otherwise go to step 6
  5. Click configure and search for your jdk folder, find javadoc.exe and click open (mine was in C:\Program Files\Java\jdk1.6.0_07\bin)
  6. Select the packages you want to include in the Javadoc
  7. Click Finish
  8. By default it should be saved in your workspace. Go to the doc file and open up the index.html to see your newly created Javadoc!
So now you are a Comment and Javadoc guru! Of course there is much more to learn, Javadocs can include html tags so feel free to liven them up with external links, colours and whatever else you might desire! Feel free to email me or leave a comment if you have any questions or improvements on what Ive wrote here.

Bye for now!

Here are some interesting links about comments and Javadocs:

What each comment is:
http://java.sun.com/docs/codeconv/html/CodeConventions.doc4.html
Wikipedia Javadocs:
http://en.wikipedia.org/wiki/Javadoc
Source code example including Javadoc:
http://java.sun.com/docs/codeconv/html/CodeConventions.doc10.html#182
How to write Javadoc comments (official Sun website):
http://java.sun.com/j2se/javadoc/writingdoccomments/index.html
Javadoc Tool homepage:
http://java.sun.com/j2se/javadoc/index.jsp




Tuesday, 19 August 2008

Lets talk IDE's

First, if you don't know what an IDE is go to
http://en.wikipedia.org/wiki/Integrated_development_environment.

Read up on what they are. Also for a hands on experience download Eclipse (and if you followed my previous posts you should already have Netbeans). Which ones better? I wouldn't know, I can barely use either of them. I started on Netbeans but thought I'd try Eclipse. I have to admit, I'm now hooked on Eclipse (shameless plug).

Yeah so I really don't know much about them. I have only recently started using one. I started programming using a text editor called JEdit. It would highlight text, that's about it! Then not too long ago someone finally got round to telling me what an IDE is (yeah thanks guys, you know how long I spent trying to debug my code!!). When I downloaded Eclipse and created a quick program, my jaw dropped as to how much easier it makes programming and debugging. There is automatic code tips that finish off what your typing before you even know what your typing, there's error checking as you program, auto import statements, code snippets and those are just some of the great reasons why I now love Eclipse. Now I need much more practice with Eclipse (or Netbeans) to be able to give you an experts view on them, but so far I'm very impressed.

I have read that IDE's can make a programmer lazy, having to rely on them and not knowing the API without it. In my opinion, when are you ever going to be in the position of not having access to an IDE? Trapped on a desert island and you need to program your way off? I don't think so. I think learning an IDE is important, and I'm sure employers would be impressed if you could use them well. (Knowing your hot keys can seriously shave off valuable programming time).

Right, now I know I said in the last post I was going to be using Netbeans, but as I just said Ive turned to Eclipse. I don't actually know anyone that uses Netbeans (sorry Sun). I also remember saying that I was going to re-create the Hello World program using an IDE. Somehow I no longer find this necessary as 1) Its soo easy anyway (given the code I gave you and Eclipse/Netbeans) and 2) Here's a link of how to do it already:
http://java.sun.com/docs/books/tutorial/getStarted/cupojava/netbeans.html.

So if you were really looking forward to how to use an IDE to get your Hello World program working, click that link. It uses Netbeans (am I a hypocrite?) and is actually quite informative.

That's it on IDEs actually, sorry I couldn't say more. I will do a post further on in my quest when I consider myself better practiced in Eclipse. If you have any comments to make please feel free to click the comments button. I'd love to hear your IDE stories! (Any Netbeans users? Anyone? No?).

Link to Eclipse download: (Get the one for Java Developers!)
http://www.eclipse.org/downloads/

Next Time I'll be practising the basics, I might throw in some programming tips and conventions as well if your lucky. :)

Monday, 18 August 2008

Hello World!

Because if you didn't know, getting your program to print the sentence "Hello World!" is the standard for the first program you write in any language, why? I don't know.

Ok, first up, notepad. That's right, good old notepad. Open up notepad because were going to write the first program in it, compile it using the command line interface and then run it. After that were going to do the same program in the Netbeans IDE. Why? Because everyone should know how to write, compile and run a Java program without an IDE. After you can do that, then consider choosing an IDE and learning to use that. Ill explain the benefits later.

Here are some things you may need to know about the whole process as a whole...

You write your code in text documents, which are saved as .java files. This is your source code. If using an IDE it may append other files and folders to better organise your work, but for a simple program, remember the source code is saved in .java files. .java files can be compiled into .class files. The compilation process is such that the javac compiler turns your source code .java files into machine readable Byte Code (.class files). These can be understood by your computer via the Java Virtual Machine (Remember JVM?) which means it can be run, theoretically on any machine with the JVM interpreter. So once your source code is compiled into byte code, you can run your program. Hope that's clear...

I'm going to give you the code, all you have to do is copy and paste it. At the moment don't worry about the details, in the next post I will explain it. Right now I think its important to be able to get through the compilation process and just get the program running before getting into specific details.

/* My first Hello World program */

class HelloWorld{

      public static void main(String[] args)
      {

            System.out.println("Hello World!"); //Print Hello World in the console
      }
}

Paste that into notepad and save it as "HelloWorld.java". It must be called HelloWorld.java, and nothing else. This is because the main class name must be the same as the file name. Save it onto your desktop.

Now open up the command prompt. Start Menu, and either in run or in the search box type "cmd". Now you need to locate your HelloWorld file. My console starts in C:\Users\Aaron. To get to the desktop type "cd Desktop". Use the "dir" command to display the list of files and folders in your current folder to find where your going. If you cant get to your desktop, just save the HelloWorld.java file in a folder you can find.

If you type "dir" now, it should have a list containing HelloWorld.java. To compile this, type "javac HelloWorld.java". This uses the javac compiler to compile that java file. A common mistake here to use "javac HelloWorld" this is wrong, you must state the full file name. Another problem people run into when compiling their java files is that they get "javac is not recognisable" or something like that. This means that the path is not setup within the console environment for it to find the javac compiler exe file within your computer. Go here http://www.phoons.com/john/classes/aboutpath.html to learn how to do that. If your still having problems drop me an email at aaron (at) gravityapple.com and I will get back to you usually within a day. (yeah I check my emails like every hour...)

If you get any errors, it is probably because you didn't copy and paste the code right, the file isn't called HelloWorld.java, your not in the correct folder in the console or your javac path is not set up properly. Go here http://java.sun.com/docs/books/tutorial/getStarted/cupojava/win32.html if your having problems compiling and follow the Sun Microsystems steps. If you did'nt get any errors, your ready to run! Notice if you type "dir" and check the file list, there will be a new file called HelloWorld.class. This is the bytecode class file I was talking about which is the actual file the Java Machine runs. Type "java HelloWorld" (no .java this time). The console should print "Hello World!". Congratulations if it does, because you have now successfully created a java file, compiled it and have run it successfully. Give yourself a pat on the back, and an email telling me how much you love it.

Next post will be on how to do the same thing, (Hello world!), in Netbeans. I'll also go through what an IDE is and why you should use one.



Ok, I want to learn Java, where to start?

So you've decided to learn Java, whether your learning the language as your first programming language or whether your already experienced in another language, your going to need these tips on how to get started.

First off, I want to point out some confusions I had when getting to grips with Java. I would wonder how comes I could view Java programs online? Does this mean I have Java installed? What do I need to develop these Java programs? And finally... what is Java script?
-
1) Becuase you have the JRE - The Java Runtime Environment
2) You can view Java programs, but not create them, for this you need the JDK - The Java Development kit
3) See 2
4) Javascript actually has nothing to do with Java by Sun Micosystems. It only shares the name. Its actually a scripting language embedded in websites, just dont confuse the two.

Java comes in two packages. The Java Runtime Environment (jre) which is what helps run the Java programs and the Java Development Kit (jdk) which is used to develop Java programs. Most people will already have some version of the jre and have already been viewing java applications on the web. I recommend that before you do anything, you update your jre to the newest version. Follow these steps:

1) Delete all previous Java versions. Go to Start Menu -> Control Pannel -> Add and remove programs (Programs and Features) -> find all java related programs and delete them (including all updates).

2) Go to http://www.java.com/en/ and download the newest Java Runtime. (click free java download).

3) Follow the instructions to install it.

So now you can open Java files. Great.

Next your going to need the JDK. (The development kit remember?) Once again I hope you have deleted all previous old versions as in step 1 as your going to need the most up-to-date version to follow this blog, as I ain't messin' wid no old shiz yo! (sorry).

1) Go to http://java.sun.com/javase/downloads/index.jsp and download the JDK with Netbeans. At this moment in time its "JDK 6 update 7 with Netbeans 6.1". Netbeans is an IDE, ill come to explain what an IDE is later.

2) Follow the instructions and install it.

Ok so now you have the JRE, the JDK and Netbeans. Your all set to start making and viewing Java applications!

If your still unsure about anything in this post, here are some suitable links:

Whats the difference between JDK and JRE? : http://www.jguru.com/faq/view.jsp?EID=46223

Java Sun website, How to develop Java programs: http://www.java.com/en/download/faq/develop.xml

The Java Tutorials: http://java.sun.com/docs/books/tutorial/

The best way to find answers to your questions is to Google it. E.g. "What is the JVM?"

Ok, so I hope you've got everything installed and updated. Next time I will go through how to develop your first Java program, both by using the command line (windows users) and through the Netbeans IDE. I missed alot of introductory stuff in this post, like what Java actually is, why its so popular, what is the JVM etc etc. These are the kind of questions you will in any Java programming book, and have probably been posted on the web countless times. I suggest if you want to learn the "book stuff" that you buy a book. I recommend Introduction to Java Programming by Daniel Liang. Although any beginners book will do.

If your not about to fork over £40 for a Java book, just look online. Go to the Java Sun Microsystems website, they have tons of information and Beginners guides.

Next up, Hello World!...

Sunday, 17 August 2008

Welcome, Hello and Whats up?

Welcome to Apple Java,

First off, yes this is another Java Blog. If your an expert in Java programming and your looking for a new hip blog to RSS feed off of like some kind of tips drug, then this is not the place for you. Why not? I am not an expert in Java, I wouldn't even say I was particularly good at it. What this blog is, is it's a depository of my working quest to learn the Java language and become a better programmer. Kind of a zero to hero account of everything I attempt, do or even think about in Java.

So follow these posts if you too want to learn the language, ill be going from the beginner levels, which I already actually am quite familiar with, to the expert levels (If I get that far!). Feel free to comment on the posts, to praise or criticise my programming style or just to say hi.

Well I'm not one for intros so that's it actually, ill let the content talk for itself.