VARIABLES - variable: named memory location (name w/ any valid identifier) - to use, must first declare: associate a name with a type, and allocate memory for it - to know how to allocate memory upon declaration, must provide data type - integers: byte, short, int, long - real numbers: float, double - vary in precision: - range of values represented - amount of memory required (only significant if declaring thousands of variables -- not in this course) - declaration syntax: ; - once declared, cannot change type - must declare a variable before use - cannot declare same variable more than once - no lim on num dif declarations - assignment statement: store a value in a variable - syntax: = ; - don't confuse w/ mathematical equality - initialization: giving a variable its first value - good practice to always do - can do upon declaration: = ; ARITHMETIC EXPRESSIONS - arithmetic operator: designates numerical computation - operand - what the operator operates on - constant, variable, method call, arithmetic expression - binary operator: operates on 2 operands - unary operator: operates on 1 operand - e.g. negation "-" - arithmetic expression - expression involving numerical values - consists of arithmetic operators and operands - division - both operands are ints - fractional part is truncated - "integer division" - one operand is real; result is real - modulo - remainder of division - can use on reals - typically only used on ints - subexpression: nested expression - precedence rules - determine order of evaluation - two or more operators - if operands of same type, result will have that type, too - type casting: convert one data type to another - implicit - numeric promotion - applied to operands of arithmetic expr - operand converted from lower to high precision - assignment conversion - occurs when var & val in assignment statement have mismatching types - only if var has higher precision - explicit - type cast operator - syntax: () - unary operator - precedence > binary SAMPLE JAVA STANDARD CLASSES: STRING - represent textual values - sequence of characters surrounded by quotes - methods - concatenation - create a new string from two existing ones - + - cannot concatenate null CONSTANTS - values of variables may vary with time - constant: lock assigned value assigned to identifier - declaration: final = value; - Java naming convention: only capital letters and underscores - judicious use increases readability - named constant: referred to w/ identifier - literal constant: referred to w/ actual value - default type of integer literal constants is int - specify long by appending l or L - default type of real literal constants is double - specify float by appending f or F - specify double by appending d or D - exponential notation - represents scientific notation - syntax: E - - literal const - may contain decimal point - can use e instead of E - is signed or unsigned int - may suffix entire expression w/ float or double modifiers GETTING NUMERICAL INPUT VALUES - type mismatch: error caused by assigning a given type to a different, incompatible one - different representations in memory - wrapper class: utility that surrounds primitive data with useful methods - Integer class - parseInt method - convert Strings to ints - if can't convert, causes error - concatenation - + for strings - also for strings and numerical - automatically converts numbers to strings, then concatenates - operator overloading - symbol used to represent > 1 operation - compiler determines meaning by context - treat as addition if both operations numerical (else concatenates) - evaluated left to right - result of concatenation is text - DecimalFormat class - specify number of decimal places to display - package java.text STANDARD OUTPUT - standard output window - window w/ black background - appearance varies w/ dev tool used - always functions the same - System class - useful class data values - instance of PrintStream class named out - PrintStream object tied to standard output window - System.out - text sent to appears in standard output window - use to output multiple lines of text - use to output numerical values via conversion to text - standard output: technique of displaying data via System.out - print method - output a value - continue printing from end of currently displayed output - implicitly does type conversion to display numerical values as parameters (for any primitive) - println method - print an argument and skip to next line - typical to use once for each line of output SAMPLE JAVA STANDARD CLASSES, CONT'D STRING, CONT'D - explicit use of new optional - methods - substring VARIABLES CONT'D - reference (object) and primitive (e.g. numerical) data types - primitive actually contains the data; object contains address of object data - declaring primitive creates room to store primitive; declaring reference creates room to store reference - to actually make the object referred to, must use new - actual variable is always same size in memory, but object (even of same class) can require varing amts of mem - assigning one obj var to another - does not create new object - two names for the same object