Home
Examples
Labs
Assignment
Marks
Midterm
Final Exam
Resources

Writing Java Programs

  • Alogirithm - A set of steps to accomplish a goal or solve a particular problem.  A cake recipe is an example of a cake baking algorithm.


  • Comments & Formatting - Comments and formatting are ignored by the compiler, but they are still very important parts of your program.  Comments describe how your program / algorithm works.  Formatting (spacing and indenting) makes your program easier to read.  It is not enough for your programs to be executable by the computer!  Your source code must also be easy for people to read and understand.
            // In java use the double slash to
            // comment only until the end of
            // the line.
    
            /* Use the slash and star notation
               to comment everything in between
               these comment symbols. */
    
  • Naming your Program - Your source code file and your program must have the same name.  Because java is a case sensitive language, even the capitalisation must be the same.  Your program is the java file that contains the main method.  The name of your program is the word that appears after the word "class" (usually near the top of the source code file).


  • Printing to the Screen - In java we use System.out.println() and System.out.print() to write information to the screen.  We specify what we would like java to display on the screen inside of the brackets.  For example :
            // Print a blank line on the screen
               System.out.println();
    
            // Print the literal String I Love
            // Java on the screen, then start
            // a new line 
               System.out.println("I Love Java");
    
            // Print the value of variable myVar
            // on the screen, and then start a
            // new line 
               System.out.println(myVar);
    
            // Print the literal String Hi on
            // the screen, followed by the value
            // of variable name, and then
            // start a new line 
               System.out.println("Hi " + name);
     
            // Print the literal String GoodBye!
            // on the screen, and DO NOT
            // start a new line 
               System.out.print("GoodBye!");
    
  • Statement Order - Java statements are executed in the order that they appear in your program.  Think carefully about the order of the steps in your algorithm.  Remember, you must declare a variable before you can assign it a value, and you must assign the variable a value before you can use it in an arithmetic expression.


  • Identifiers - When you declare or define something in java (like a variable, method, or class), you must give it a name.  This name is called an identifier.  Java identifiers may contain any letter of the alphabet (lower or upper case), numerical digits (0 through 9), the underscore symbol, and/or the dollar sign.  Although identifiers may contain numbers, they may not start with a numerical digit.

            Valid Identifiers in Java:
                  number
                  userName
                  num1
                  myVar26
                  showUsers22
                  calcHeight

            Invalid Identifiers in Java:
                  number!
                  user-Name
                  1num
                  26_myVar
                  showUsers#22
                  calc Height

  • Keywords - A keyword (sometimes called a reserved word) is a word with a special predefined meaning.  You cannot use keywords for anything other than their intended purpose.  Specifically, you cannot use a keyword as an identifier.  The list below includes most of the keywords in java.  You do not need to memorise this entire list, but you should be familiar with the keywords that you have been using in your programs.

    abstract
    boolean
    break
    byte
    case
    catch
    char
    class
    continue
    default
    do
    double
    else
    extends
    false
    final
    finally
    float
    for
    if
    implements
    import
    instanceof
    int
    interface
    long
    native
    new
    null
    package
    private
    protected
    public
    return
    short
    static
    super
    switch
    synchronized
    this
    throw
    throws
    transient
    true
    try
    void
    volatile
    while

  • Using a Text Editor - You can use any text editor to create and edit java programs.  Just make sure that when you save your source code that it is saved in plain text format with a .java extension.


Running Java Programs

  1. Creating & Editing Java Programs - Use any text editor to create java source code files.  Remember to save source code files with a .java extension.  Make sure that your program and your program file have the exact same name.
  2. Compiling Java Programs - Use the javac command at the command prompt to compile your source code.  The compiler will translate your source code into java byte code.  Java byte code is stored in .class files.
  3. Running Java Programs - Run your java programs using the java virtual machine.  The java virtual machine is an interpreter that executes byte code instructions.  Remember that you cannot run a program until you have compiled it successfully.  Use the java command at the command prompt to run a java program.