Java Tutorial – The Basics


Due to some recent events I need to pick up my java programming (and good lord, it has been a while since I’ve written some java code). So I figured to devote a few posts on this topic.
Together we’ll (re)discover the magical world of Java.

First things first, just like HTML or virtually any other programming language you don’t need a specialized editor to write java. If you know what you are doing you can do it in notepad for example. But trust me, it’s a lot easier if you just use an editor which is build to write java.

I will be using eclipse which is free. You can use any editor you like but I will base my tutorials on this editor.
Eclipse basic will do the job.

Alright.. One you get that started open up this tool and click: File >> New >> Project. Enter a name for your new project and leave the rest by default. Click OK. A new folder will be created in the right hand screen. My Project
This screen is where you can find all your projects, packages, classes, etc..
If some of this still seems gibberish to you, don’t worry we’ll get to that. All you have to know for now is that you always work within a project.

Another important thing to know is that you will always write your java code within a class. So every project contains at least 1 class. This classes contain all the information your program needs to run. Let us begin.. On your project, right click and pick new >> class.
Give your class the name HelloWorld and make sure you check the “public static void main” checkbox (I’ll explain in a minute). Don’t worry if it gives you a warning about using the default package. Intime you’ll understand what the problem is (and me as well :P).

When this is done some default code will be generated. You’ll have a class HelloWorld and within this class you’ll have a main method.
Whut.. A method?!.. Yes indeed.. Within a class you can write methods. These are like functions you write to use within your program.
And the class which was generated here is something special. The “main” method is where you input the instructions of the program. Here you’ll describe what the program actually does.
In all the other classes you describe what the program could do.

Right let us try to write some java code. And let’s start with printing something on the screen. Between the { } of the main method (and you always need to make sure you write code between { }) write this line: System.out.println(“Hello World”);

Make sure you end the line with ; . Each and every instruction should end with a ; (there are a few exceptions but we’ll come to that).
This line will do nothing more than just print Hello World on the screen.
In order to do so, we need to run the program.

Do this by clicking the button.

Once this is done you’ll see below the editor Hello World. This is the output of your program.

Congratz! This is you first little java program :).. Next time we’ll use some scanners and variables.

Advertisements

One thought on “Java Tutorial – The Basics

  1. Pingback: Java Tutorial – The Basics Part 2 « Tech-o-rama

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: