Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:50 645-4739

Similar presentations


Presentation on theme: "CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:50 645-4739"— Presentation transcript:

1 CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:50 645-4739 alphonce@buffalo.edu

2 Agenda Today: –Primitives operator types floating point mixing types type casting and coercion wrapper classes Coming up: –More control structures

3 ‘+’ is overloaded ‘+’ is just a name ‘+’ has different interpretations –String concatenation –integer addition addition of two’s complement numbers –floating point addition addition of IEEE754 numbers

4 type of ‘+’ In the following expression, what is the type of ‘+’? 5 + 2

5 mappings an operator / function maps from a domain set to a range set: DOMAINRANGE

6 ‘+’ as int addition range is int domain is pairs of int ‘+’ has type int x int  int DOMAIN int X int RANGEint

7 int values: 0, 1, -1, 2, -2, … maximum int: 2147483647 = +2 (32-1) -1 minimum int: -2147483648 = -2 (32-1) operations: + - * / % 5+2 = 7+int X int  int 5-2 = 3-int X int  int 5*2 = 10*int X int  int 5/2 = 2 (quotient) /int X int  int 5%2 = 1 (remainder)%int X int  int

8 types of operators for type int Notice that all of these operators take two int arguments, and produce an int result. There is hardware circuitry to perform these operations.

9 double values: 0.0, 1.0, -3.5, 3141.5926535e-3 inexact representation (!) operations: + - * / 5.0 + 2.0 = 7.0+double X double  double 5.0 – 2.0 = 3.0-double X double  double 5.0 * 2.0 = 10.0*double X double  double 5.0 / 2.0 = 2.5/double X double  double

10 floating point types’ representation both double and float use the IEEE754 representation scheme size of representation differs according to type: –the representation of a float is 4 bytes wide –the representation of a double is 8 bytes wide main point: values of different types have different representations – you can’t “mix and match”!

11 Things to watch for! Representation is inexact –e.g.  has a truncated representation –0.1 does not have an exact representation representation is in terms of powers of 2: Mixing magnitudes –it is possible that x+y is the same as x! 1.0e-15 + 1.0e-15  2.0 e-15 1.0e+15 + 1.0e-15  1.0 e+15 (whoops!) Round-off errors – comparisons! double d = 0.1; double pointNine = d+d+d+d+d+d+d+d+d; pointNine is not the same as 0.9 (whoops!)

12 mixing types in expressions Operators such as +, -, * and / are overloaded: the same name has many different values + overloaded as String concatenation too! “Good” + “ ” + “morning!”  “Good morning!” What happens in an expression which mixes values of different types? 5 + 2.5 = ???? 5 is coerced to its equivalent double value, 5.0: 5.0 + 2.5 = 7.5 Type coercion happens only from “smaller” type to “larger” type (e.g. int  double, not double  int)

13 relational operators We can form expressions like: x < y which have a value of true or false (i.e. the type of this expression is boolean) relational operators: >= ==

14 Equality testing Equality testing: –of primitive values: == –of objects: equals method Consider: Foo a = new Foo();Foo c = a; Foo b = new Foo(); what is value ofwhat is value of (a==b)(a==c)


Download ppt "CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:50 645-4739"

Similar presentations


Ads by Google