Yet again, I’m posting partly to give you all a sign of life. Also, this is a short Java tutorial with some of the basics.
I chose to write a Java tutorial because Java was the first programming language i learned, and it iis easy to move on to C, C#, C+, Cocoa, etc., when you know the principles of Java. That is because all the previously listed languages are object-oriented programming languages, and so is Java.
So, read on if you want to learn some basics of Java!
Intro
This is gonna be a learn by example – tutorial, so you’ll need some stuff before we start.
The requirements
- A java compiler/editor (I’m using BlueJ, as it’s Universal Binary, and super-easy to understand)
What are we going to do?
After finishing this tutorial, you will know the basic structure of a Java app, and how to debug, compile and run the app.
The app will simply display a quote by Abraham Lincoln. It will not be dynamic, as that is a little more advanced, and I do not aim to cover more than displaying output in this tutorial.
And we’re on the road!
Let’s start off by looking at the BlueJ main window. (I’m assuming you all use BlueJ from now on. If not, I’m sure you’ll find your way around any other app on your own.
So, the first thing that might hit you is: “Damn, that’s one anti-delicious app!” True, but how often do you take screenshots while programming?
On to more serious stuff, to start programming, press Project in the menubar and select New Project. Then you will be presented with a new window. Give our project a name, like Lincoln, then choose a relevant loaction for our it. I place all my apps in HDD/Developer/Documents to keep things organised. :) Press Create and you’ll see that the main window has changed. There buttons on the left are now clickable, and there’s this thingy in the big white area. What’s that?, you may ask. That is a presentation of all the files in the folder that was created when you pressed Create.
Double click on the file and the editor will open, and display the file’s contents, which are in this case Readme stuff. If you want, you can edit this file to your liking, then save it by pressing Compile or Cmd + S.
Now that you’ve frantically filled the readme file with mental self obsession, we can move on. Click New Class to, obviously enough, create a new class. Give the class a name, like Lincoln, and make sure Class is selected as Class Type. Now you’ll see there’s a new thingy in the file overview. This one is the class you just created.
Filling the class
First of all, double-click the class, and type everything from the below picture into the editor that just popped up. (Sorry, Symphony is acting strange, so copy/paste gets all f***ed up.)
Now let’s dissect that.
*********************Code*******************************
//———————————————————————
// Lincoln.java Author:
//
// Demonstrates the basic structure of a Java application.
//———————————————————————
*********************Code*******************************
The double slashes (//) indicate that these lines are comments. Comments start with // and continue to the end of the line. And now another quote which I like a lot. (This is not to be used in the code, it’s simply a useful tip.)
“Comments do not affect a program’s processing, they server to facilitate human comprehension.”
A programmer should always comment his/her code, to make it easier to navigate through later, and as a reference for other people on a possible team.
**********************Code************************
public class Lincoln
{
********************Code***************************
This is the beginning of the class definition. Public class Lincoln is the name of the class, and the opening brace ({) indicates that the definition of that class goes from the first { to the last }. The class definition is what actually tells the computer what to do with your app.
Then there’s another comment before we get to the important stuff.
*********************************Code*************************************
public static void main (String[] args)
{
System.out.println (“A quote by Abraham Lincoln:”);
System.out.println (“Whatever you are, be a good one.”);
}
}
*********************************Code*************************************
Then there’s the definition itself.
Each class has a main method, which contains, in this case two programming statements. Like a class, every method starts and ends with braces ({}). All Java applications have a main method, which is where processing begins. Each programming statement in the main method is executed in order, until the end of the method is reached. The main method in a Java app is always preceded by the words public static void, which I will not explain in this tutorial.
The first closing brace } terminates the main method and the second terminates the class and therefore also the app.
Compiling and running
To save the app, press the Compile button at the top of the editor. When you do that, BlueJ checks for syntax errors in your code. Syntax errors are more commonly known as bugs. They cause the app to crash. If you’ve done everything correctly, you should get no syntax errors, and you can then close the editor.
To see the result of your code, right click on the Lincoln class icon in the file overview and select void main(String[] Args). Click ok in the following dialog and admire your work.
Outro
That’s it for now. I’ll probably write some more Java tutorials later, so keep an eye out. Hope you enjoyed this tutorial, and please leave a comment if you did. :)