Beginnings
And so, we open a door into the wonderful world of Java coding, and of course, the fascinating history of jolly ole England.
During the last Ice Age the British Isles were connected to the mainland of Europe.
To make this statement in Java you can use the code shown below:
- //This code prints a statement about English history.
- class Geograpy {
- public static void main(String[] args) {
- System.out.println("During the last Ice Age the Brittish Isles were connected to the mainland of Europe.");
- }
- }
You can cut and paste the code shown above or type the code into Notepad++. Typing the code is beneficial since you tend to think about what is happening as you are entering each line.
You can achieve the indenting shown in the example by clicking the tab key in Notepad++. Indenting makes your code easier to read and understand.
The list shown here explains what the lines of code mean.
- The // symbol is used to indicate that the code is a comment only. Comments help you and other programmers to understand the code.
- The class is the primary component in all of your Java files. The class must have capitals at the beginning of each word. Curly braces are required to surround the statements within the class. The name of your Java file must always be the same as the class name that you declare.
- This line automatically executes the code shown within its curly braces.
- This is the line that uses the "println" method to print the history statement.
- This curly brace closes the "main" method on line 3.
- This curly brace closes the class declaration on line 2.
When your code is complete: Click File then Save As and save the file to the folder that you created previously. Save the file as: Geography.java. I have saved the training file to c:\javamike.
Once you have the Geography.java file saved, you can click the Command prompt icon in your task bar to open the Command prompt window.
In the Command prompt window you can type: cd\ to get to the c:\ prompt. You can also type cd \filename to get to your folder. To get to the training folder I typed cd \javamike. If you type dir you will see a list of the files in the folder. You should see your Geography.java file. If not, go back to Notepad++ and retry your save.
Before running the program you must compile the program into computer language. Type javac Geography.java. If you have errors in your code you will see error messages. Make the necessary corrections and try again. When you receive no errors, you are ready to run your program. Type dir</em/> and you should see a file named: Geography.class. This is the compiled file. You can now start the program by typing: java Geography.
The output will look like this:
c:\javamike>java Geography
During the last Ice Age the British Isles were connected to the mainland of Europe.