// DrJava saved history v2 2.0 5 + 3 // Java has 8 primitive types. 5 / 2 5.0 / 2.0 5 / 2.0 5.0 / 2 1.0 / 3 // A type is a set of values and a set of operations that // can be performed on those values. // So far we've seen type int and type double // here is type float: 1.0f / 3 // type double has twice as many significant digits as float 'a' // 'a' is a character literal (character value) 'a' + 1 'a' + 'b' // each character has a numeric value // 'a' has the value 97 // 'b' has the value 98 'a' + 'b' 'a' / 2 // some values can be converted from one type to another // int, double, float, char // type boolean // values: true, false // operations: &&, ||, ! (and, or, not) !true !false 4 < 2 ! 4 < 2 ! (4 < 2) ! ((4) < 2) ! (((4) < 2)) true && false false && true false && false true && true // && evaluates to true if both operands are true, and // if evaluates to false otherwise true || false false || true false || false true || true // || evaluates to true if at least on of the operands is // true, and evaluates to false otherwise !true && true // order of precendence:! && || true + 8 true && 0 // Three more types: short, long, byte // These are all able to store integer values, but have // smaller ranges than type int. 10000000000 10000000000L Integer.MIN_VALUE Integer.MAX_VALUE Short.MIN_VALUE Short.MAX_VALUE // A non-primitive type: String "hello" "hello" + '!' // + is concatenation "hello everybody!" "hello everybody!" + ' ' int n; // In the statement above, we declared a variable named n, // which can hold int values. n = 8; n n + 1 // Variable: a name with an associated type. // Variable declaration: ; // Variable assignment: = ; double x; x x = 4.5; x + 1 x = 1.2; x + 1 // If we declare a double variable, but don't assign it // a value, then it will have the value 0.0 int mynum mynum int 0abc //varaible name cannot start with a number int a0 iny mySalary int mySalary int day = 2 mySalary day+5 mySalary*day int _sfsafdfldsakjf9320432493214ewj day day = 2.3 day = 2.3f //enforcing a float day = (int 2.3f) day = (int 2.3) day = int 2.3 day = int (2.3) (int 2.3) (int) 2.3 day = (int) 2.3f //explicit casting a float into int day = (int) 2.9f //it is truncated! day = (int) 2.9 //explicit casting a double into int day = (int) 2.9E201 //too big! day int d d =1 day day = 5 day d day = d + 5 d day day day; //in the "interaction pane" a ; at the end of statement mean don't display the result. day