Dec 17, 2007

Your Basic Blend

Hello there!

Alright so now we get to some of the basic code structure of Java!

First we have the source file.
- Inside your source file you put your classes.
-Inside your classes you put your methods.
-Inside your classes you put your statements.

A source file is a file with a .java extension.
Then inside your source file you have (this would be in your text editor):
(P.S. these statements should be tabbed but blogger takes them out. I'll get on it)

public class Heater {
void heat() {
1stStatement;
2ndStatement;
}
}

So let's make the traditional initiation program "Hello World".

We know it must have a source file, class(es) , method(s) and statement(s).

file HelloWorld.java (in text editor save it as a .java)

public class HelloWorld {
public static void main (String[] args) {
System.out.print("Hello World");
}
}

Alright let's break that down...

public class HelloWorld {
-the public means it can be accessed by all
-class means it is a class...
-HelloWorld is the name of the class
-you must open the class with a brace {

public static void main (String[] args) {
- once again public means it can be accessed by all (as opposed to private which we'll get to)
- static we'll do later
- void means it is a method with no return type
- main() is the name of the method, the main() method always runs first!
- (String[] args), this is a line that creates an "array" of strings and were calling that array args and is a parameter of main(). This allows for information to be entered into the program from the command line as far as I know.
- you must open the method with a brace {

System.out.print("Hello World");
-System.out.print puts the string(characters, letters, numbers strung together) that is in the parenthesis () and quotes "" into the command line.
- you must end a statement with a semicolon ;

Then you finish up by closing the method then the class.

Now we want to run this.
First we must compile this into a .class file.
Navigate to the folder your file is in in command prompt (unsure for mac)
Type dir to check that the .java file is in the directory.
Then now that we have the Javac command because of the last post type:
javac HelloWorld.java

If there are no errors in your program this will compile. Otherwise your going to get some odd messages. I'll put some common errors and errors I made when I first did this.

- Java is case sensitive, capitols matter!
- All statements must end with a semicolon!
- Your string must be within quotes!
- You must open classes and methods with opening braces!
- You must close classes and methods with closing braces!

Alright so if you have your .class file now you can type (still in the same directory):
java HelloWorld
and voila! You get the uber-exiting message Hello World.

Now that you have that you can edit your original code to make that say whatever you want. If you want the message to appear on new lines you just have to put in another identical statement with a different string. Have fun!

2 comments:

Darren Kuropatwa said...

This is great stuff Gra-eme! You should write a blook! (book on a blog)

Uh ... I guess you already are. ;-)

Cheers!

Mal said...

Wow is all I can say. Mr.K said it best, you are writing a "blook." You're explanations are simple, easy to follow, and very nicely organized. Keep it up my friend.