Java review

Organized by Java: How to Program chapter.

  1. About Java - history, compilation, etc.
  2. Applications, first program, arithmetic, memory.
  3. Applets, drawing, floating point.
  4. Conditional statements - if, else, while ++, --
  5. Conditional statements - for, do...while, switch, break, continue.
  6. Methods
  7. Arrays
  8. Object-Oriented Programming
  9. Graphical User Interface Components I

Typical Java Compilation/Execution Steps

  1. Edit source (.java) program (eg, your IDE)
  2. Compile source program to byte-codes (.class).
  3. Load into RAM using the Java Class Loader.
    • Verify correctness of byte codes.
    • Allocate stack, heap (classes and statics).
    • Start JVM (Java Virtual Machine) at main (application).
  4. JIT. JVM may translate a method from byte code to machine code the first time it's called (Just In Time compilation).
  5. Execute the machine code.
  6. The server version of the Java run-time system may recompile from byte code to machine instructions after analyzing the performance (HotSpot).

Implications of Java Byte Code

Java Byte Code (JBC) is interpreted/translated by the Java Virtual Machine (JVM).

Design Patterns

Design patterns are "proven architectures for constructing flexible and maintainable object-oriented software". They:

A First Program with Console Output

Similar to JHTP p 34 without comments

public class Welcome1 {
    public static void main(String[] args) {
        System.out.println("Welcome Earthlings");
    }
}

Program using Dialog Box

// Similar to JHTP p 43 without comments.
import javax.swing.JOptionPane;

public class Welcome2 {
    public static void main(String[] args) {
        JOptionPane.showMessageDialog(null, "Welcome Earthlings");
        
        System.exit(0);
    }
}

I don't expect you to know the answers to these - this time!

Converting Strings to Integers

// Similar to JHTP p 47 without comments.
import javax.swing.JOptionPane;
public class Add2 {
    public static void main(String[] args) {
        String s1, s2;
        int    n1, n2;
        s1 = JOptionPane.showInputDialog(null, "Enter 1st number");
        n1 = Integer.parseInt(s1);
        s2 = JOptionPane.showInputDialog(null, "Enter 2nd number");
        n2 = Integer.parseInt(s2);
        
        JOptionPane.showMessageDialog(null, "Sum is " + (n1 + n2));
        
        System.exit(0);
    }
}

Arithmetic Operators

Basic arithmetic operators: +, -, *, /, %

Precedence: *, /, and % are done before + or -.

If either operand of + is a string, the other operand is converted to string and the two strings are concatenated.

Arithmetic Comparison Operators

Comparison operators: <, <=, ==, !=, >=, >

Precedence: All are lower precedence than the arithmetic operators.
<, <=, >=, > are higher than ==, !=, but there is no apparent reason for this except compatibility with C and C++.

The result of any comparison is boolean true or false.

Logical Operators

End