untitled
viviti

Java Tutorial


Writing and Compilling in Java

Writing the program

To write your first program you will need to open up a text editor(such as textpad) and type out your code. You then save the file as .java. From command propmt use the javac command(after you have downloaded java) followed by the filename to complie the program and java followed by your filename to run the program(after it has been compiled).

You may have problems doing this(I certianly did), if you do you need to set up your computer so that it knows where to find the javac and java commands, alternativley you can use textpad and under tools you should be able to use the complie java and run java application functions

What to write

The outline of any java program should be the following:
import java.io.*;
//other imports go here

public class helloworld
{
//declerations are made here

	public static void main(String[] args)
	{

	}

	//functions go here eg)
}

All java programs have these parts, at the top you import any packages that you will be using, I have put java.io.*; only because that is usefull if you want to do simple i/o(input/output) before we move on to using a screen. The public class bit declares the program itself. Any variables that are to be stored in the program are to be put just after this and before the first function. Then the methods to be used in the program are entered, this only has one method – method main, this is executed when the program is called.

This may be a bit confusing at first but below is an example that will (hopefully) help you see how to write a program, anyway over time as you this template you will begin to see how it works and why the different bits are needed but until then you only need to know how to use it.

import java.io.*;

public class helloworld
{
	public static void main(String[] args)
	{
		system.out.println(“hello world!!!”);
	}
}

Here is a first program, when it is run(I will tell you how to do this in a minute) it will write the line of text “hello world!!!” to the screen.

Compiling and running your first program

To compile and run this program you need to do the following steps:
  1. Type out (or copy and paste) the code above into a text editor. Save this file as “helloworld.java” (make sure that the editor doesn't try to add its own extension on the end)
  2. .from command prompt go the folder of the file and type “javac helloworld”, this should produce a file called helloworld.class, it may generate an error message in which case check the code that you typed out. You may have to configure the commands on your computer so that it knows where to find the java and javac commands Alternatively, if you have textpad(from textpad.com) you should be able to go to tools>complie java, if this tool isnt there you may have to add it manually but it still a lot easier that using command prompt(unless you like all that white wrting and makes you think your clever)
  3. once it has been succesfully complied type “java helloworld” into the command prompt, this should run you program. In the case of this program you should see the line of text “hello world!!!” appear in the command window.
  4. If this is successful well done, you have written, compiled and run your first java program. As you go through the tutorial you will see how to use variables, controll struture and objects to make more powerfull programs and eventually a fully working game. Good luck!!!

top^^
Page by David woodford

Web Hosting · Blog · Guestbooks · Message Forums · Mailing Lists
Easiest Website Builder ever! · Build your own toolbar · Free Talking Character · Email Marketing
powered by a free webtools company bravenet.com