Download presentation
Presentation is loading. Please wait.
Published byAsher Chandler Modified over 9 years ago
1
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall 645-4739 alphonce@buffalo.edu
2
Primitives in Java Java has eight primitive types –boolean –integral types: signed: long, int, short, byte unsigned: char –floating point types: double, float Values of the primitive types are not objects –no properties –no capabilities
3
boolean values: true, false operations: &&“and” ||“or” !“not”
4
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,int) int 5-2 = 3 -: (int,int) int 5*2 = 10 *: (int,int) int 5/2 = 2 (quotient) /: (int,int) int 5%2 = 1 (remainder)%: (int,int) int
5
integral types’ representations representation used differs according to whether type is signed (byte, short, int, long) or unsigned (char): –signed integral values are represented using “two’s complement” representation –unsigned integral values are represented using “binary” representation size of representation differs according to type: –byte is 1 byte wide (1 byte = 8 bits) –short is 2 bytes wide –int is 4 bytes wide –long is 8 bytes wide main point: values of different types have different representations – you can’t “mix and match”!
6
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.
7
Two’s complement fixed-width encoding limited range of values encodes both negative and non-negative values familiar properties hold –unique representation of zero ( 0 = -0 ) –x + 0 = 0 + x = x –x = - ( -x ) –x + (-x) = 0 –x - y = x + ( -y ) last property lets us use addition circuitry to perform subtraction
8
Bit pattern interpretation half of bit patterns (those with a zero in the leftmost bit) are for non- negative values, and are interpreted just as base 2 (binary) numbers are the assignment of values to the remaining bit patterns is done as described on the following slides
9
-x To find representation of -x given the representation of x: 1.find the one’s complement of x do this by flipping all the bits in the representation (1 becomes 0, 0 becomes 1) 2.find the two’s complement of the result do this by adding one to the one’s complement, ignoring any overflow carry
10
Example Using a 4-bit wide representation, find the representation of -3: –representation: 0011 –one’s complement: 1100 –two’s complement: 1101 Representation of -3 is 1101 Exercise: verify that the desirable properties hold!
11
Extra pattern? since -0 = 0, there is one leftover “negative” bit string let that represent a negative number, -8 in the case of a 4-bit wide representation in general, range of values for a k-bit wide two’s complement representation is from -2 (k-1) to +2 (k-1) -1 for 4-bit wide representation: -8 to +7
12
Rollover What happens when you add 1 to 7 in the 4-bit wide scheme? 0111 + 0001 = 1000 The answer is -8 (!) Adding one to the largest magnitude positive number yields the largest magnitude negative number.
13
Extra negative number The negative of the largest magnitude negative number is itself. In the 4-bit wide scheme, -(-8) is -8.
14
Understand the representation! It is important that you understand the limitations of working with fixed-width representations.
15
‘+’ 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
16
type of ‘+’ In the following expression, what is the type of ‘+’? 5 + 2
17
mappings an operator / function maps from a domain set to a range set: DOMAINRANGE
18
‘+’ as int addition range is int domain is pairs of int ‘+’ has type int x int int DOMAIN int X int RANGEint
19
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: >= == ‘<’ has type int x int boolean
20
Primitives in Java Java has eight primitive types –boolean –integral types: signed: long, int, short, byte unsigned: char –floating point types: double, float Values of the primitive types are not objects –no properties –no capabilities
21
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
22
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”!
23
Things to watch for! Representation is inexact –e.g. in base 10, 1/3 does not have an exact representation –e.g. similarly, has a truncated representation –in base 2, 1/10 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!)
24
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)
25
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: >= == BUT: be careful doing == with floating point numbers!
26
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)
27
type casting a type cast is a function which maps from a value in one type to a corresponding value in another type (i.e. it performs a type conversion) form: ( ) semantics: mapped to example: (int) 2.1 maps to 2 example: (int) 2.9 maps to 2
28
type coercion a coercion is an implicit type conversion example –3.5 + 4 evaluates to 7.5: –4 is coerced to 4.0, then double addition takes place
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.