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

Info iconThis preview shows pages 1–11. Sign up to view the full content.

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

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

View Full DocumentRight 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
Background 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
Background image of page 3

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

View Full DocumentRight 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
Background 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
Background image of page 5

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

View Full DocumentRight 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.)
Background image of page 6
( typeName ) expression Example: (int) (balance * 100) Purpose: To convert an expression to a different type. Syntax 4.1 Cast
Background image of page 7

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

View Full DocumentRight Arrow Icon
Which are the most commonly used number types in Java? Answer: int and double Self Check 4.1
Background 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
Background image of page 9

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

View Full DocumentRight Arrow Icon
double value x to the nearest int value, assuming that you know that it is less than 2 · 109 ? Answer:
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/27/2009 for the course SC 418262 taught by Professor Chuleerut during the Spring '08 term at Kasetsart University.

Page1 / 54

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

This preview shows document pages 1 - 11. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online