Code Structure & Syntax
Now we have the basic environment set up we can take a look at the code structure of a Java source file and look at some syntax. In this lesson we do this and then run our very first Java program.
A Look At Java Code Structure Top
A Java source file is made up of a class with the code for the class going inside the class definition. The name of the file that we store the source in, which is also known as a compilation unit, should be the same as the class it contains including any capitalisation when the class is public. The reason for this is the Java compiler will use the name of the public class within our program to create the bytecode version of our source file. Before we go into more details of this lets take a look at the structure of a Java source file using a diagram.
So using the diagram above we can note down a few points about a Java source file:
1.If we want to create a class called Hello then by convention and best practice we should save our source file as Hello.java. Always capitalize the first letter of each word used for the class name.
2.The public keyword preceding the name of the class is an access modifier and in this case means the class can be accessed by anyone.
Only one public class per source file is allowed and if present is what we use as the identifer when we come to compile our source file into bytecode.
If there is no public class present we can call a source file any valid identifier we choose.
An empty source file with nothing in it will also compile without error.
3.The source file will then contain the class keyword followed by the name of the class and a set of curly braces.
4.We can specify one or more methods within the curly braces of the class, with each method also having a set of curly braces.
5.The void keyword preceding the name of each method specifies that the method doesn’t return any value. This is known as a return type and is mandatory.
6.We can specify one or more statements within the curly braces of each specified method and each must end with a semi-colon.
There is more to source files than the points raised above but for now this is more than enough to think about.
As a side note Java has a set of coding standards for how to set out your code. Of course with the restrictions of screen sizes the site can’t always adhere to these standards. Also in some tests we put more than one statement on a line which is what you will get if you sit the certification. Use the following link Code Conventions for the Java Programming Language to get a copy of coding standards to get into the habit of using them.
The Java main() Method Top
A Java application is a class or collection of classes that interact with each other to produce the desired results. So how does the application know which class to start off with? Well within our suite of classes one of the classes needs to have a special method called main() which is used to run the application from the command line. This is achieved by typing the java command on the command line followed by the name of the class containing the main() method. Lets take a look at the syntax of the main() method and see how it fits into a class.
There’s a lot of information contained within the diagram above so take your time looking at it and getting a feel for the syntax and how it all hangs together. We will cover things like access modifier, objects and such in later lessons so we don’t overload too much at the moment although it should be noted that it is perfectly valid to declare the main method public static void or static public void.
Creating A Source File, Compiling And Running it Top
I am sure you are itching to get into some coding now, but lets just go through the process of what this entails first:
1.We need to type our source file into a simple text editor, the choice is yours, but we will be using Notepad.
2.Once we have coded the source we need to save the file with the .java extension tagged on the end.
3.Once the coding is complete we need to save and compile our source using the javac command from the command line.
4.Assuming we have a clean compile we need to run the program from the command line using the java command followed by the name of a class containing a main() method. The java.exe intepreter file resides in C:Program FilesJavajdk1.7.0_07bin, which we have already set a classpath to, so the java command will work fine.
Creating A Folder For Our Beginning Java6 Source Files
Lets create a folder for our Beginning Java6 files, in Windows this would be:
double click My Computer icon
double click C: drive (or whatever your local drive is)
right click the mouse in the window
from the drop down menu Select New
click the Folder option and call the folder _BeginningJava6 and press enter.
The Text Editor
All we need to get going is a basic text editor such as Notepad for windows. So for instance using Windows XP:
click Start>>All Programs>>Accessories>>Notepad
With your text editor open, cut and paste the following code into it.
/*
A simple java class that outputs a message to the console
We will save our source file as Hello.java
*/
public class Hello {
// We need a main() method to begin execution of our code
public static void main (String[] args) {
System.out.println(“Hello World”);
}
}
Click file from the toolbar and then click the save option.
In the tab at the top make sure you are pointing to the _BeginningJava6 folder we created above.
In the File name: text area type Hello.java and then click the Save button and close the Notepad.
The .java file extension lets the system know that this is a JAVA file, you must save the file with this extension to be able to compile it with the Java compiler. If you do not save the file with the .java file extension Notepad and other text editors save the file with the .txt file extension. This will stop you compiling the file.
Compiling Our Source File
Open your command line editor:
change to directory cd c:_BeginningJava6
check directory listing (should contain 1 file called Hello.java) dir
compile Hello.java using the java compiler javac Hello.java
check directory listing again (should contain 2 files called Hello.java & Hello.class) dir
Running our Compiled Class
If you followed all the steps above correctly you should now have a file called Hello.class ready to run (this is the compiled bytecode).
If you closed your command line editor, reopen it again:
change to directory if reopened cd c:_BeginningJava6
run Hello.class using the java intepreter java Hello
Hopefully you will see a line of text saying “Hello World”, which we highlighted in yellow for the screenshot, after running the intepreter. This text is output from the program with the statement System.out.println(“Hello World”);. We will go more into syntax like System.out.println when we look at the Java I/O Overview in the API Contents section. For now all you need to know is that System is a predefined class that provides access to the underlying system and out is the output stream connected to the console. The println() method displays the message passed to it on a new line. So when joined together as in our example a message is passed to the console and appears on a new line.
The /* block comment */ allows us to enter comments over multiple lines. The // comment allows us to put a comment before/after code or on a single line.
Code Structure & Syntax Quiz Top
Try the quiz below to test your knowledge of this lesson
Question 1 : Which of the following goes inside a class definition?
· Functions
· Subroutines
· Methods
· Procedures
– Methods go inside a class definition.
Next >>
Status Bar Please select an answer
What’s Next?
Java comes with two types of variables to choose from, primitives and reference. In the next lesson we look at the various types of primitive variables that are available in Java.
<< Getting Started Primitive Variables >>
Java6 Tutor Homepage Top
All the Beginning Java6 lessons are listed below. Click a link to go to that lesson.
Beginning Java6
Getting Started
Code Structure & Syntax
A Look At Java Code Structure
The Java main() Method
Our First Program
Code Structure & Syntax Quiz
Primitive Variables
Method Scope
Operators
Bitwise Operators
Conditional Statements
Loop Statements