- variable declaration: associate a name w/ a type - upon declaration, memory allocated - variable: named mem location - can use any valid identifier for name - after declaration, variable can only contain data of declared type - syntax: ; - variables must be declared before use - no limit to the number of declarations - cannot declare the same var more than once - numerical data types - integers: byte, short, int, long - real numbers: float, double - within category, difference is - varying range of values - varying storage requirements - higher precision: larger range of vals (costs more space) - space usage of data types only significant if program extremely large (not in this course) - double generally preffered over float b/c of higher precision - can initialize var when declare - use assignment statement to store values in variables - syntax: = ; - don't confuse with mathematical equality - number variable vs. object variable - number variable actually contains the numerical data; object variable contains address of object data - number variable always the same size; size of object itself may vary, even if of same class - assigning one object variable to another - does not create a new object - means having two distinct ways of referring to something else - create an object with keyword new; declaring only allocates memory for the obj's address - reference data type - object - variable contains address - primitive data types - include numerical - variable contains data itself Quick Check: 1. a) declared a twice b) int is a reserved word, so it can't be used as a name c) must declare different types in separate statements d) big number is not a type, and double is a reserved word, so it cannot be used as an identifier 2. You can't redeclare an identifier, whether or not you change it's type. 3. byte, short, int, long, float, double 4. a, d 5. a) _____ latteAcct | | |_____| _____ espressoAcct | | |_____| b) _____ latteAcct | .- |----\ |_____| __\/______ | | | :Account | | -------- | |__________| _____ espressoAcct | | |_____| c) _____ latteAcct | .- |----\ |_____| __\/______ | | | :Account | | -------- | |__________| _____ espressoAcct | .- |----\ |_____| __\/______ | | | :Account | | -------- | |__________| d) _____ latteAcct | .- |----\ |_____| __\/______ | | | :Account | | -------- | |__________| ^ _____ / espressoAcct | .- |----/ |_____|