Computersight > Programming > Java

The Basics of Java Programming [1]

It doesn't take long to learn the basics, and once you do, you can write your very own java programs.

Getting Started

Before we begin, you are going to need a few things to work with Java. These are:

Okay, once you have all these things installed and setup, you can proceed into some java programming.

Note: Any words in italics is one which you should familiarise yourself with. I have included the definitions of these at the end.

The Basics

Java is a very easy-to-use language and very user-friendly, especially if you have programmed in other languages before. If you haven't, don't stress, this tutorial will teach you step by step. To begin, lets go over the basic syntax of a java program.

Here is a simple program which prints "Hello World" into a command-line box:

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

}

Let's look at this step by step. The first line:

public class HelloWorld {

The word "public" refers to the level of access of the program. Public is used to allow other programs and classes to interact with it. "class" begins the definition for a class which we have named "HelloWorld". It isn't important right now to know all about classes, just know our class is created named HelloWorld and is signified by the curly bracket ("{" and "}"). These brackets define where a class, method, function etc begin and end. Lets move onto the next line:

public static void main(String args[]) {

Once again, "public" is used as an access level identifier. "static" methods are used for methods such as computing mathematical equations and other processing in which other objects aren't used. Don't worry yourself too much with this, all you need to know is we are going to use it in our programs. As your Java knowledge expands, you will understand more and more about these types of things. An important thing to know is that all Java programs must have a "main" method. This method is the entry point for your program and will be used to activate other methods required by your program. The main method accepts a single argument: An array of elements of datatype String. Each string in the array is called a command-line arugment which lets users affect the application without recompiling it. Again, do not worry too much about this, just know that this line of code is necessary for Java programs. The last line of code is what actually prints the text "Hello World":

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

This line of code uses the "System" class from the core library to display the text "Hello World", hence the "out.println" which basically means "Output and Print Line 'Hello World'". Any text within quotation marks (i.e. "Hello World") is a string and can contain letters, numbers and some symbols. You will notice the semi-colon after the bracket, this is tell the computer where that specific line of code ends. This is required after every line of code except those which end in a curly bracket or a few other exceptions which are unimportant for the time being.

Now that you know exactly what this code means, you can try it yourself. Open your IDE (hopefully Eclipse) and start a new java file. Type in or copy and paste the following code and try compiling and running your program.

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


}

NOTE: When saving the program, be sure to name it EXACTLY as you've named your class. Otherwise your program will not work. For my program, I would call it: "HelloWorld.java".

Hopefully you receive a black command-line box with the text: "Hello World". If so, congratulations! You have just written your first Java program! My next tutorial will cover variables and simple mathematical operations. I hope you enjoyed learning about the Java language and see my next tutorial for more Java fun.

Glossary

Java Development Kit (JDK): This is a necessary Java kit needed for compiling and running java programs.

Integrated Development Environment (IDE): This is a text editor which usually has a compiler built into it, used for programming in Java and a range of other languages.

Syntax: The syntax of a program is the basic "template" or "layout" it requires to understand what you're telling it to do.

Public: Public refers to the level of access of that class, method etc.

Method: A method is a set of grouped instructions which can be called to run at any time within the program.

Function: A function is similar to a method but a function uses values you can send to it to return a different value after doing the appropriate processing or calculations.

Datatype: Datatype is a word used to define variables etc as a certain type. i.e. A string has a text datatype and can accept letters, numbers and some symbols. An integer on the other hand, can only contain numbers.

String: A datatype for text, see above.

4
Liked It
I Like It!
Related Articles
The Basics of Java Programming 2  |  The Java Platform
More Articles by Jay A
The Basics of Java Programming 2
Latest Articles in Java
How to Make an Analog Clock for Your Website  |  Collection Framework: List Interface
Comments (5)
#1 by Ed, Aug 20, 2008
very helpful article, thanks
#2 by Tarik, Aug 20, 2008
Really useful. This guide helped me.
#3 by Mike, Sep 8, 2008
good job
#4 by , Sep 19, 2008
awesome tutorial
#5 by , Sep 22, 2008
sweet as, it helped me alot
Post Your Comment:
Name:  
Copy the code into this box:  
Post comment with your Triond credentials?
Inside Computersight

Communication & Networks

 /

Computers

 /

Hardware

 /

Operating Systems

 /

Programming

 /

Software


Popular Tags
Popular Writers
Powered by
Computersight
About Us
Terms of Use
Privacy Policy
Services
Submit an Article
Advertise with Us
Contact

© 2007 Copyright Stanza Ltd. All Rights Reserved.