VisageHosting.com
 
Home
 
Resources
- PHP and MySQL (2)
- HTML and CSS (0)
- CGI and Perl (0)
- Java (0)
- JavaScript (1)
- Python (0)
- ASP (1)
- Web Hosting/Servers (14)
 
Articles
- PHP/MySQL (11)
- HTML/CSS (5)
- Other Languages (4)
- Web Hosting (2)
- Computer Tech (7)
 
Forum
 
 
Register
Login

Username:

Password:

 
 
Currently 0 registered users and 6 guests browsing.

29 articles available for viewing.

1317495 total page hits.

Welcome to our newest registered user, choonchan!


Other Languages -> Java



Basic Syntax of a Java Program

- by Coolio on 05/24/06, 9:46PM CEST (1329 Views)



Program Structure

When writing Java programs you are not allowed much freedom, if your code does not adhere to the syntax rules of the language it will not compile and you will have no program. In order to establish some of these basic rules we need to analyse how the code is set out.

The following is a simple program that will output the line “Hello World”:


Code:
// This is the HelloWorld program, it will output the String ‘Hello World’
// Created 21 May, 2006
public class HelloWorld
{
       public static void main(String[] args)
       {
              System.out.println("Hello World");
       }
}


Comments

The first 2 lines of code are comments. Any line that begins with // is a comment and ignored by the compiler. This allows the programmer to leave comments explaining what happens in various parts of the program, making it easier for another person to understand and maintain. Code can be commented over multiple lines by placing it between these tags:


Code:
/**
This is a comment
Spanning multiple lines
*/


Any code between the /** and */ tags is ignored when the program is compiled.

Class Declaration

Most programs consist of a number of different classes, for now though we will just be dealing with one class declaration for the program. From the example shown the class declaration consists of:


Code:
public class HelloWorld


all the way down to the last line of code:


Code:
}


The first line of the class declaration is known as the header, and it basically just sets the name of the program (HelloWorld). Between the next opening bracket { and the last line of code } is where the rest of the program code and methods will be placed.

The next block of the program is the main method declaration, which is the code that is executed when the program first starts.


Code:
public static void main(String[] args)
{
       …
}


For the time being you don’t need to know what all of the words in the method header do, these will be covered in later tutorials. Every statement between the containing brackets is carried out, in this example program it is just one but any number of tasks can be placed in the method.

More on syntax


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


A useful feature of Java is that it has hundreds of predefined methods which allow you to perform complex tasks simply. System.out.println is a good example of this, one of Java’s standard methods which will output a line of text/characters/numbers or whichever parameters you give it to the console window. In this case the parameter was the text “Hello World” (text must be placed in double quotes in order for the compiler to recognise it as text).

You may notice the semi colon at the end of this command. Every command that ‘does something’ must be followed by a semi colon in order for the compiler to know to move on to the next command. Where the class and method declarations give the compiler information about the program they do not actually perform a task, whereas outputting text is an explicit command so must be followed by the semi colon.

The compiler will ignore all white space in the program so how you lay it out is up to you. It is a good exercise trying to cut the program down to just one line of code and removing as many spaces as possible (Note some spaces are needed to separate statements). Doing this demonstrates how the compiler interprets your code and the importance of how an organised layout makes it much easier to interpret the code.

That's all for now, be sure to check on the next article - "Compiling and executing your Program".


Seen this article somewhere else? Report it for plagiarism!

Noticed an error in this article? Tell us what's wrong!

View Comments for this Article

Related Articles

- Prerequisites for Java Programming

- Compiling and Executing Your Java Program

- PHP Syntax (Control Structures): if, else and elseif

- Basic Syntax of PHP

- Basic network diagnostics within the XP command prompt
Rating

5.00/5

1 Votes

Want to rate this article? Register a free account or Login if you already have one.


Comments

Comment by choonchan on 08/10/10, 1:03PM CEST

aFWGRv [a href="http://alajdsiakfgu.com/"]alajdsiakfgu[/a], [url=http://rvbcvjjrusir.com/]rvbcvjjrusir[/url], [link=http://mvjzcrmpukfv.com/]mvjzcrmpukfv[/link], http://zzcaglvhxbjq.com/

Comment by lymanknap on 08/07/10, 4:15AM CEST

3ZRq2f [a href="http://wiykrxwnurbs.com/"]wiykrxwnurbs[/a], [url=http://jgfyfemefvta.com/]jgfyfemefvta[/url], [link=http://ocrarcpwupsl.com/]ocrarcpwupsl[/link], http://olinpymckcpr.com/



Want to add a comment? Register a free account or Login if you already have one.



All content on this site is property of www.juxtaposing.com. Do not copy anything on this site without prior written permission of the site owner.