ch04 - Chapter Four Fundamental Data Types Chapter Goals To...

Info icon This preview shows pages 1–13. Sign up to view the full content.

View Full Document Right Arrow Icon
Chapter Four: Fundamental Data Types
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
To understand integer and floating-point numbers To recognize the limitations of the numeric types To become aware of causes for overflow and roundoff errors To understand the proper use of constants To write arithmetic expressions in Java To use the String type to define and manipulate character strings To learn how to read program input and produce formatted output Chapter Goals
Image of page 2
int: integers, no fractional part 1 , -4 , 0 double : floating-point numbers (double precision) 0.5 , -3.11111 , 4.3E24 , 1E-14 A numeric computation overflows if the result falls outside the range for the number type int n = 1000000; System.out.println(n * n); // prints -727379968 Java: 8 primitive types, including four integer types and two floating point types Number Types
Image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Type Description Size int The integer type, with range -2,147,483,648 . . . 2,147,483,647 4 bytes byte The type describing a single byte, with range -128 . . . 127 1 byte short The short integer type, with range -32768 . . . 32767 2 bytes long The long integer type, with range -9,223,372,036,854,775,808 . . . -9,223,372,036,854,775,807 8 bytes double The double-precision floating-point type, with a range of about ±10 308 and about 15 significant decimal digits 8 bytes float The single-precision floating-point type, with a range of about ±10 38 and about 7 significant decimal digits 4 bytes char The character type, representing code units in the Unicode encoding scheme 2 bytes boolean The type with the two truth values false and true 1 bit Primitive Types
Image of page 4
Rounding errors occur when an exact conversion between numbers is not possible double f = 4.35; System.out.println(100 * f); // prints 434.99999999999994 Java: Illegal to assign a floating-point expression to an integer variable double balance = 13.75; int dollars = balance; // Error Casts: used to convert a value to a different type int dollars = (int) balance; // OK Cast discards fractional part. Continued Number Types: Floating-point Types
Image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
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 Number Types: Floating-point Types (cont.)
Image of page 6
( typeName ) expression Example: (int) (balance * 100) Purpose: To convert an expression to a different type. Syntax 4.1 Cast
Image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Which are the most commonly used number types in Java? Answer: int and double Self Check 4.1
Image of page 8
When does the cast (long) x yield a different result from the call Math.round(x )? Answer: When the fractional part of x is ≥ 0.5 Self Check 4.2
Image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
How do you round the double value x to the nearest int value, assuming that you know that it is less than 2 · 109 ? Answer: By using a cast: (int) Math.round(x) Self Check 4.3
Image of page 10
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; 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; Constants: final
Image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
If constant values are needed in several methods, declare them
Image of page 12
Image of page 13
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern