Google
 
Site navigation: [ Home | Theory | Java | Moodle courses | Resource wiki | About ]

Some elements of syntax

Big Picture/Little Picture

The approach we are going to take requires you to to think about little pieces of the code at a time.

You probably won't get the whole picture until you have had some practice writing and modifying the example programs.

Some students hate this approach because they really need the big picture - if you are one of these, try the next page before returning here.


Also on this page: [ Common beginner's pitfalls | Using comments in code ]

Declaring objects:

  // A text field, a button, a label
   private TextField name = new TextField("Type your name here!");
   private Button pressMe = new Button("Press me!");
   private Label greeting = new Label("The message will appear here");

Notice the similar stucture here:

  • Each line (statement) ends with a semi-colon.
  • The keyword private is followed by an Object (Class) name in Capitals (eg TextField).
  • The next part (name) is an identifier - we get to choose the name of this (eg name, pressMe).
  • Identifiers start with small letters by convention.
  • new creates a new instance of a Class using the information we supply in brackets.

Back to top

Common Pitfalls for Beginners

If we don't stick to this format (syntax) we will get an error (syntax error) and our code won't compile. Common errors when starting out are:

  • Forgetting the semi-colon at the end of a line (or putting one where it doesn't belong);
  • Not matching curly brackets (braces);
  • Mixing upper and lower case (Name and name are different identifiers);
  • Not putting // together at the beginning of a comment line;

If you are studying alone and don't have an experienced teacher to help out these errors can be extremely frustrating. One possible solution is to cut and paste the code from this page into your editor or download the source files directly by right-button clicking the source link at the top.

Another possible solution is to get in touch with IB Computing who can supply tailor-made courses just for you - but not for free :(

Back to top

Comments

Notice that comments (in blue in the code above) are of two types:

Comments that span several lines can start with /* and end with */. Everything between them is a comment. Comments that start with // last until the end of the line and can even start part way through a line.

    // add places objects on the Applet surface in default layout
     //(we have no control)
 
    add(name);                 // adds the name to the Applet Panel
     add(pressMe);
     add(greeting);

We will use plenty of comments in our code to explain to humans what the code means. Sometimes comments are redundant (like the one we added to the example above) which is bad practice. Sometimes comments are left out altogether which is worse practice. We will say more on this later.

Comments are ignored by the compiler.

Back to top

Related: [ Java Home | previous:PressMe.java | next:Intro to methods ]

Examining Java code from the PressMe example.


 
The site is partly financed by advertising revenue, partly by online teaching activities and partly by donations. If you or your organisation feel these resouces have been useful to you, please consider a donation, $9.95 is suggested. Please report any issues with the site, such as broken links, via the feedback page, thanks.

Questions or problems related to this web site should be addressed to Richard Jones who asserts his right to be identified as the author and owner of these materials - unless otherwise indicated. Please feel free to use the material presented here and to create links to it for non-commercial purposes; an acknowledgement of the source is required by the Creative Commons licence. Use of materials from this site is conditional upon your having read the additional terms of use on the about page and the Creative Commons Licence. View privacy policy.

Creative Commons License


This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License. © 2001 - 2009 Richard Jones, PO BOX 246, Cambridge, New Zealand;
This page was last modified: May 31, 2009