Expressions and Assignment Statements This slide set was compiled from the Absolute Java textbook slides (Walter Savitch) and the professor’s own class materials. CSS161: Fundamentals of Computing
CSS161: Fundamentals of Computing Identifiers The name of a variable or other item (class, method, object, etc.) Must be a~z, A~Z, 0~9, and _. Must not start with 0~9. Can be of any length. Is case-sensitive: Rate, rate, and RATE are the names of three different variables. Excludes keywords and reserved words. public class void static Excludes system-predefined class names System String CSS161: Fundamentals of Computing
CSS161: Fundamentals of Computing Naming Conventions The names of variables, methods, and objects Start with a lowercase letter topSpeed bankRate1 timeOfArrival The names of classes Start with an uppercase letter FirstProgram MyClass String Word boundaries Delimited with an uppercase letter CSS161: Fundamentals of Computing
CSS161: Fundamentals of Computing Practice X 34 count main firstData %growth data-1 numberOfBeans ABC123z7 first.data while PROG.CLASS variable? x_1 data2 variable! variable for x1 5A mainProgram do doIt int Integer integer String string Choose bad identifiers Choose recommended variable names CSS161: Fundamentals of Computing
Variable Declarations Every variable must be declared before it is used. Tell the compiler what kind of data you will be storing in the variable Declare variables either just before they are used or at the start of a block Syntax Type Variable_1, Variable_2, . . .; Examples int count, numberOfDragons, numberOfTrolls; char answer; double speed, distance; CSS161: Fundamentals of Computing
CSS161: Fundamentals of Computing Primitive Types CSS161: Fundamentals of Computing
Assignment Statements with Primitive Types Syntax Variable = Expression; Evaluates the expression of the right-hand side and store the value into the variable of the left-hand side. Example float distance, speed, time; speed = 15; time = 20; distance = speed * time; distance time 15 20 300 15 20 * 300 CSS161: Fundamentals of Computing
Assignment Statements with Primitive Types (Cont’d) Note that a variable can occur on both sides of the assignment operator count = count + 2; The assignment operator is automatically executed from right-to-left, so assignment statements can be chained number2 = number1 = 3; 10 12 10 + 2 3 3 CSS161: Fundamentals of Computing
Variable Initialization Syntax Type Variable_1 [= Expression_1]{, Variable_2 [= Expression_2]}; [..] can be omitted {..} can be repeated 0 or more times. Each variable is declared as well as initialized or left uninitialized. Example int a = 10, b; // initialize a but not b System.out.println( a ); System.out.println( b ); Javac Test.java Test.java:5: variable b might not have been initialized ^ 1 error CSS161: Fundamentals of Computing
Shorthand Assignment Statements The general form is Variable Op = Expression which is equivalent to Variable = Variable Op (Expression) Op can be +, -, *, /, or % Example: Equivalent To: count += 2; count = count + 2; sum -= discount; sum = sum – discount; bonus *= 2; bonus = bonus * 2; time /= rushFactor; time = time / rushFactor; change %= 100; change = change % 100; amount *= count1 + count2; amount = amount * (count1 + count2); CSS161: Fundamentals of Computing
CSS161: Fundamentals of Computing Self-Test Exercises Work on Textbook p20’s 12 ~ 16 with your neighboring classmate. CSS161: Fundamentals of Computing
Assignment Compatibility Illegal int intVarible; intVariable = 2.99; possible loss of precesion found : double required : int ^ Legal/Acceptable double doubleVariable; doubleVariable = 2; intVariable = ( int )2.99 // Type Casting CSS161: Fundamentals of Computing
CSS161: Fundamentals of Computing Type Casting byteshortintlongfloatdouble Char An explicit type cast is required to assign a value of one type to a variable whose type appears to the left of it on the above list (e.g., double to int) The type resulted from arithmetic operations with combined types is adjusted to the highest type. int a = 1; double b = 2.0; doule c = a + b; // int + double -> double CSS161: Fundamentals of Computing
CSS161: Fundamentals of Computing Constants Integer 0, 2, -1, 5, 1000 Decimal-fraction 0.0, 2.0, -1.5, 5.0, 1000.5 Floating-point 3.65e5 (= 3.65 * 105 = 3.65 * 10000 = 36500.0) 5.89e-4 (= 5.89 * 10-4 = 5.89 * 0.0001 = 0.00589) Character ‘A’, ‘0’, ‘Welcome to Java’ String “A”, “0”, “Welcome to Java” CSS161: Fundamentals of Computing
Arithmetic Operations Division / 15.0 / 2 // equals 7.5 15 / 2 // equals 7 int a = 15, b = 2; double c = a / b; // equals 7.0 double c = a / (double)b; // equa;s 7.5 Modulus operator % 15 % 2 = 1; Increment and decrement operators ++, -- int n = 2; int a = 3 * (n++);// a equals 6, n incremented to 3 int b = 3 * (++n);// n incremented to 4, b equals 12 CSS161: Fundamentals of Computing
Arithmetic Operations (Cont’d) Precedence Associativity rules Binary operators: left-to-right order a + b + c equals (a + b) + c Unary operators: right-to-left order +-+a equals +(-(+a)) Assignment operators: right-to-left order a = b = c equals a = (b = c) CSS161: Fundamentals of Computing
CSS161: Fundamentals of Computing Practice Evaluate z, and m1 ~ m5 public class Test { public static void main( String[] args ) { int x = 1, y = 2, z = 3; z += y += x += 5; System.out.println( "z += y += x += 5; z = " + z ); int m1 = 0; x= 1; y = 2; z = 3; m1 = x +(++y); System.out.println( "m1 = x +(++y); m1 = " + m1 ); int m2 = 0; x = 1; y = 2; z = 3; m2 = x +++y; System.out.println( "m2 = x +++y; m2 = " + m2 ); int m3 = 0; x = 1; y = 2; z = 3; m3 = x ---y; System.out.println( "m3 = x ---y; m = " + m3 ); int m4 = 0; x = 1; y = 2; z = 3; m4 = x + +-++z; System.out.println( "m4 = x + +-++z; m4 = " + m4 ); int m5 = 0; x = 1; y = 2; m5 = x *-y; System.out.println( “m5 = x *-y; m5 = " + m5 ); } CSS161: Fundamentals of Computing
CSS161: Fundamentals of Computing Self-Test Exercises Work on Textbook p28’s 17 ~ 21 with your neighboring classmate. Work on Textbook p32’s 22 ~ 23 with your neighboring classmate. CSS161: Fundamentals of Computing
CSS161: Fundamentals of Computing Static Methods A set of statements to repeat the same action Being called from the main( ) static void main( String[] args ) { static void main( String[] args ) { printMessage( ); System.out.println( “D” ); System.out.println( “A” ); System.out.println( “B” ); System.out.println( “C” ); System.out.println( “D” ); } static void printMessage( ) { System.out.println( “A” ); System.out.println( “B” ); System.out.println( “C” ); } } CSS161: Fundamentals of Computing
Static Methods (Cont’d) What will be printed out? public class Test2 { public static void main( String[] args ) { repeatHello( ); repeatBye( ); } static void repeatHello( ) { System.out.println( "hello" ); static void repeatBye( ) { System.out.println( "bye" ); CSS161: Fundamentals of Computing