Java

TOPIC 05 – YOUR FIRST JAVA PROGRAM

AP EXTRA

 

 

AP LESSON NOTE

 

 

INDENTING STYLES

 

When indenting, there are different ways that one can organize their code.  This can include different indenting spacing amounts, the use of empty lines and the location of the curly brackets.

The most noticeable difference between styles is the location of placement of the curly brackets.

 

BEST STYLE

 

There is no best style.  Most agree that what is most important is that you remain consistent throughout your code.

 

MR CAMPEAU'S FAVOURITE STYLE

 

The style that is used in this course is known the Allman style.  Mr. Campeau likes this style because it makes code very easy to read.  It also helps students keep better track of all the curly brackets.

 

Essentially, in this style, the curly brackets are always placed on their own lines.  For example:

 

public class FirstProgram

{

   public static void main(String[] args)

   {

      //All code goes here

   }

}

 

AP EXAM STYLE

 

The style used in the AP Exam and in many examples of code online is called the Java K&R style.  It involves placing the opening curly bracket at the end of the previous line instead of on its own line.

 

The example above, when written in Java K&R style, looks like this:

 

public class FirstProgram {

   public static void main(String[] args){

      //All code goes here

   }

}

 

This leads to using less lines which means that printouts of code take less paper.  On the other hand, the brackets no longer line up vertically making it a little more difficult to see where they start and end.