Categories : Java

 

This is to be the first in a series of tutorials that are intended to help newbies to these fields become more aquainted with the priciples and techniques involved in programming, algorithms, specific languag issue, and the like. As this is intended to be the beginners Java tutorial, I’ll begin with a brief decription of what Java is, what it’s not, and when it’s useful.

What Java is

Java is a programming language that was developed by Sun microsystems to be platform independent. It’s original use was supposed to be for embedded systems, but in the early days of the web (1995, for those who can remember that far back), it became popular because of the ability to make applets, which, aside from animated gifs, was one of the only ways to make a web page have dynamic content.

It should be noted, however, that Java does not just come in applets. Regular applications can be made using Java. An example of each can be found in the programs section.

What Java is NOT

–Java is NOT JavaScript.
–Java is NOT easy.
–Java is NOT the best way to do everything.

If you want to write a Linux device driver in Java, you’re going to be sorely dissapointed. If you try to write simple applications that are better suited to a lower level language (like C, or even Assembler), you are going to find that they are dreadfully slow. The Calc program above is an example of this. The only practical use that Calc has is as a teaching tool.

Java is slower than languages like C because Java is interpreted. It is the nature of the platform independence that Java programs will execute more slowly than other programs. However, Java is quite useful if you are doing the right things with it.

If you want to write a GUI, a network application, or a program that you know you’re going to use on machines without compilers, Java is the way to go. It contains a wide variety of packages that are designed to help you create graphical user interfaces and network apps, which can be difficult to create in C. Also, Java is a clean language, and therefore good for learning the principles of programming, and object oriented programming. Many of the things that beginners first trip on in C and especially C++ (aka, the Devil’s Language) are simply not in Java, so you can focus on learning more of the simple rules and figuring out the algorithms involved.

Okay, enough said… on to the program.

                                        
import java.io.*;                       
                                        
public class HelloWorld                 
{                                       
 public static void main(String[] args) 
 {                                      
  System.out.println("Hello World!");   
 }                                      
}                                       
                                        

First, let me just say that this is bad code; it lacks comments. If you don’t comment your code, than I can almost promise you that when you’ve left it sitting around for months (or years!), and go back to it, you’re not going to remember what everything does. Plus, if you don’t comment your code and you send it to someone to get help, they’re not going to know what everything you’re doing means. Put comments in your code. Put them in as you write your code.

For those of you who don’t know, a comment can occur in two ways in Java. Block Comments are written as /* COMMENTS GO HERE */. These have the advantage of being able to set aside a large block of comments (i.e., the GNU Public Liscence) with releatively little typing. In-line Comments are written as // THIS IS A COMMENT. These are useful for quick notes in the code (i.e. variable x is now 10), and are useful for commenting buggy lines of code out of the program without actually deleting them. Now let us revisit HelloWorld.

                                                                              
import java.io.*; //import statement so we can do some IO                     
                                                                              
public class HelloWorld //This is our class definition.                       
{                                                                             
 public static void main(String[] args) //create the main method of the class.
 {                                                                            
  System.out.println("Hello World!");   //print "Hello World!" to stdout.     
 }                                                                            
}                                                                             
                                                                              

We didn’t need to write any comments that took up more than a line, so using in-line comments was the way to go. Now lets dissect this program

import java.io.*;

This line of code tells the compiler that we will be using methods from the java.io package. In this case, it is the println method. I could have been really specific and told the compiler EXACTLY where to look, but saying that anything in the java.io package is up for grabs is good enough. The semicolon is used to terminate the line.

public class HelloWorld

This is our class definition. In Java, everything you write must be part of a class, which is a way of conatining data for future refference (kind of like a struct in C). Here we specify that the class HelloWorld is a class, it’s name is HelloWorld, and that it is using a public security level (that last part is for another tutorial).

{

This is a left curly brace, it’s how a class or method body is opened in Java. As you can see, they can be nested, but you have to make sure you close all of the ones you open. You close a class or method body by using a right curly brace }. I will ignore all further instances of these because they should be pretty self-explanatory from this time forward.

public static void main(String[] args)

This is the decleration for the “main” method. The main method is the point of entry into the program. It doesn’t matter where you put the main method, in fact, there will be some instances when you won’t have on at all. However, in order for the other methods to execute, somewhere a main method has to call them, or call another method that calls them. The “public” and “static” are best left to another tutorial, but note that methods in Java do have a return type (just as in C). In this case, we declare it to be void because we don’t want the method to return anything. As soon as it’s done printing the data, we want it to exit. The String[] args decleration inside the parenthesees tells the program to expect command line input in the form of a string. However, we don’t do anything with it, so it can be safely ignored.

System.out.println("Hello World!");

This is where we actually call the println method. It is accessed by calling it through a chain. Think of it as accessing a file from a directory. We want to use the println method inside the out directory, which in turn is inside the System directory.

That’s it. The program will now stop running, as it has done everything we wanted it to do.

 Posted on : August 22, 2014
Tags:

You might also likeclose