Lecture 2-1: Writing Simple Java Programs

After reading through Chapter 2, you should be able to

Today's Learning Objectives

Simple Java Programs

Here is a simple Java program that displays "Programming is great fun" on the computer screen.

Code Example 2-1-1

// Displays the message "Programming is great fun"
   public class Simple  
   {
      public static void  main (String[] args) 
      {

         System.out.println("Programming is great fun");
      }
   }

Building blocks of a Java program

Notes

Writing simple Java Programs

Steps involved in writing a Java program:

  1. Enter (or Edit) the program (Java source code *.java)
  2. Compile the program (Java bytecode *.class)
  3. Execute the program (Java Virtual Machine)
  4. Debug the program (return to step 1)

Programming process illustration

While it is possible to perform all these steps at the command prompt, most programmers prefer to use an integrated development environment (IDE) that provides a Graphical User Interface (GUI also known as WIMP) to the four steps involved in writing Java programs.

The computers in the Hebeler labs have jGRASP IDE installed and ready to use with CS 110 and CS 111 assignments. jGRASP is available online with tutorials to help first time users.

All but the simplest Java programs consist of multiple source code files (*.java) and class files (*.class). jGRASP's projects manage the multiple source code and class files that make up a Java program.

Programming Style

Programming style refers to the coding standards, conventions, and guidelines for writing correct, high-quality, and maintainable Java code.

The CS 110 Java Programming Style Guide outlines the conventions we will follow in CS 110 and 111. It contains separate sections for identifier naming conventions, commenting, and formatting.

If you are brand new to programming, some of the content of the Java Programming Style Guide at his point may be confusing. One student learning objective of CS 110 is to have you leave knowing how to correctly apply the style guidelines that produce readable and maintainable Java programs.

Variables

Variables are names for values in our programs that can be changed.

In Java, every variable must be declared once before it can be used.

Declaring a variable means informing the compiler of the variable's name and its type.

The two most common primitive types in Java are

In Java, the pattern to declare (designate) a name with a variable is

    <the type> <the name>;

For example:

Code Example 2-1-2

 // Declare two variables
    int milesWalked;
    double outsideTemperature;

The Java Style convention for capitalizing variable names is to begin with a lower-case letter and then begin each new word in the name begins with an upper-case letter. Do not use spaces or underscore characters in your variable names.

If you know the variable's initial value, you can also set it to that value at the same time as the variable declaration.

The pattern to assign a name to variable along with an initial value is

    <the type> <the name> = <the initial value>;

For example:

Code Example 2-1-3

// Declare two variables with initial values
   double combinedWeight = 250.7;
   int age = 48;

Named Constants

Named Constants are names for variables whose values cannot be changed

Java uses the keyword final to indicate constants.

In Java, the pattern to assign a name to constant value that cannot be changed is

   final <the type> <the name> = <the constant value>;

Examples:

Code Example 2-1-4

// Declare two constants

   final int MINIMUM_AGE = 21;
   final double PI = 3.14159;

The Java Style convention for capitalizing constant names is to use all capital letters. To improve readability, subsequent words in the name are combined with the underscore character ('_')

URL: http://www.cwu.edu/~schwing/cs110/Java/notes/lecture2-1.html
Title: CS 110: Lecture Notes for Programming Fundamentals 1
Author: Ed Gellenbeck, Central Washington University
Last Updated: Monday, January 16, 2006