Computer Science 15-100, Fall 2008
Class Notes:  Getting Started


  1. Hello World
  2. Basic Console Output
    1. print vs println
    2. Printing Numbers (not Strings)
  3. Comments
    1. End-of-Line Comments
    2. Block Comments
  4. Errors
    1. Syntax Errors (Compile-Time Errors)
    2. Runtime Errors ("Crash")
    3. Logical Errors (Compiles and Runs, but is Wrong!)

Getting Started

  1. Hello World

    public class HelloWorld {
          public static void main(String[] args) {
              System.out.println("Hello World!");
          }
    }
     
  2. Basic Console Output
     
    1. print vs println
      class MyCode {
        public static void main(String[] args) {
          System.out.print("This is");
          System.out.print("a test");
          System.out.println("that will run, albeit");
          System.out.println();
          System.out.println("with some problems...");
        }
      }
    2. Printing numbers (not Strings)
      class MyCode {
        public static void main(String[] args) {
          System.out.println(1);
          System.out.println(1 + 2);
        }
      }
  3. Comments
     
    1. End-Of-Line Comments
      class MyCode {
        public static void main(String[] args) {
          System.out.println("Howdy!"); // this is an end-of-line comment
                                        // (but not a very good one!)
      
          // The next line is commented out, so it is ignored!
          // System.out.println("This will be ignored");
      
          System.out.println("And howdy on line #2");
        }
      }
    2. Block Comments
      class MyCode {
        public static void main(String[] args) {
          System.out.println("Howdy!");
          /* The preceding comments end at the end of the line, but this
             is a block comment, started with a slash-star, which will continue
             across new lines until a star-slash (on the next line):
          */
          System.out.println("And howdy on line #2");  /* another block comment! */
        }
      }
  4. Errors
     
    1. Syntax Errors (Compile-Time Errors)
      class MyCode {
        public static void main(String[] args) {
          System.out.println("Uh oh!); // ERROR!  missing close-quote
        }
      }
      
      Compiler output:
         MyCode.java:3: unclosed string literal
         System.out.println("Uh oh!); // ERROR! missing close-quote
    2. Runtime Errors ("Crash")
      class MyCode {
        public static void main(String[] args) {
          System.out.println(1/0); // ERROR!  Division by zero!
        }
      }
      
      Runtime (Console) output:
         Exception in thread "main" java.lang.ArithmeticException: / by zero
         at MyCode.main(Foo.java:3)
    3. Logical Errors (Compiles and Runs, but is Wrong!)
      class MyCode {
        public static void main(String[] args) {
          System.out.println("2+2=5"); // ERROR!  Untrue!!!
        }
      }
      
      Runtime (Console) output:
         2+2=5

carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem