I want to share with you guys a simple program to demonstrate what software programming is about; this is a "Hello World!" example that many people see when learning on how to write computer programs using a specific programming language.
In this case, I'm going to show you on how to create the example program in JAVA. If you do not have JAVA installed, be sure to go on to http://www.oracle.com and download the latest version of the language plugin on your computer. You should also take the time to download a developer's toolkit and an integrated development environment for you to write programs at a more convenient level.
The advantage of using an IDE over a text file is, every time you save your source code file, it is compiled automatically. You can also see the moment you make a mistake, a compiler error seen on the scroll bar where if you hover your mouse over, you'll see why you get an error like this.
Read the on-screen instructions and check to see if you have enough space on your computer or hard drive to install the software needed. By the time you have it installed, open up the IDE and follow the introduction instructions and how to go to the workspace and write a new source code file.
When you get there, you should see a large white text area with the cursor on the top left corner, with no characters inserted. That's where you will start writing your source code.
Let's get started, shall we?
The first thing you need to do is declare and open up a class definition. This is where your program is going to lie; the other purpose of a class definition is to prepare "a recipe where you can make exact copies of it," which is essentially the principle of object-oriented programming. But let's worry about making a simple program for now.
Type in "public class," all in lower case, and then the class name. You want to make sure that the class name is the same as the file name you're typing in, without the .java extension, because otherwise you will receive a compiler error. You cannot declare a class name that is other than the name of the source code file itself.
Since we have created a file called "HelloWorld.java," the header of the definition will look like this:
public class HelloWorld
Next, press Enter, and type in the left brace, press Enter, and the right brace. This is where you should be so far:
public class HelloWorld
{
}
Now, you want to add an empty bar between the braces, because you're now going to add a method called "main." This is what the code should look like by now:
public class HelloWorld
{
}
The "main" method is always the first method the program goes to upon execution. This is where your starting statements or your method calls will be done. The header is usually constant, but you'll eventually see that the parentheses can be set up in different ways as long as you can declare it of the same type.
Anyway, the header of the main method looks like this:
public static void main(String[] args)
If you are curious about what each part of the header is, let me tell you what they are.
"public" is an access specifier. It tells the compiler how the method can be accessed. There are two other specifiers you can use, with an extra one being no specifier at all; each one of them has a different visiblity, so you may want to stick with the specifier "public" before learning on how the other specifiers set the access to a method or a class. The other specifiers you can use for a method or class are "private" and "protected"; do not use them in your class declaration, however. You can only use them in what are called inner classes, which you shall see later if you decide to keep learning.
"static" simply means it is part of the entire class and not to every specific object. Let's say you instantiate 10 objects out of the Car class you've programmed yourself beforehand, and you declared a counter in the class with the word "static" that every time you instantiate the class for an object, it increases by one. Instead of returning 1 when you want to display that variable, because it is common to all objects, it returns 10.
You cannot reference a static variable or method with a non-static variable or method; otherwise you will get a compiler error. All Java classes use main as part of the entire class instead of every single object instantiated, so that's why the method is declared as a static method.
"void" is a return type. All method headers you write need to have their return types declared, but since the main is not designed to return anything, we use the key word "void." For any primitive data type or object type, you have to specifically return a reference or an actual value, as dependent on the type. If you attempt to return something in a void method, you will get a compiler error.
"main" is simply the name of the method. All methods and classes have names associated with their declaration; it's the norm for programming applications or experiments.
And what's inside the brackets are simply the arguments of the method. In this case, we are declaring one argument to be an array of String objects. You'll see more information about this by still attending your programming course or even reading a book on doing programming like this,
Anyway, moving on to the programming, what you want to do next is the same thing you did as the step after declaring your class: add two opposing braces, with an empty line, over three different lines. It will look like this:
public static void main(String[] args)
{
}
This program consists of displaying a simple message, so what we want to do is use console output. In Java, the way to do this is to use a method called println, referenced by System.out. The statement looks like this:
System.out.println();
Any object reference, variable, or a constant string message, can go between the parentheses and be displayed on the console upon execution. Here, we want to display a message, so we write the statement like this:
System.out.println("Hello world!");
Put the statement inside the braces of the main method. The final program will look like this:
public class HelloWorld
{
public static void main(String[] args)
{
System.out.println("Hello world!");
}
}
Compile your program using the IDE compiler or the command way, run your program on the command prompt, or IDE, and voila: you will see "Hello world!" on your screen!
That's how you write the most simplest Java program; if you keep paying attention to every specific detail of the program, you'll eventually build your steps to becoming a programmer.
Anyway, I'll see you around!
Tuesday 28 May 2013
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment