Java

The version of the Java Development Kit you have installed in RacyPy2 is 1.7.0_02.

Firstly, I’d like to recommend Herbert Schildt’s books. I am using “Java: The Complete Reference”.

You can download the (free) first chapter of his “Java: A Beginner’s Guide” from various places online.

I am not going to write a lot about Java here – it’s better if you read the Schildt chapter. But I will mention that in Java, all programs use the Object Orientated programming model. We will get around to using this approach in Python (it’s very useful and amazingly powerful). But for simple programs, we haven’t really needed to use it.

What this means in practice is that every Java program has at least one “class”. Here’s an example:

The first four lines are a multi-line comment – we use /* to start these and */ to close them.

Next we have the command that declares the main “class”¬† of the program. It’s essential that this has the same name as the name of the file. It’s traditional to give this class an initial capital letter. Java is case-sensitive. So if i have called my file “Hello.java”, my class has to be called “Hello” (not “hello”). The curly braces “{” are used to group a block of code – here the contents of this class.

The next line is a single-line comment which we do by starting the line with “//”.

The next line is one you will see in almost all Java programs (I think – I am only a learner!). I’m not entirely sure what each of the commands means yet! But the important thing is that this line include the call to “main()” which all Java programs need. The next curly brace opens a new code-block – this time for what our program will actually do. What this line actually does is to invoke a class called “System” which gives us control of things like output to the screen. Within this class there is a method called “out” which does just that – outputs to the screen. The “println()” bit is just like print() in Python3 – it will print what we put inside the brackets – in this case a string enclosed in quotation marks – and add a newline. The line ends with a semi-colon because all “statements” in Java must end with a semi-colon. The lines that don’t end in semi-colons are not actually “statements” (yes – I found this a bit confusing at first, but you’ll get the hang of it!).

Then we close the curly braces that enclose the “main()” part of our code, and finally close the curly braces that end the description of our class “Hello”.

Anyway, the fun bit comes next! We have to compile our program!

Open a terminal in the directory where you saved “Hello.java”. Then type:

javac Hello.java

This compiles your program into Java bytecode. You then issue the command:

java Hello

And your program should run.

Just to make this really clear, I’ve done a few extra commands here. Typing “pwd” displays the directory we are in. Then “ls” lists the files in it. As you can see, there’s just the file “Hello,java”. Once we have run “javac Hello.java” a new file is created: “Hello.class”. When we type: “java Hello”, Java looks for a class called “Hello” and executes it, printing: “Hello World!”.

Wow, are you having fun yet!

antiloquax

Advertisements

Tags:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: