Small Java Programs

This chapter discusses naming and coding conventions as well as reserved words in Java. When you go through this chapter, you'll get some hands-on experience with writing in Java.

12. Longer Example Program


  1. Will this program compile without syntax errors?
    • Yes.
  2. Will this program run?
    • Yes.
  3. Does the program meet the assignment?
    • No — it has a bug.

Longer Example Program

public class Emily
  public static void main ( String[] args )
    System.out.println("A bird came down the walk:");
    System.out.println("He did not know I saw;");
    System.out.println("He bit an angle-worm in halves");
    System.out.println("And ate the fellow, raw.");

Usually bugs are much more difficult to find than the one in the previous program. The longer a program is, the more bugs it is likely to have, and the more difficult it is to find them. It is a good idea to practice with short programs where syntax errors and bugs are more easily seen before moving on to longer programs.

Above is a somewhat longer example program.

The program is much like the "Hello World!" program but the main method has more statements inside of it. Create this program with a text editor, compile and run it.

(Remember that you can use "copy-and-paste" with your program editor to very quickly create this program.) (Or you can use the web pages mentioned at the start of chapter 5.)

Save the program in a file called Compile it. The compiler will create a bytecode file called Emily.class .

Each System.out.println statement writes out the characters inside the quote marks on one line:

A bird came down the walk
He did not know I saw;
He bit an angle-worm in halves
And ate the fellow, raw.

Question 12:

Another method, System.out.print("Some String") writes out the characters inside
the quote marks and then stops without going on the the next line of the monitor.

Imagine that each println in the program is changed to print. What will the program write?