Monday, December 23, 2013

Learning to Use Java

I like doing things myself. So do most writers I know. I hate technology. So do most writers I know. Java was probably the biggest annoyance I encountered, but once I figured it out, I was able to use it to do all kinds of stuff. Java is now my best friend in the technology world, right up there with wireless internet and word processers. But learning to use it can be a challenge. so here are some tips.

In 1995, Sun Microsystems a compiled programming language called Java. This language, which is free to download and integrated into most computer applications today, is platform independent. This means that Java can run on any operating system and so is truly a universal programming language.

Using Java on your computer is a fairly simple process. For the most part, once it’s installed correctly, it just runs itself with very little maintenance from the user except for the occasional update. Java requires Java Runtime Environment (JRE) to run correctly. It is this program which allows the Java code to execute. JRE can be downloaded for free and can be easily located using your favorite search engine.

For those users who wish to write Java code, an additional program will be required. The easiest way to write Java code is by using a Java Development Kit and compiler combination such as Eclipse. There are other programs available, but they all work in much the same manner. They allow you to review and examine your Java code, and even point out syntax errors that you may have missed. These programs are invaluable for the beginner wishing to learn how to write Java code.

Once JRE and Eclipse (or other program; Eclipse will be used in this example) are successfully installed on your computer, you can begin to write your first program. Open Eclipse and begin a new project. You’ll have to name your project, but what you call it isn’t important; for now you can simply call it ‘myProject’. After you’ve named your project, your main class and main method should be automatically generated.

This may sound a little confusing to the beginner, but classes and methods are simply used to accomplish certain tasks within your program. Classes are required to create your program, but are really rather useless things when left all by themselves. Methods are equally useless alone, but are necessary components to have the classes accomplish anything. The methods are those things which put your program into action.

Your new project should look something like this:

public class myProject{

                public static void main(String args[]){

}

}

The term ‘public’ means only that everyone can access it. You can, if you wish, designate your project as ‘private’ or ‘protected’ but it might confuse you right now. Since you’re only experimenting with Java, you can leave it as ‘public’ to avoid any problems that might occur as you’re writing your program.

It is important to understand that your program can have many methods. However, it can only have one main method. The main method of your program is the method that will be called initially when you first run your program, so you can never have more than one main method. Your starting code will go after your main method.

At this point you may be simply staring at your screen wondering what you’re supposed to do next, so try running a very simple program. Choose a name. Any name will do. In this example, we’ll pretend you have a dog named Fluffy and you want his name to appear on the screen. To accomplish this, add the line System.out.println(“Fluffy”); right after your main method initialization.

To execute your program, you’ll need to ‘run’ it. This is the easy part. The program you’re using to write the code should have a run tab. Click the tab and select ‘runas -> java application’. This should execute your entire program beginning with your main method. Since you only have the one method in this particular example, there shouldn’t be any confusion.

Once executed, the program should print out the line “Fluffy” to the console. If it doesn’t, check your code again. You may have neglected to add the semi-colon (;). All Java statements must end in a semi-colon, or Eclipse won’t be able to make heads or tails of your program. If it helps, think of the semi-colons as signposts, telling everyone what’s what. This isn’t a perfect analogy, but they’re just as necessary as signposts. Without them, everyone ends up lost and confused.

This is a very simple example of how to use Java to write code. It can obviously be much more complex, especially when you start adding additional methods. However, with a little practice and a good book (or website) on the subject, you should be able to write a variety of Java programs.