31
1 Number Types Every value in Java is either: 1. a reference to an object or 2. one of the eight primitive types eight primitive types: a. four integer types b. two floating-point types c. two other

1 Number Types Every value in Java is either: 1.a reference to an object or 2.one of the eight primitive types eight primitive types: a.four integer

Embed Size (px)

Citation preview

Page 1: 1 Number Types  Every value in Java is either: 1.a reference to an object or 2.one of the eight primitive types  eight primitive types: a.four integer

1

Number Types

Every value in Java is either: 1. a reference to an object or2. one of the eight primitive types

eight primitive types: a. four integer typesb. two floating-point typesc. two other

Page 2: 1 Number Types  Every value in Java is either: 1.a reference to an object or 2.one of the eight primitive types  eight primitive types: a.four integer

2

Primitive TypesType Numeric Range and Precision Memory

Whole Numbers

byte -128 … 127 1 byte (8 bits)

short -32,768 … 32,767 2 bytes (16 bits)

int -2,147,483,648 … 2,147,483,647(little over 2 billion)

4 bytes (32 bits)

long -9,223,372,036,854,775,808 … 9,223,372,036,854,775,807(just under 19 digits)

8 bytes (64 bits)

Decimal Numbers

float 7 digits of precision, exponent of 38, i.e., 1.234567*1038 4 bytes (32 bits)

double 15 digits of precision, exponent of 308 8 bytes (64 bits)

Other Stuff

boolean 0 is false, 1 is true 1 bit

char Unicode: potential to represent over 32,000 characters 2 Bytes (16 bits)

Page 3: 1 Number Types  Every value in Java is either: 1.a reference to an object or 2.one of the eight primitive types  eight primitive types: a.four integer

3

Number Literals

Any number that appears in your code:

If it has a decimal, it is floating point (double)

If not, it is an integer (int)

Page 4: 1 Number Types  Every value in Java is either: 1.a reference to an object or 2.one of the eight primitive types  eight primitive types: a.four integer

4

Number Literals

Page 5: 1 Number Types  Every value in Java is either: 1.a reference to an object or 2.one of the eight primitive types  eight primitive types: a.four integer

5

Overflow

The result of a computation exceeds the range for the number type

Example

int n = 1000000;System.out.println(n * n); // Prints –727379968, which is clearly wrong

Page 6: 1 Number Types  Every value in Java is either: 1.a reference to an object or 2.one of the eight primitive types  eight primitive types: a.four integer

6

Overflow

int n = 1000000;System.out.println(n * n); // Prints –727379968, which is clearly wrong

1012 is larger that the largest int• The result is truncated, wrap around effect• No warning is given

Solution: use long instead, but even long can truncate.

Page 7: 1 Number Types  Every value in Java is either: 1.a reference to an object or 2.one of the eight primitive types  eight primitive types: a.four integer

7

Rounding Errors

Floating-point numbers have limited precision. Not every value can be represented precisely, and roundoff errors can occur.

Example:

double f = 4.35;System.out.println(100 * f); // Prints 434.99999999999994

Page 8: 1 Number Types  Every value in Java is either: 1.a reference to an object or 2.one of the eight primitive types  eight primitive types: a.four integer

8

Constants: final

Use symbolic names for all values, even those that appear obvious.

A final variable is a constant • Once its value has been set, it cannot be changed

Named constants make programs easier to read and maintain.

Convention: use all-uppercase names for constants:

final double QUARTER_VALUE = 0.25;

Page 9: 1 Number Types  Every value in Java is either: 1.a reference to an object or 2.one of the eight primitive types  eight primitive types: a.four integer

9

Constants: final

final double QUARTER_VALUE = 0.25;final double DIME_VALUE = 0.1;final double NICKEL_VALUE = 0.05;final double PENNY_VALUE = 0.01;

payment = dollars + quarters * QUARTER_VALUE +

dimes * DIME_VALUE + nickels * NICKEL_VALUE +

pennies * PENNY_VALUE;

Page 10: 1 Number Types  Every value in Java is either: 1.a reference to an object or 2.one of the eight primitive types  eight primitive types: a.four integer

10

Constants: static final

If constant values are needed in several methods, • Put them with your instance variables• Tag them as static and final

The static reserved word means that the constant belongs to the class

Give static final constants public access to enable other classes to use them.

Page 11: 1 Number Types  Every value in Java is either: 1.a reference to an object or 2.one of the eight primitive types  eight primitive types: a.four integer

11

Constants: static final

Declaration of constants in the Math classpublic class Math{

public static final double E = 2.7182818284590452354; public static final double PI = 3.14159265358979323846;}

Using a constant

double circumference = Math.PI * diameter;

Page 12: 1 Number Types  Every value in Java is either: 1.a reference to an object or 2.one of the eight primitive types  eight primitive types: a.four integer

12

Section_1/CashRegister.java 4 public class CashRegister 5 { 6 public static final double QUARTER_VALUE = 0.25; 7 public static final double DIME_VALUE = 0.1; 8 public static final double NICKEL_VALUE = 0.05; 9 public static final double PENNY_VALUE = 0.01; 10 11 private double purchase; 12 private double payment; 13

17 public CashRegister() 18 { 19 purchase = 0; 20 payment = 0; 21 } 22

Continued

Page 13: 1 Number Types  Every value in Java is either: 1.a reference to an object or 2.one of the eight primitive types  eight primitive types: a.four integer

13

Section_1/CashRegister.java 27 public void recordPurchase(double amount) 28 { 29 purchase = purchase + amount; 30 } 31

40 public void receivePayment(int dollars, int quarters, 41 int dimes, int nickels, int pennies) 42 { 43 payment = dollars + quarters * QUARTER_VALUE 44 + dimes * DIME_VALUE +

nickels * NICKEL_VALUE 45 + pennies * PENNY_VALUE; 46 } 47

Continued

Page 14: 1 Number Types  Every value in Java is either: 1.a reference to an object or 2.one of the eight primitive types  eight primitive types: a.four integer

14

Section_1/CashRegister.java 47 /** 48 Computes the change due and resets the machine for the next

customer. 49 @return the change due to the customer 50 */ 51 public double giveChange() 52 { 53 double change = payment - purchase; 54 purchase = 0; 55 payment = 0; 56 return change; 57 } 58 }

Page 15: 1 Number Types  Every value in Java is either: 1.a reference to an object or 2.one of the eight primitive types  eight primitive types: a.four integer

15

Section_1/CashRegisterTester.java 4 public class CashRegisterTester 5 { 6 public static void main(String[] args) 7 { 8 CashRegister register = new CashRegister(); 9 10 register.recordPurchase(0.75); 11 register.recordPurchase(1.50); 12 register.receivePayment(2, 0, 5, 0, 0); 13 System.out.print("Change: "); 14 System.out.println(register.giveChange()); 15 System.out.println("Expected: 0.25"); 16 17 register.recordPurchase(2.25); 18 register.recordPurchase(19.25); 19 register.receivePayment(23, 2, 0, 0, 0); 20 System.out.print("Change: "); 21 System.out.println(register.giveChange()); 22 System.out.println("Expected: 2.0"); 23 } 24 }

Program Run:Change: 0.25Expected: 0.25Change: 2.0Expected: 2.0

Page 16: 1 Number Types  Every value in Java is either: 1.a reference to an object or 2.one of the eight primitive types  eight primitive types: a.four integer

16

Arithmetic Operators Four basic operators:

• addition: + • subtraction: - • multiplication: * • division: /

Expression: combination of variables, literals, operators, and/or method calls

Parentheses control the order of the computation(a + b) / 2

Multiplication and division have a higher precedence than addition and subtractiona + b / 2

Page 17: 1 Number Types  Every value in Java is either: 1.a reference to an object or 2.one of the eight primitive types  eight primitive types: a.four integer

17

Arithmetic Operators Mixing integers and floating-point values in an

arithmetic expression yields a floating-point value

7 + 4.0 is the floating-point value 11.0

Page 18: 1 Number Types  Every value in Java is either: 1.a reference to an object or 2.one of the eight primitive types  eight primitive types: a.four integer

18

Increment and Decrement The ++ operator adds 1 to a variable (increments)

counter++;

// Adds 1 to the variable counter

The -- operator subtracts 1 from the variable (decrements)

counter--;

// Subtracts 1 from counter

Page 19: 1 Number Types  Every value in Java is either: 1.a reference to an object or 2.one of the eight primitive types  eight primitive types: a.four integer

19

Integer Division and Remainder Division works as you would expect, as long as at least

one of the numbers is a floating-point number.

Example: all of the following evaluate to 1.75

7.0 / 4.0 7 / 4.0 7.0 / 4

Page 20: 1 Number Types  Every value in Java is either: 1.a reference to an object or 2.one of the eight primitive types  eight primitive types: a.four integer

20

Integer Division and Remainder If both numbers are integers, the result is an integer.

The remainder is discarded

• 7 / 4 evaluates to 1

Use % operator to get the remainder with (pronounced “modulus”, “modulo”, or “mod”)

• 7 % 4 is 3

Page 21: 1 Number Types  Every value in Java is either: 1.a reference to an object or 2.one of the eight primitive types  eight primitive types: a.four integer

21

Integer Division and Remainder

int pennies = 1729 Obtain the dollars through an integer division by 100

int dollars = pennies / 100; // Sets dollars to 17

To obtain the remainder, use the % operator

int cents = pennies % 100; // Sets cents to 29

Page 22: 1 Number Types  Every value in Java is either: 1.a reference to an object or 2.one of the eight primitive types  eight primitive types: a.four integer

22

Integer Division and Remainder

Page 23: 1 Number Types  Every value in Java is either: 1.a reference to an object or 2.one of the eight primitive types  eight primitive types: a.four integer

23

Powers and Roots Math class contains methods sqrt and pow to compute square

roots and powers

To take the square root of a number, use Math.sqrt; for example

Math.sqrt(x)

To compute xn, you write

Math.pow(x, n)

To compute x2 it is significantly more efficient simply to compute

x * x

Page 24: 1 Number Types  Every value in Java is either: 1.a reference to an object or 2.one of the eight primitive types  eight primitive types: a.four integer

24

Powers and Roots In Java,

can be represented as

b * Math.pow(1 + r / 100, n)

Page 25: 1 Number Types  Every value in Java is either: 1.a reference to an object or 2.one of the eight primitive types  eight primitive types: a.four integer

25

Analyzing an Expression

Page 26: 1 Number Types  Every value in Java is either: 1.a reference to an object or 2.one of the eight primitive types  eight primitive types: a.four integer

26

Mathematical Methods

Page 27: 1 Number Types  Every value in Java is either: 1.a reference to an object or 2.one of the eight primitive types  eight primitive types: a.four integer

27

Mathematical Methods

Page 28: 1 Number Types  Every value in Java is either: 1.a reference to an object or 2.one of the eight primitive types  eight primitive types: a.four integer

28

Converting Floating-Point Numbers to Integers - Cast

The compiler disallows the assignment of a double to an int because it is potentially dangerous • The fractional part is lost• The magnitude may be too large• This is an error

double balance = total + tax;int dollars = balance; // Error: Cannot assign double to int

Page 29: 1 Number Types  Every value in Java is either: 1.a reference to an object or 2.one of the eight primitive types  eight primitive types: a.four integer

29

Converting Floating-Point Numbers to Integers - Cast

Use the cast operator (int) to convert a convert floating-point value to an integer.

double balance = total + tax;int dollars = (int) balance;

Cast discards fractional part You use a cast (typeName) to convert a value to any

different type.

Page 30: 1 Number Types  Every value in Java is either: 1.a reference to an object or 2.one of the eight primitive types  eight primitive types: a.four integer

30

Converting Floating-Point Numbers to Integers - Rounding

Math.round converts a floating-point number to nearest integer:

long rounded = Math.round(balance);

If balance is 13.75, then rounded is set to 14.

Page 31: 1 Number Types  Every value in Java is either: 1.a reference to an object or 2.one of the eight primitive types  eight primitive types: a.four integer

31

Arithmetic Expressions