Big Java 4

Chapter 4 – Fundamental Data Types

Chapter Goals

Number Types

Primitive Types

Type Description Size
int The integer type, with range -2,147,483,648 (Integer.MIN_VALUE) . . . 2,147,483,647 (Integer.MAX_VALUE) 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 ±10308 and about 15 significant decimal digits 8 bytes
float The single-precision floating-point type, with a range of about ±1038 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

Number Types: Floating-point Types

Self Check 4.1

Which are the most commonly used number types in Java?
.

Self Check 4.2

Suppose you want to write a program that works with population data from various countries. Which Java data type should you use?

Self Check 4.3

Which of the following initializations are incorrect, and why?
  1. int dollars = 100.0;
  2. double balance = 100;

Constants: final

Constants: static final

Syntax 4.1 Constant Declaration

Syntax 4.1 Constant Declaration

ch04/cashregister/CashRegister.java

Your browser does not support the <object> tag.

ch04/cashregister/CashRegisterTester.java

Your browser does not support the <object> tag. Program Run:

Self Check 4.4

What is the difference between the following two statements?
final double CM_PER_INCH = 2.54;
and
public static final double CM_PER_INCH = 2.54;

Self Check 4.5

What is wrong with the following statement sequence?
double diameter = . . .;
double circumference = 3.14 * diameter;

Arithmetic Operators

Increment and Decrement

Integer Division

Integer Division

Example:
final int PENNIES_PER_NICKEL = 5;
final int PENNIES_PER_DIME = 10;
final int PENNIES_PER_QUARTER = 25;
final int PENNIES_PER_DOLLAR = 100;

// Compute total value in pennies
int total = dollars * PENNIES_PER_DOLLAR + quarters * PENNIES_PER_QUARTER
   + nickels * PENNIES_PER_NICKEL + dimes * PENNIES_PER_DIME + pennies;

// Use integer division to convert to dollars, cents
int dollars = total / PENNIES_PER_DOLLAR;
int cents = total % PENNIES_PER_DOLLAR;

Powers and Roots

Analyzing an Expression

Mathematical Methods

Function Returns
Math.sqrt(x) square root
Math.pow(x, y) power xy
Math.exp(x) ex
Math.log(x) natural log
Math.sin(x), Math.cos(x), Math.tan(x) sine, cosine, tangent (x in radians)
Math.round(x) closest integer to x
Math.min(x, y), Math.max(x, y) minimum, maximum

Cast and Round

Syntax 4.2 Cast

Syntax 4.2 Cast

Arithmetic Expressions

Arithmetic Expressions

Self Check 4.6

What is the value of n after the following sequence of statements?
n--;
n++;
n--;

Self Check 4.7

What is the value of 1729 / 100? Of 1729 % 100?

Self Check 4.8

Why doesn't the following statement compute the average of s1, s2, and s3?
double average = s1 + s2 + s3 / 3; // Error

Self Check 4.9

What is the value of Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2)) in mathematical notation?

Self Check 4.10

When does the cast (long) x yield a different result from the call Math.round(x)?

Self Check 4.11

How do you round the double value x to the nearest int value, assuming that you know that it is less than 2 · 109?

Calling Static Methods

Syntax 4.3 Static Method Call

Syntax 4.3 Static Method Call

Self Check 4.12

Why can't you call x.pow(y) to compute xy?

Self Check 4.13

Is the call System.out.println(4) a static method call?

The String Class

Concatenation

Concatenation in Print Statements

Converting between Strings and Numbers

Substrings

Self Check 4.14

Assuming the String variable s holds the value "Agent", what is the effect of the assignment s = s + s.length()?

Self Check 4.15

Assuming the String variable river holds the value "Mississippi ", what is the value of river.substring(1, 2)? Of river.substring(2, river.length() - 3)?

German Keyboard

Thai Alphabet

Chinese Ideographs

Reading Input

ch04/cashregister/CashRegisterSimulator.java

Your browser does not support the <object> tag. Program Run:

Self Check 4.16

Why can't input be read directly from System.in?

Self Check 4.17

Suppose in is a Scanner object that reads from System.in, and your program calls
String name = in.next();
What is the value of name if the user enters John Q. Public?

Reading Input From a Dialog Box

Reading Input From a Dialog Box