Saturday 6 June 2015

Hello World

Getting started with any programming language can be painful. Java it seems goes out of it's way to hurt your brain and make it explode. Oracle do however have some very good and freely accessible tutorials over at their website. These will teach you the basics of the Java language. Examples of which I intend to post here as I learn the language myself.

The very first program you will ever write in any language is the "Hello World" program. It's an important starting point. It's your introduction to the language and it's most basic syntax. But first some important information.

These tutorials are based on version 7 or 1.7 of the Java language (no I don't understand the need for two version numbers either … other than marketing). I'm also using the Eclipse IDE version 3.8.1 with Ubuntu 14.04.

Hello World Code:

public class helloWorld {
        public static void main(String[] args) {
                System.out.println("Hellow World")
        } 
}
Using an IDE like Eclipse or a text editor like gedit or notepad, copy the example into a file called helloworld.java. It is better to type the code yourself rather than copying and pasting it. You'll be more likely to remember what you've typed than copied.

If you are using Eclipse and have never used it before. Don't worry. Eclipse comes with built in tutorials. It will in fact take you through the creation of the exact code above as it introduces you to it's basic features. You can run the program in Eclipse. Just follow the tutorial. Other wise you will have to use the javac compiler tool. This requires a little command line work. So go ahead and open a terminal on your PC. Switch to the directory containing your helloworld.java file and type the following command.
javac helloworld
Now switch to the directory where the binary file was created and run the following command.
java helloworld
Your program should now print the line “Hello Word”. And note I've deliberately left off the file extensions. They are not needed here.

No comments:

Post a Comment

Translate