Computer Science 15-110, Spring 2010
Class Notes:  Getting Started


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

Getting Started

  1. Installation
    See the notes on installing Java, DrJava, and JCreator.
     
  2. Hello World

    class HelloWorld {
          public static void main(String[] args) {
              System.out.println("Hello World!");
          }
    }
     
  3. 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);
        }
      }
  4. 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! */
        }
      }
  5. 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
  6. Variables and Expressions

    class MyCode {
      public static void main(String[] args) {
        int x = 5;
        int y = 3;
        int sum = x + y;
        System.out.print(x);
        System.out.print("+");
        System.out.print(y);
        System.out.print("=");
        System.out.println(sum);
      }
    }

    Once again, but more concisely:

    class MyCode {
      public static void main(String[] args) {
        int x = 5;
        int y = 3;
        int sum = x + y;
        System.out.println(x + "+" + y + "=" + sum);
      }
    }

     
  7. Basic Console Input

    import java.util.Scanner;
    class MyCode {
      public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int x = scanner.nextInt();
        int y = scanner.nextInt();
        int sum = x + y;
        System.out.println(x + "+" + y + "=" + sum);
      }
    }

    Once again, but with nicer prompts and more operators:

    import java.util.Scanner;
    class MyCode {
      public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter an integer: ");
        int x = scanner.nextInt();
        System.out.print("Enter another integer: ");
        int y = scanner.nextInt();

        int sum = x + y;
        System.out.println(x + "+" + y + "=" + sum);

        int difference = x - y;
        System.out.println(x + "-" + y + "=" + difference);

        int product = x * y;
        System.out.println(x + "*" + y + "=" + product);

        int quotient = x / y;
        System.out.println(x + "/" + y + "=" + quotient);

        int remainder = x % y;
        System.out.println(x + "%" + y + "=" + remainder);
      }
    }

     

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