Computer Science 15-100, Fall 2008
Class Notes:  Data and Expressions


  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
  3. double values
    1. Print a floating-point value
    2. Use "double" variables
    3. Integer vs Floating-point division
    4. Approximate values of floating-point numbers
    5. "almost equal" comparison of doubles
    6. Some double Math methods (abs, min, max, pow, sqrt)
  4. String values
    1. String literals
    2. Getting the length of a String
    3. null versus empty Strings
    4. String concatenation
    5. Escape Sequences
  5. char values
    1. char literals
    2. char comparisons
  6. Type conversion

Data and Expressions

  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!
        }
      }
  3. double values
     
    1. Print a floating-point value
      class MyCode {
        public static void main(String[] args) {
          System.out.println(1.2);
        }
      }
    2. Use "double" variables
      class MyCode {
        public static void main(String[] args) {
          double x = 1.2;
          System.out.println(2*x);
        }
      }
    3. Integer vs Floating-point division
      class MyCode {
        public static void main(String[] args) {
          int i = 10;
          double d = 10.0;
          System.out.println( 10 / 3 );
          System.out.println(  i / 3 );
          System.out.println(  d / 3 );
          System.out.println( 10 / 3.0 );
          System.out.println(  i / 3.0 );
        }
      }
    4. Approximate values of floating-point numbers
      class MyCode {
        public static void main(String[] args) {
          double d1 = (29.0 / 7.0) * 7.0;
          double d2 = 29.0;
          System.out.println(d1 == d2); // false!
          System.out.println(d2 - d1);  // -3.552713678800501E-15  (tiny!)
        }
      }
    5. "almost equal" comparison of doubles
      class MyCode {
        public static void main(String[] args) {
          double d1 = (29.0 / 7.0) * 7.0;
          double d2 = 29.0;
          System.out.println(d1 == d2); // still false (of course)
      
          // now compare if their difference is very small...
          double epsilon = 0.000001;
          System.out.println(Math.abs(d2 - d1) < epsilon); // true!
        }
      }
    6. Some double Math methods (abs, min, max, pow, sqrt)
      class MyCode {
        public static void main(String[] args) {
          System.out.println(Math.abs(4.2));   // 4.2
          System.out.println(Math.abs(-4.2));  // 4.2
      
          System.out.println(Math.min(3.5, 2.5)); // 2.5
          System.out.println(Math.min(2.5, 3.5)); // 2.5
      
          System.out.println(Math.max(3.5, 2.5)); // 3.5
          System.out.println(Math.max(2.5, 3.5)); // 3.5
      
          // We can use these in expressions, too:
      
          double x = Math.min(5, -13);
          double y = Math.max(x, x/2);
          double z = Math.abs(x) + Math.abs(y);
          System.out.println(x);
          System.out.println(y);
          System.out.println(z);
        }
      }

      Some Math methods (like pow and sqrt) do not have int versions:

      class MyCode {
        public static void main(String[] args) {
          System.out.println(Math.pow(2, 3));   // 8.0 (and not int 8), which is 2^3
          System.out.println(Math.sqrt(9));     // 3.0 which is the square root of 9
          System.out.println(Math.pow(9, 0.5)); // ditto (why?)
        }
      }
  4. String values
     
    1. String literals
      class MyCode {
        public static void main(String[] args) {
          String s = "A"; // use double-quotes, not single-quotes
          System.out.println(s);
      
          s = "";         // Empty strings (of zero length) are allowed!
          System.out.println(s);
      
          s = 'b';        // ERROR! Must use double-quotes!
          System.out.println(s);
       }
      }
    2. Getting the length of a String
      class MyCode {
        public static void main(String[] args) {
          String s = "a";
          System.out.println(s.length());
      
          s = "ab";
          System.out.println(s.length());
      
          s = "";
          System.out.println(s.length());
          System.out.println(s.length() == 0);
       }
      }
    3. null versus empty Strings
      class MyCode {
        public static void main(String[] args) {
          String s = "";  // empty string
          System.out.println(s == null);
          System.out.println(s.length());
      
          s = null;       // null string
          System.out.println(s == null);
          System.out.println(s.length()); // Runtime Error!
        }
      }
    4. String concatenation
      class MyCode {
        public static void main(String[] args) {
          System.out.println("a" + "b");
          System.out.println("a" +  1 );
          System.out.println("a" +  1 + "2");
          System.out.println("a" +  1 +  2 );
          System.out.println( 1  +  2 + "a");
        }
      }
    5. Escape Sequences
      class MyCode {
        public static void main(String[] args) {
          System.out.println("Double-quote: \"");
          System.out.println("Backslash: \\");
          System.out.println("Newline (in brackets): [\n]");
          System.out.println("Tab (in brackets): [\t]");
      
          System.out.println("These items are tab-delimited, 3-per-line:");
          System.out.println("abc\tdef\tg\nhi\tj\\\tk\n---");
        }
      }
  5. char values
     
    1. char literals
      class MyCode {
        public static void main(String[] args) {
          char c = 'A'; // use single-quotes, not double-quotes
          System.out.println(c);
      
          c = '\\';     // Escape sequences work here, too!
          System.out.println(c);
      
          c = "b";      // ERROR! Must use single-quotes!
          System.out.println(c);
      
          c = 'ab';     // ERROR! Only one character allowed!
          System.out.println(c);
      
          c = '';       // ERROR! No empty char literals allowed!
          System.out.println(c);
      
          c = null;     // ERROR! No null char allowed!
          System.out.println(c);
        }
      }
    2. char comparisons
      class MyCode {
        public static void main(String[] args) {
          System.out.println('D' >= 'A');
          System.out.println('D' <= 'Z');
          System.out.println('D' >= 'a');
          System.out.println('D' <= 'z');
      
          System.out.println("Testing for uppercase:");
          char c = 'D';
          System.out.println((c >= 'A') && (c <= 'Z'));
          c = 'd'; 
          System.out.println((c >= 'A') && (c <= 'Z'));
        }
      }
  6. Type conversion

    Quick-reference table (see details below):
     
      to String to int to double to char to boolean
    from
    String
    n/a // use "parse" method
    int i =
      Integer.parseInt(s);
    // use "parse" method
    double d =
      Double.parseDouble(s);
    // use "charAt" method
    char c = s.charAt(0);
    // use "parse" method
    boolean b =
      Boolean.parseBoolean(s);
    from
    int
    // String concatenation
    String s = "" + i;
    n/a // Widening, so assign
    double d = i;
    // Narrowing, so cast
    char c = (char) i;
    n/a
    from
    double
    // String concatenation
    String s = "" + d;
    // Narrowing, so cast
    int i = (int) d;
    n/a n/a n/a
    from
    char
    // String concatenation
    String s = "" + c;
    // Widening, so assign
    int i = c;
    // Widening, so assign
    double d = c;
    n/a n/a
    from
    boolean
    // String concatenation
    String s = "" + b;
    n/a n/a n/a n/a


    Same table, with more details:
     

      to String to int to double to char to boolean
    from
    String
    n/a
    // From String to int:
    // use "parse" method
    String s = "42";
    int i = Integer.parseInt(s);
    System.out.print(i);
    // prints 42
     

    // From String to double:
    // use "parse" method
    String s = "42.0";
    double d = Double.parseDouble(s);
    System.out.print(d);
    // prints 42.0
     

    // From String to char:
    // use "charAt" method
    String s = "ABCD";
    char c = s.charAt(0);
    System.out.print(c);
    // prints A
     

    // From String to boolean:
    // use "parse" method
    String s = "true";
    boolean b = Boolean.parseBoolean(s);
    System.out.print(b);
    // prints true
    from
    int

    // From int to String:
    // String concatenation
    int i = 65;
    String s = "" + i;
    System.out.print(s);
    // prints 65 (as a String)
    n/a
    // From int to double:
    // Widening, so assign
    int i = 3;
    double d = i;
    System.out.print(d);
    // prints 3.0
     

    // From int to char:
    // Narrowing, so cast
    int i = 65;
    char c = (char) i;
    System.out.print(c);
    // prints A (UNICODE 65)
     
    n/a
    from
    double

    // From double to String:
    // String concatenation
    double d = 3.7;
    String s = "" + d;
    System.out.print(s);
    // prints 3.7 (as a String)

    // From double to int:
    // Narrowing, so cast
    double d = 3.7;
    int i = (int) d;
    System.out.print(i);
    // prints 3
     
    n/a n/a n/a
    from
    char

    // From char to String:
    // String concatenation
    char c = 'A';
    String s = "" + c;
    System.out.print(s);
    // prints A (as a String)

    // From char to int:
    // Widening, so assign
    char c = 'A';
    int i = c;
    System.out.print(i);
    // prints 65 (UNICODE 'A')
     

    // From char to double:
    // Widening, so assign
    char c = 'A';
    double d = c;
    System.out.print(d);
    // prints 65.0
    // UNICODE 'A' as a double
     
    n/a n/a
    from
    boolean

    // From boolean to String:
    // String concatenation
    boolean b = true;
    String s = "" + b;
    System.out.print(s);
    // prints true (as a String)
     
    n/a n/a n/a n/a

 


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