Chapter 3 (in our edition)

Fundamental Data Types


Chapter Goals

Number Types

Primitive Types


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 ±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

Syntax 4.1: Cast

  (typeName) expression

Example:

  (int) (balance * 100)

Purpose:

To convert an expression to a different type

Self Check

  1. Which are the most commonly used number types in Java?
  2. When does the cast (long) x yield a different result from the call Math.round(x)?
  3. How do you round the double value x to the nearest int value, assuming that you know that it is less than 2 · 109?

Answers

  1. int and double
  2. When the fractional part of x is ≥ 0.5
  3. By using a cast: (int) Math.round(x)

Constants: final

Constants: static final

Syntax 4.2: Constant Definition

  In a method:
final typeName variableName = expression ;
 
In a class:
accessSpecifier static final typeName variableName = expression;

Example:

  final double NICKEL_VALUE = 0.05;
public static final double LITERS_PER_GALLON = 3.785;

Purpose:

To define a constant in a method or a class

File CashRegister.java

File CashRegisterTester.java


Output

   Change=0.25
Change=2.0

Self Check

  1. 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;
  2. What is wrong with the following statement?
    double circumference = 3.14 * diameter;

Answers

  1. The first definition is used inside a method, the second inside a class
  2. (1) You should use a named constant, not the "magic number" 3.14
    (2) 3.14 is not an accurate representation of π

Assignment, Increment, and Decrement

Assignment, Increment, and Decrement



Self Check

  1. What is the meaning of the following statement?
    balance = balance + amount;
  2. What is the value of n after the following sequence of statements?
    n--;
    n++;
    n--;

Answers

  1. The statement adds the amount value to the balance variable
  2. One less than it was before

Arithmetic Operations

Arithmetic Operations

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;

The Math class

Mathematical Methods in Java

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 radian)
Math.round(x)
closest integer to x
Math.min(x, y), Math.max(x, y)
minimum, maximum

Analyzing an Expression


Self Check

  1. What is the value of 1729 / 100? Of 1729 % 100?
  2. Why doesn't the following statement compute the average of s1, s2, and s3?
    double average = s1 + s2 + s3 / 3; // Error
  3. What is the value of Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2)) in mathematical notation?

Answers

  1. 17 and 29
  2. Only s3 is divided by 3. To get the correct result, use parentheses. Moreover, if s1, s2, and s3 are integers, you must divide by 3.0 to avoid integer division:
    (s1 + s2 + s3) / 3.0

Calling Static Methods

Syntax 4.3: Static Method Call

  ClassName. methodName(parameters)

Example:

  Math.sqrt(4)

Purpose:

To invoke a static method (a method that does not operate on an object) and supply its parameters

Self Check

  1. Why can't you call x.pow(y) to compute xy?
  2. Is the call System.out.println(4) a static method call?

Answers

  1. x is a number, not an object, and you cannot invoke methods on numbers
  2. No–the println method is called on the object System.out

Strings

Concatenation

Concatenation in Print Statements

Converting between Strings and Numbers

Substrings

Self Check

  1. Assuming the String variable s holds the value "Agent", what is the effect of the assignment s = s + s.length()?
  2. 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)?

Answers

  1. s is set to the string Agent5
  2. The strings "i" and "ssissi"

International Alphabets

International Alphabets

International Alphabets


Reading Input

File InputTester.java


Output

   Enter price: 7.55
   Enter dollars: 10
   Enter quarters: 2
   Enter dimes: 1
   Enter nickels: 0
   Enter pennies: 0
   Your change is 3.05

Reading Input From a Dialog Box



Reading Input From a Dialog Box

Self Check

  1. Why can't input be read directly from System.in?
  2. 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?

Answers

  1. The class only has a method to read a single byte. It would be very tedious to form characters, strings, and numbers from those bytes.
  2. The value is "John". The next method reads the next word.