Take 1: Java Programming

"... a person does not really understand something until after teaching it to a computer, i.e., expressing it as an algorithm."
          - D. Knuth

Computers are good at following instructions, but not at reading your mind."
          - D. Knuth

Plan for Today

  • Computers and Computer Languages
  • Hardware and Software
  • How Java works
  • Some Simple Java Programs

Some Definitions

Programming Language - a language that is designed to express calculations that can be carried out by a computer.

Program - a set of instructions written in a programming language that can be carried out by a computer to solve a problem.

Program Execution ("running" the program) - the computer carries out the program's instructions.

Machine language (or code) - the low-level language of a computer that consists of each computation's encoding as a sequence of 0s and 1s.

What is a computer?

  • A machine that processes information and performs calculations
  • A computer executes a program, a detailed set of instructions that tell the computer what to do to solve a particular problem
  • Hardware - the mechanical and electronic parts of the computer
  • Software - the programs that tell the hardware what to do
  • Components:
    • Central Processing Unit (CPU) - brain of the computer. The CPU repeatedly fetches the next instruction from the computer's memory and executes it. The CPU controls the other components, and performs all calculations, such as addition and subtraction and logical comparisons.
    • Primary or Main Memory - storage used to store data and programs. Main memory consists of electronic components called integrated circuit chips, and it is very fast. Information stored in main memory is lost when the computer's power is turned off.
    • Secondary Storage - devices used for long-term, permanent storage. E.g. magnetic disks, magnetic tapes, CDs. Relatively slow, but data is not lost when computer is turned off and large amounts of data can be stored.
    • Input Devices - devices that bring data into the computer. E.g. keyboard, mouse, scanner.
    • Output Devices - devices that display the information stored in the computer. E.g. Speakers, printer, monitor.

Programming Languages

  • High-level Languages
    • Many: Java, C++, ML, Lisp, Python, etc.
    • statements look a lot like English
    • easily read and understood by humans
    • computers do NOT understand high-level languages
    • Snippet of a program in a high-level language:
                 while(sum < 1000)
                    number = number + 1;
                    sum = sum + number;

  • Machine Language
    • instructions now in form of sequences of 0s and 1s 
    • 0s and 1s correspond to off and on in electronic devices
    • Each chip (PowerPC, etc) has its own machine language that it understands
    • Programs called translators convert high-level languages to machine/object code
      • A compiler translates an entire high-level program into machine language
    • In machine language, a program could look like this:


      where a sequence of 0s and 1s (called bits) correspond to operations such as Add, Store, Get Instruction, etc. Each computer has its own machine language.
  • Assembly Language
    • mnemonics for machine language instructions
    • ADD, STORE, GET INSTR, etc.
    • A little easier to read and understand than sequences of 0s and 1s, but still not very readable (for humans).

How Java Works
"source" code in Java --> compile -->
bytecode --> start up JVM -->
Program output-
problem solution

    .java file                                                 .class file                   

A Java Program

public class Hello {
    public static void main(String[] args) {
        System.out.println("Hello, world!");
        System.out.println("This program produces");
        System.out.println("four lines of output");

    Hello, world!

    This program produces
    four lines of output

Java Program Structure

public class SomeJavaClass
   public static void main(String[] args)

This class is stored in a source file called SomeJavaClass.java.

An application program: one or more classes + (a single main) method (in one of the classes).

A Java class - one way to think of it: a unit of code that is the basic building block of Java programs.

A method: a named segment of code that can be called or invoked to carry out some operations or actions. 
A statement: a command to be executed.
The main method is where program execution begins.

One method is called in our main method: System.out.println.


Names and identifiers

        public class GangstaRap {


keyword: An identifier that you cannot use because it already has a reserved meaning in Java.

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


Syntax Error Example

1   public class Hello {
2       pooblic static void main(String[] args) {
3           System.owt.println("Hello, world!")_
4       }
5   }

    Hello.java:2: <identifier> expected
        pooblic static void main(String[] args) {
    Hello.java:3: ';' expected
    2 errors


Escape Sequences

Two character sequences that represent other characters

System.out.println("\\hello\nhow\tare \"you\"?\\\\");


how         are "you"?\\

Practice Questions

What is the output of the following println statements?

Write a println statement to produce this output:
/ \ // \\ /// \\\

Question 1: What is an example of secondary storage?
A. monitor
C. USB flash drive

Question 2: Every Java statement must end in:
A.  }
B.  )
C.  ;
D.  .

Question 3: What is the output?
Hello World
C. Hello\nWorld
D. Hello       World