// APS101, Winter 2009: Lecture 3 (Jan. 13) // // Review: last time we looked at basic data types // Java has 8 primitive types // ex. int, long, short, byte // Real numbers: approximated by floating-point numbers in Java // two types: double and float 5.0 // this is of type double, by default 5.0f // this is how you specify a float // what's the difference? // double has twice as many significant digits as float. // // Today will be all about primitive types and // what you can do with them. // (Next time we'll start talking about non-primitive types.) // // A type is a set of values and a set of operations that // can be performed on those values. // // here is one more primitive type: char 'a' // this is a character literal (char value) // each char has a numeric (ASCII) value... more on ASCII later. // how do you see this value? (int) 'a' // this is a type conversion 'a' + 0 'a' + 1 'a' / 2 'a' + 'b' 'b' / 'a' // char values aren't just letters! '%' '.' ''' // this produces an error - why? '\'' '\"' '\\' // here is the last primitive type: boolean // values: true, false // operations: &&, ||, ! (and, or, not) !true !false ! 4 < 2 // what's the problem? ! (4 < 2) ! (((4) < 2)) // don't need this many parentheses! // && (and) evaluates to true only if both operands are true. true && false false && true false && false true && true // || (or) evaluates to true if at least one of the operands is true. true || false false || true false || false true || true // order of precedence: ! && || true || false && !true // can't really combine boolean with other types true + 5 true && 0 // // Variable: a name with an associated type. // Variable declaration: ; // Variable assignment: = ; int n; n = 5 // initializing a variable int n = 10 // can declare and initialize on one line // "assignment statement": assigning a new value to a variable n = 2 n + 1 // this just prints the value! n = n + 1 // this changes the value stored in variable n x = 1.5 // missing , but no error! Java decides it's a double double y; y // If we declare a double variable, but don't assign a value to it, // then it is initialized to 0.0, by default. int 0abc int )abc // variables can't start with certain characters! int _abc int .abc day = 2.3 // of type double day = 2.3f // enforcing a float // let's say we want to convert float -> int... day = (int 2.3f) day = (int 2.3) day = int 2.3 day = int (2.3) day = 2.3 (int) day = (int) 2.3 // finally works! day = (int) 2.3f // explicitly casting a float into int day = (int) 2.9f // remember that decimal is truncated day = (int) 2.9E201 day = (int) 2.9E-201 day day; // in the "interactions pane", a ; at the end of a statement // means that the result shouldn't be displayed. // // Typecasting: converting a value of one type into // its equivalent value in another type. (double) 15 (int) 71.9f (int) true // boolean type cannot be cast to or from other types! int myInt; double myInt; myInt // can't have multiple variables with the same name, // so the value of the variable is overridden. (char) myInt char c; c // comparison operator: == // don't mix this up with the assignment statement: = 5 == 4 5 == 5 5 == 5.0 5.0 == 5.0f 'a' == 'b' 'a' == 97 !(5 == 6) // "not equals" 5 != 6 // this is a better way to write it // // operator precedence: // Unary + - ! // Binary * / + - // Equality == != < <=, etc. // Logical and: && // Logical or: || // // EXTRA MATERIAL // // modulo operation (or just "mod"), written as % // x mod y gives the remainder of x / y 5 % 2 5 % 5 'a' % 2 'a' % 'b' 'b' % 'a'