Small Java Programs

2. Example Source Program


  • Source code file:
    • a text file created by a programmer that contains a Java program
  • Bytecode file:
    • a file created by a Java compiler that contains machine language for the Java virtual machine

Example Source Program

public class Hello
  public static void main ( String[] args )
    System.out.println("Hello World!");

Above is the source program (source file) from chapter 5 The program types the characters Hello World! on the monitor.

The file must be named to match the name of the class, Hello On most computer systems, the upper and lower case characters of the file name are important. (So if the file is named with a small h it will not work). On all computers, upper and lower case inside a Java program are important.

A programming language where upper and lower case make a difference is called case sensitive. Java is case sensitive. Not all programming languages are.

Lines are indented to show the logical structure of the program. This is not required by Java, but makes the program easier to understand. (More on this later.)

The first line

public class Hello

says that this source program defines a class called Hello. A class is a section of a program that describes an object. (A more complete definition will come later on in these notes.) Small programs often consist of just one class. When the program is compiled the compiler creates a file of bytecodes called Hello.class.

Question 2:

Here is the first line of another Java program:

      public class AddUpNumbers   
  1. What should the source file be named?
  2. What is the name of the bytecode file the compiler create