Expression Involving Multiple Types in Java Programming | CSIS 110, Study notes of Javascript programming

Material Type: Notes; Professor: Hanks; Class: Intro to Programming in Java; Subject: Computer Science Info Systems; University: Fort Lewis College; Term: Unknown 1989;

Typology: Study notes

Pre 2010

Uploaded on 08/05/2009

koofers-user-hn4-1
koofers-user-hn4-1 🇺🇸

10 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSIS 110 – Lecture 6
Review
Char
Double
Expressions
- We’ve already seen some expressions: BMI, Canadian currency, C to F
- What is value of:
o int expr = 4 + 2 * 5;
o int result = 4 * 3 / 2;
Precedence
Java uses precedence rules to determine the order of evaluation
() nonassociative
Unary +, - Right associative
* / % Left associative
+ - Left Associative
- Use () to override order of evaluation
oa + b + c / 3.0;
o(a + b + c) / 3.0;
- Nested (): innermost parenthesis evaluated first
o(3 * (5 – 2))/4;
- Use () to make order of evaluation explicit
oheight * width / 2.0;
o(height * width) / 2.0;
Associativity
- When multiple operators of same precedence
oInt result = 3 * 5 / 2;
- Even though () not necessary, you can use them to make the expression clear
oInt result = (3*5) / 2;
*** Let's try some examples ***
Expressions involving multiple types
Widening
- What is it?
- Java widens operands if necessary
oCalled widening because it takes smaller types and makes them bigger
- Same thing happens with float and double
pf3
pf4
pf5

Partial preview of the text

Download Expression Involving Multiple Types in Java Programming | CSIS 110 and more Study notes Javascript programming in PDF only on Docsity!

CSIS 110 – Lecture 6

Review Char Double Expressions

  • We’ve already seen some expressions: BMI, Canadian currency, C to F
  • What is value of: o int expr = 4 + 2 * 5; o int result = 4 * 3 / 2; Precedence Java uses precedence rules to determine the order of evaluation () nonassociative Unary +, - Right associative
  • / % Left associative
    • Left Associative
  • Use () to override order of evaluation o a + b + c / 3.0; o (a + b + c) / 3.0;
  • Nested (): innermost parenthesis evaluated first o (3 * (5 – 2))/4;
  • Use () to make order of evaluation explicit o height * width / 2.0; o (height * width) / 2.0; Associativity
  • When multiple operators of same precedence o Int result = 3 * 5 / 2;
  • Even though () not necessary, you can use them to make the expression clear o Int result = (3*5) / 2; *** Let's try some examples *** Expressions involving multiple types Widening
  • What is it?
  • Java widens operands if necessary o Called widening because it takes smaller types and makes them bigger
  • Same thing happens with float and double
  • Widening also used in assignment o Double aDouble = 3 * 7; Narrowing
  • What if expression type is wider than variable type?
  • Java does not do this automatically – we have to tell it to o Text page 72 suggests that narrowing happens automatically – in general, this is not true
  • Can force results to fit in a smaller type than the expression type
  • Use casting o int value = (int) (7.5/2.4); o int value = (int) 7.5 / 2.4; -- Not what you want Look at NextChar.java *** End of Review *** Overflow and underflow
  • Strange things happen if you try to use numbers that are too large Int a = 2000000000; Int b = a + a; System.out.println( b );
  • Remember, the types have a range of valid values
  • Can overflow this range – Java doesn’t tell you
  • Look at Overflow.java
  • Casting can also cause overflow o (byte) 300
  • Can also underflow Variable Assignment We've seen the '=' operator used to initialize the value of variable and constants in declarations. int a = 7; // declare a and give it the value 7 But, the '=' operator is also used in assignment statements, where we can change the value of a variable that has been declared earlier in the program. a = 9; // Change the value of the variable a to 9 The general form of an assignment statement is: = ;

double temp = x; x = y; y = temp; See Swap.java Special Increment and Decrement Operators Counting and adding 1 are common operations in computing. Remember when we counted the number of spaces in a sentence? There was a statement like: count = count + 1; This is such a common operation that Java has special operator for adding one - the increment operator: ++ int i = 4; i++; System.out.println("i = " + i ); The ++ operator can also be used as part of an expression on the RHS of an assignment statement: int a = 3; int b = 7; a = b++; The above ++ operator works like this: The value of the expression is evaluated before incrementing the variable - in this case, the value of the expression is 7. So, the above statements set the value of a to 7, and then increments b to 8. The above ++ occurs after the operator - this is called a postfix increment. There is also a prefix increment operation, where ++ is place in front of the variable: int a = 3; int b = 7; a = ++b; In this case, b is incremented before the expression is evaluated. So, b gets the value 8, and then this value (8) is assigned to a. Look at Increment.java Don't use these operations in arbitrary expressions - it can be very hard to read!

k = i++ * 3 + --j * 2; They can be very hard to read and comprehend! Precedence () None op++ op-- unary +,- Right ++op --op Right (cast) Right

  • / % Left
    • Left assignment = Right Read: Finish reading chapter 2 Appendix B (pages 688-690)