1. First Java Application

    public class First { public static void main(String[] args) { System.out.println("First Java application"); } }
  2. Error Types

    Error: Syntax Error (Compile-Time Errors)
    public class FirstWithMissingSemicolon { public static void main(String[] args) { System.out.println("First Java application") // Missing a semi colon at the end of line } }
    Compiler output:
    FirstWithMissingSemicolon.java:6: error: ';' expected
    System.out.println("First Java application")
    ^
    1 error

  3. Error: Run-Time Error
    public class HelloErrors { public static void main(String[] args) { System.out.println(1/0); // Division by Zero Error } }
    Runtime (Console) output:
    Exception in thread "main" java.lang.ArithmeticException: / by zero
    at HelloErrors.main(HelloErrors.java:5)

    Error: Logical Error (Compiles & Runs, but is WRONG!)
    public class First { public static void main(String[] args) { System.out.println("2+2=5"); // 2+2 does not equal 5 Untruth! } }
    The statement is False/Untrue.

  4. Adding Comments to a Java Class

    Comment typeDescription
    Line commentsstart with two forward slashes ( // ) and continue to the end of the current line. A line comment can appear on a line by itself or at the end (and to the right) of a line following executable code. Line comments do not require an ending symbol.
    Block commentsstart with a forward slash and an asterisk ( /* ) and end with an asterisk and a forward slash ( */ ). A block comment can appear on a line by itself, on a line before executable code, or on a line after executable code. Block comments also can extend across as many lines as needed.
    Javadoc comments comments are a special case of block comments called documentation comments because they are used to automatically generate nicely formatted program documentation with a program named javadoc. Javadoc comments begin with a forward slash and two asterisks ( /** ) and end with an asterisk and a forward slash ( */ ). Appendix E teaches you how to create javadoc comments.
  5. Java Reserved words

    abstractcontinuefornewswitch
    assertdefaultgotopackagesynchronized
    booleandoifprivatethis
    breakdoubleimplementsprotectedthrow
    byteelseimportpublicthrows
    caseenuminstanceofreturntransient
    catchextendsintshorttry
    charfinalinterfacestaticvoid
    classfinallylongstrictfpvolatile
    constfloatnativesuperwhile
    Everything that you use within a Java program must be part of a class. When you write public class First, you are defining a class for which you have chosen the name First. You can define a Java class using any name or identifier you need, as long as it meets the following requirements: • A Java identifier must begin with a letter of the English alphabet, a non-English letter (such as α or π), an underscore, or a dollar sign. A class name cannot begin with a digit. • A Java identifier can contain only letters, digits, underscores, or dollar signs. • A Java identifier cannot be a reserved keyword, such as public or class. • A Java identifier cannot be one of the following values: true, false, or null.
  6. Creating a Java Application that Produces GUI Output

    import javax.swing.JOptionPane; // Notice Import Statement public class FirstDialog { public static void main(String[] args) { JOptionPane.showMessageDialog(null, "First Java dialog"); // Pop Up Window } }
  7. You Do It Exercises

    // Filename Hello2.java // Written by Joyce Farrell // Written on September 2, 2019 public class Hello2 /* This class demonstrates the use of the println() method to print the message Hello, world! */ { public static void main(String[] args) { System.out.println("Hello, world!"); } }
    public class HelloErrors { public static void main(String[] args) { System.out.println("Hello"); System.out.println("This is a test"); } }
    // Filename HelloDialog.java // Written by Sally Student // Written on September 2, 2019 import javax.swing.JOptionPane; public class HelloDialog { public static void main(String[] args) { JOptionPane.showMessageDialog(null, "Hello, world!"); } }
  8. Case Problems

    public class CarlysMotto2 { public static void main(String[] args) { System.out.println("****************************************************"); System.out.println("* *"); System.out.println("* Carly's makes the food that makes it a party. *"); System.out.println("* *"); System.out.println("****************************************************"); } }
    public class SammysMotto2 { public static void main(String[] args) { System.out.println("SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS"); System.out.println("S S"); System.out.println("S Sammy's makes it fun in the sun. S"); System.out.println("S S"); System.out.println("SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS"); } }