Computer Science 15-110, Spring 2010
Class Notes:  Data and Expressions (1 of 3)


  1. int values
    1. Declare a variable and use it
    2. Use a variable without declaring it
    3. Declaring and assigning on different lines
    4. Assigning and re-assigning
    5. Using before assigning a value
    6. Using two variables
    7. Declaring two variables on one line
    8. Using two variables in an expression
    9. Operator Precedence  (just as you'd expect)
    10. Overflow
    11. Integer division
    12. The Modulus or Remainder operator (%)
    13. Increment and Decrement Operators (as statements)
    14. Assignment Operators
    15. Some int Math methods (abs, min, max)
  2. boolean values
    1. Boolean literals (true and false)
    2. Equality operators
    3. Relational operators
    4. Boolean operators (and, or, not)
    5. Short-circuit evaluation

Data and Expressions (1 of 3)

  1. int values
     
    1. Declare a variable and use it
      class MyCode {
        public static void main(String[] args) {
          int x = 3;
          System.out.println(x);
        }
      }

      Same, but with a nicer UI

      class MyCode {
        public static void main(String[] args) {
          int x = 3;
          System.out.println("x = " + x);
        }
      }
    2. Use a variable without declaring it
      class MyCode {
        public static void main(String[] args) {
          System.out.println("x = " + x);  // ERROR!  No such variable as x
        }
      }
    3. Declaring and assigning on different lines
      class MyCode {
        public static void main(String[] args) {
          int x;  // declare the variable
          x = 5;  // initialize it (assign it its first value)
          System.out.println("x = " + x);
        }
      }
    4. Assigning and re-assigning
      class MyCode {
        public static void main(String[] args) {
          int x;
          x = 1;
          System.out.println("x = " + x); // prints "x = 1"
          x = 2;
          System.out.println("x = " + x); // prints "x = 2"
        }
      }
    5. Using before assigning a value
      class MyCode {
        public static void main(String[] args) {
          int x;  // declared, but not assigned a value
          System.out.println("x = " + x);  // ERROR!  x is not initialized
        }
      }
    6. Using two variables
      class MyCode {
        public static void main(String[] args) {
          int x = 1;
          int y = 2;
          System.out.println("x = " + x);
          System.out.println("y = " + y);
        }
      }
    7. Declaring two variables on one line
      class MyCode {
        public static void main(String[] args) {
          int x = 1, y = 2;
          System.out.println("x = " + x);
          System.out.println("y = " + y);
        }
      }
    8. Using two variables in an expression
      class MyCode {
        public static void main(String[] args) {
          int x = 1, y = 2;
          System.out.println("x = " + x);
          System.out.println("y = " + y);
          System.out.println("x + y = " + (x + y));
        }
      }

      Same, but with a nicer UI

      class MyCode {
        public static void main(String[] args) {
          int x = 1, y = 2;
          System.out.println("x = " + x);
          System.out.println("y = " + y);
          System.out.println("x + y = "
                             + x + " + " + y
                             + " = " + (x + y));
        }
      }
    9. Operator Precedence  (just as you'd expect)
      class MyCode {
        public static void main(String[] args) {
          System.out.println(2+3*4); // prints 14, not 20
        }
      }
    10. Overflow
      class MyCode {
        public static void main(String[] args) {
          int x = 1*1000*1000*1000;  // 1 billion
          int y = 2*1000*1000*1000;  // 2 billion
          System.out.println(x + y); // Prints -1294967296
        }
      }
    11. Integer division
      class MyCode {
        public static void main(String[] args) {
           System.out.println("20/3 = " + (20/3));
           System.out.println(" 6/3 = " + ( 6/3));
           System.out.println(" 5/3 = " + ( 5/3));
           System.out.println(" 2/3 = " + ( 2/3));
        }
      }
    12. The Modulus or Remainder operator (%)
      class MyCode {
        public static void main(String[] args) {
           System.out.println(" 7%3 = " + (7%3));
           System.out.println(" 6%3 = " + (6%3));
           System.out.println(" 5%3 = " + (5%3));
           System.out.println(" 3%3 = " + (3%3));
           System.out.println(" 2%3 = " + (2%3));
           System.out.println(" 0%3 = " + (0%3));
           System.out.println(" 3%0 = " + (3%0));
        }
      }
    13. Increment and Decrement Operators (as statements)
      class MyCode {
        public static void main(String[] args) {
          int x = 5;
          System.out.println(x); // 5
          x++;
          System.out.println(x); // 6
          ++x;
          System.out.println(x); // 7
          x--;
          System.out.println(x); // 6
          --x;
          System.out.println(x); // 5
        }
      }
    14. Assignment Operators
      class MyCode {
        public static void main(String[] args) {
          int x = 5;
          System.out.println(x); // 5
          x += 2;
          System.out.println(x); // 7
          x *= 2;
          System.out.println(x); // 14
          x %= 9;
          System.out.println(x); // 5
          x /= 2;
          System.out.println(x); // 2
          x -= 5;
          System.out.println(x); // -3
        }
      }
    15. Some int Math methods (abs, min, max)
      class MyCode {
        public static void main(String[] args) {
          System.out.println(Math.abs(4));   // 4
          System.out.println(Math.abs(-4));  // 4
      
          System.out.println(Math.min(3, 2)); // 2
          System.out.println(Math.min(2, 3)); // 2
      
          System.out.println(Math.max(3, 2)); // 3
          System.out.println(Math.max(2, 3)); // 3
      
          // We can use these in expressions, too:
      
          int x = Math.min(5, -13);
          int y = Math.max(x, x/2);
          int z = Math.abs(x) + Math.abs(y);
          System.out.println(x);
          System.out.println(y);
          System.out.println(z);
        }
      }
  2. boolean values
     
    1. Boolean literals ( true , false )
      class MyCode {
        public static void main(String[] args) {
          boolean b = true;
          System.out.println(b);     // prints true
          System.out.println(false); // prints false
        }
      }
    2. Equality operators ( == , != )
      class MyCode {
        public static void main(String[] args) {
          System.out.println(5 == 4);
          System.out.println(5 == 5);
          System.out.println(5 != 4);
          System.out.println(5 != 5);
        }
      }
    3. Relational operators ( < , <= , >= , > )
      class MyCode {
        public static void main(String[] args) {
          System.out.println(5 < 4);
          System.out.println(5 > 4);
          System.out.println(5 < 5);
          System.out.println(5 <= 5);
          System.out.println(5 >= 5);
          System.out.println(5 > 5);
        }
      }
    4. Boolean operators ( && , || , ! )
      class MyCode {
        public static void main(String[] args) {
          System.out.println("&& (AND)");
          System.out.println(true  && true );
          System.out.println(true  && false);
          System.out.println(false && true );
          System.out.println(false && false);
      
          System.out.println("|| (OR)");
          System.out.println(true  || true );
          System.out.println(true  || false);
          System.out.println(false || true );
          System.out.println(false || false);
      
          System.out.println("! (NOT)");
          System.out.println( !true );
          System.out.println( !false );
        }
      }
    5. Short-circuit evaluation
      class MyCode {
        public static void main(String[] args) {
           int x = 0, y = 0;
           System.out.println((y != 0) && ((x/y) != 0)); // Works!
           System.out.println(((x/y) != 0) && (y != 0)); // Crashes!
        }
      }

      Once again, using the "or" operator ( || )

      class MyCode {
        public static void main(String[] args) {
           int x = 0, y = 0;
           System.out.println((y == 0) || ((x/y) == 0)); // Works!
           System.out.println(((x/y) == 0) || (y == 0)); // Crashes!
        }
      }

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