FUNDAMENTAL DATA TYPES
Transcript of FUNDAMENTAL DATA TYPES
![Page 1: FUNDAMENTAL DATA TYPES](https://reader030.fdocuments.net/reader030/viewer/2022012805/61bd360c61276e740b107388/html5/thumbnails/1.jpg)
Copyright © 2013 by John Wiley & Sons. All rights reserved.
FUNDAMENTAL DATA TYPES
CHAPTER
Slides by Donald W. SmithTechNeTrain.com
Final DraftOct. 15, 2011
2
![Page 2: FUNDAMENTAL DATA TYPES](https://reader030.fdocuments.net/reader030/viewer/2022012805/61bd360c61276e740b107388/html5/thumbnails/2.jpg)
Chapter Goalsq To declare and initialize variables and constantsq To understand the properties and limitations of
integers and floating-point numbersq To appreciate the importance of comments and
good code layoutq To write arithmetic expressions and assignment
statementsq To create programs that read and process inputs,
and display the resultsq To learn how to use the Java String type
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 2
![Page 3: FUNDAMENTAL DATA TYPES](https://reader030.fdocuments.net/reader030/viewer/2022012805/61bd360c61276e740b107388/html5/thumbnails/3.jpg)
Contentsq Variablesq Arithmeticq Input and Outputq Problem Solving:
§ First Do It By Handq Strings
Numbers and character strings (such as the ones in thisdisplay board) are important data types in any Javaprogram. In this chapter, you will learn how to work withnumbers and text, and how to write simple programs thatperform useful tasks with them.
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 3
![Page 4: FUNDAMENTAL DATA TYPES](https://reader030.fdocuments.net/reader030/viewer/2022012805/61bd360c61276e740b107388/html5/thumbnails/4.jpg)
2.1 Variablesq Most computer programs hold temporary
values in named storage locations§ Use variables to store values
• i.e. A variable is a storage location in a computer program
§ Programmers name them for easy accessq There are many different types (sizes) of
storage to hold different things§ You ‘declare’ a variable by telling the compiler:
• What type (size) of variable you need• What name you will use to refer to it
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 4
![Page 5: FUNDAMENTAL DATA TYPES](https://reader030.fdocuments.net/reader030/viewer/2022012805/61bd360c61276e740b107388/html5/thumbnails/5.jpg)
Syntax 2.1: Variable Declarationq When declaring a variable, you often specify an
initial valueq This is also where you tell the compiler the size
(type) it will hold
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 5
typeName variableName = value;
![Page 6: FUNDAMENTAL DATA TYPES](https://reader030.fdocuments.net/reader030/viewer/2022012805/61bd360c61276e740b107388/html5/thumbnails/6.jpg)
An Example: Soda Dealq Soft drinks are sold in cans and bottles. A store offers a
six-pack of 12-ounce cans for the same price as a two-liter bottle. Which should you buy? (12 fluid ounces equal approximately 0.355 liters.)
q List of variables: Type of Number§ Number of cans per pack Whole number§ Ounces per can Whole number§ Ounces per bottle Number with fraction
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 6
• Java supports quite a few data types:• Numbers, text strings, files,
dates, and many others
![Page 7: FUNDAMENTAL DATA TYPES](https://reader030.fdocuments.net/reader030/viewer/2022012805/61bd360c61276e740b107388/html5/thumbnails/7.jpg)
Variables and contentsq Each variable has an identifier (name) and
contentsq Initialization: You can (optionally) set the
contents of a variable when you declare itint cansPerPack = 6;
q Imagine a parking space in a parking garage§ Identifier: J053§ Contents: Bob’s Chevy
6cansPerPack
A variable is a storage location with a nameCopyright © 2013 by John Wiley & Sons. All rights reserved. Page 7
![Page 8: FUNDAMENTAL DATA TYPES](https://reader030.fdocuments.net/reader030/viewer/2022012805/61bd360c61276e740b107388/html5/thumbnails/8.jpg)
Example Declarations
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 8
32 Chapter 2 Fundamental Data Types
Table 1 Variable Declarations in Java
Variable Name Comment
int cans = 6; Declares an integer variable and initializes it with 6.
int total = cans + bottles; The initial value need not be a fixed value. (Of course, cans and bottles must have been previously declared.)
bottles = 1; Error: The type is missing. This statement is not a declaration but an assignment of a new value to an existing variable—see Section 2.1.4.
int volume = "2"; Error: You cannot initialize a number with a string.
int cansPerPack; Declares an integer variable without initializing it. This can be a cause for errors—see Common Error 2.1 on page 37.
int dollars, cents; Declares two integer variables in a single statement. In this book, we will declare each variable in a separate statement.
2.1.2 Number Types
In Java, there are several different types of numbers. You use the int type to denote a whole number with out a fractional part. For example, there must be an integer num-ber of cans in any pack of cans—you can not have a fraction of a can.
When a fractional part is required (such as in the number 0.335), we use floating-point numbers. The most commonly used type for floating-point numbers in Java is called double. (If you want to know the reason, read Special Topic 2.1 on page 39 .) Here is the declaration of a floating-point variable:
double canVolume = 0.335;
Table 2 Number Literals in Java
Number Type Comment
6 int An integer has no fractional part.
–6 int Integers can be negative.
0 int Zero is an integer.
0.5 double A number with a fractional part has type double.
1.0 double An integer with a fractional part .0 has type double.
1E6 double A number in exponential notation: 1 × 106 or 1000000. Numbers in exponential notation always have type double.
2.96E-2 double Negative exponent: 2.96 × 10–2 = 2.96 / 100 = 0.0296
100,000 Error: Do not use a comma as a decimal separator.
3 1/2 Error: Do not use fractions; use decimal notation: 3.5
Use the int type for numbers that cannot have a fractional part.
When a value such as 6 or 0.335 occurs in a Java program, it is called a number literal. If a number literal has a decimal point, it is a floating-point number; otherwise, it is an integer. Table 2 shows how to write integer and floating-point literals in Java.
2.1.3 Variable Names
When you declare a variable, you should pick a name that explains its purpose. For example, it is better to use a descriptive name, such as canVolume, than a terse name, such as cv.
In Java, there are a few simple rules for variable names:
1. Variable names must start with a letter or the underscore (_) character, and the remaining characters must be letters, numbers, or underscores. (Technically, the $ symbol is allowed as well, but you should not use it—it is intended for names that are automatically generated by tools.)
2. You cannot use other symbols such as ? or %. Spaces are not permitted inside names either. You can use uppercase letters to denote word bound aries, as in cansPerPack. This naming convention is called camel case because the uppercase letters in the middle of the name look like the humps of a camel.)
3. Variable names are case sensitive, that is, canVolume and canvolume are differ ent names.
4. You cannot use reserved words such as double or class as names; these words are reserved exclusively for their special Java meanings. (See Appendix C for a listing of all reserved words in Java.)
It is a convention among Java programmers that variable names should start with a lowercase letter (such as canVolume) and class names should start with an uppercase letter (such as HelloPrinter). That way, it is easy to tell them apart.
Table 3 shows examples of legal and illegal variable names in Java.
Table 3 Variable Names in Java
Variable Name Comment
canVolume1 Variable names consist of letters, numbers, and the underscore character.
x In mathematics, you use short variable names such as x or y . This is legal in Java, but not very common, because it can make programs harder to understand (see Programming Tip 2.1 on page 38).
CanVolume Caution: Variable names are case sensitive. This variable name is different from canVolume, and it violates the convention that variable names should start with a lowercase letter.
6pack Error: Variable names cannot start with a number.
can volume Error: Variable names cannot contain spaces.
double Error: You cannot use a reserved word as a variable name.
ltr/fl.oz Error: You cannot use symbols such as / or.
Use the double type for floating-point numbers.
By convention, variable names should start with a lowercase letter.
bjlo_ch02_9.indd 32 10/17/11 4:57 PM
![Page 9: FUNDAMENTAL DATA TYPES](https://reader030.fdocuments.net/reader030/viewer/2022012805/61bd360c61276e740b107388/html5/thumbnails/9.jpg)
Why different types?q Back to the garage analogy, parking spaces may
be different sizes for different types of vehicles§ Bicycle§ Motorcycle § Full Size
§ Electric Vehicle
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 9
![Page 10: FUNDAMENTAL DATA TYPES](https://reader030.fdocuments.net/reader030/viewer/2022012805/61bd360c61276e740b107388/html5/thumbnails/10.jpg)
Why different types?
q There are three different types of variables that we will use in this chapter:
1) A whole number (no fractional part) int2) A number with a fraction part double3) A word (a group of characters) String
q Specify the type before the name in the declarationint cansPerPack = 6;double canVolume = 12.0;
![Page 11: FUNDAMENTAL DATA TYPES](https://reader030.fdocuments.net/reader030/viewer/2022012805/61bd360c61276e740b107388/html5/thumbnails/11.jpg)
Floating-Point Numbersq Java stores numbers with fractional parts as ‘floating point’ numbers.
q They are stored in four parts§ Sign§ Mantissa§ Radix§ Exponent
Sign Mantissa Radix exponent
-1 5 10 0
Parts of a floating point number -5:
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 11
![Page 12: FUNDAMENTAL DATA TYPES](https://reader030.fdocuments.net/reader030/viewer/2022012805/61bd360c61276e740b107388/html5/thumbnails/12.jpg)
q Java has two floating-point types: float and doubleq A ‘double’ is a double-precision floating point
number: It takes twice the storage (52 bit mantissa) as the smaller ‘float’ (23 bit mantissa) See JavaWorld article for more detail
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 12
![Page 13: FUNDAMENTAL DATA TYPES](https://reader030.fdocuments.net/reader030/viewer/2022012805/61bd360c61276e740b107388/html5/thumbnails/13.jpg)
Number Literals in Javaq Sometimes when you just type a number, the
compiler has to ‘guess’ what type it is amt = 6 * 12.0;PI = 3.14;canVol = 0.335;
Use the doubletype for floating-point numbers.
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 13
If a number literal has a decimal point, it is a floating-point number; otherwise, it is an integer
![Page 14: FUNDAMENTAL DATA TYPES](https://reader030.fdocuments.net/reader030/viewer/2022012805/61bd360c61276e740b107388/html5/thumbnails/14.jpg)
All of the Java Numeric Types
q Each type has a range of values that it can hold
Whole Numbers (no fractions)
Floating point Numbers
Characters (no math)
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 14
![Page 15: FUNDAMENTAL DATA TYPES](https://reader030.fdocuments.net/reader030/viewer/2022012805/61bd360c61276e740b107388/html5/thumbnails/15.jpg)
Value Ranges per Typeq Integer Types
§ byte: A very small number (-128 to +127)
§ short: A small number (-32768 to +32767)§ int: A large number (-2,147,483,648 to +2,147,483,647)§ long: A huge number
q Floating Point Types§ float: A huge number with decimal places§ double: Much more precise, for heavy math
q Other Types§ boolean: true or false§ char: One symbol in single quotes ‘a’
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 15
![Page 16: FUNDAMENTAL DATA TYPES](https://reader030.fdocuments.net/reader030/viewer/2022012805/61bd360c61276e740b107388/html5/thumbnails/16.jpg)
Storage per Type ( in bytes)q Integer Types
§ byte:§ short:§ int:§ long:
q Floating Point Types§ float:§ double:
q Other Types§ boolean:§ char:
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 16
8 bits16
3264
1 The actual information represented by a boolean is 1 bit: 0/1 But the actual size of a boolean in memory is JVM dependent.
![Page 17: FUNDAMENTAL DATA TYPES](https://reader030.fdocuments.net/reader030/viewer/2022012805/61bd360c61276e740b107388/html5/thumbnails/17.jpg)
Naming Variablesq Name should describe the purpose
§ ‘canVolume’ is better than ‘cv’q Use These Simple Rules
1) Variable names must start with a letter or the underscore ( _ ) character
• Continue with letters (upper or lower case), digits or the underscore
2) You cannot use other symbols (? or %...) and spaces are not permitted
3) Separate words with ‘camelHump’ notation• Use upper case letters to signify word boundaries
4) Don’t use reserved ‘Java’ words (see Appendix C)Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 17
Case sensitive
•Variable names should start with a lowercase letter (e.g. canVolume)•Class name should start with a upper case letter (e.g. HelloPrinter)
![Page 18: FUNDAMENTAL DATA TYPES](https://reader030.fdocuments.net/reader030/viewer/2022012805/61bd360c61276e740b107388/html5/thumbnails/18.jpg)
Variable Names in Javaq Legal and illegal variable names
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 18
![Page 19: FUNDAMENTAL DATA TYPES](https://reader030.fdocuments.net/reader030/viewer/2022012805/61bd360c61276e740b107388/html5/thumbnails/19.jpg)
The Assignment Statementq Use the ‘assignment statement’ ( with an '='
) to place a new value into a variableint cansPerPack = 6; // declare & initializecansPerPack = 8; // assignment§ First statement:
to create a new variable of type int, to give it the name canPerPack, and to initialize it with 6
§ Second: to replace the contents of the existing variable cansPerPack with another value 8
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 19
![Page 20: FUNDAMENTAL DATA TYPES](https://reader030.fdocuments.net/reader030/viewer/2022012805/61bd360c61276e740b107388/html5/thumbnails/20.jpg)
Assignment Syntaxq Beware: The = sign is NOT used for comparison:
§ The value on the right of the '=' sign is copied to the variable on the left
§ You will learn about the comparison operator in the next chapter
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 20
![Page 21: FUNDAMENTAL DATA TYPES](https://reader030.fdocuments.net/reader030/viewer/2022012805/61bd360c61276e740b107388/html5/thumbnails/21.jpg)
Updating a Variable
q Step by Step:totalVolume = totalVolume + 2;
1.Calculate the right hand side of the assignment Find the value of totalVolume, and add 2 to it
2. Store the result in the variable named on the left side of the assignment operator (totalVolume in this case)
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 21
![Page 22: FUNDAMENTAL DATA TYPES](https://reader030.fdocuments.net/reader030/viewer/2022012805/61bd360c61276e740b107388/html5/thumbnails/22.jpg)
Constantsq When a variable is defined with the reserved word final, its value can never be changedfinal double BOTTLE_VOLUME = 2;
q It is good style to use named constants to explain numerical values to be used in calculations § Which is clearer?
double totalVolume = bottles * 2;double totalVolume = bottles * BOTTLE_VOLUME;
§ A programmer reading the first statement may not understand the significance of the 2
§ Also, if the constant is used in multiple places and needs to be changed, only the initialization changes
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 22
Use UPPERCASE for constants
![Page 23: FUNDAMENTAL DATA TYPES](https://reader030.fdocuments.net/reader030/viewer/2022012805/61bd360c61276e740b107388/html5/thumbnails/23.jpg)
Constant Declaration
q It is customary (not required) to use all UPPER_CASE letters for constants
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 23
final typeName variableName = expression;
![Page 24: FUNDAMENTAL DATA TYPES](https://reader030.fdocuments.net/reader030/viewer/2022012805/61bd360c61276e740b107388/html5/thumbnails/24.jpg)
Big Java by Cay HorstmannCopyright © 2009 by John Wiley & Sons. All rights reserved.
• If constant values are needed in several methods, declare them together with the instance fields of a class and tag them as static and final
• Give static final constants public access to enable other classes to use them public class Math {
. . . public static final double E = 2.7182818284590452354; public static final double PI = 3.14159265358979323846;
}
double circumference = Math.PI * diameter;
Constants: static final
• Static variable (aka class variable): All instances share the same copy of the variable. A class variable can be accessed directly with the class, without the need to create a instance.
![Page 25: FUNDAMENTAL DATA TYPES](https://reader030.fdocuments.net/reader030/viewer/2022012805/61bd360c61276e740b107388/html5/thumbnails/25.jpg)
Big Java by Cay HorstmannCopyright © 2009 by John Wiley & Sons. All rights reserved.
Syntax 4.1 Constant Definition
![Page 26: FUNDAMENTAL DATA TYPES](https://reader030.fdocuments.net/reader030/viewer/2022012805/61bd360c61276e740b107388/html5/thumbnails/26.jpg)
Java Commentsq There are three forms of comments:
1: // single line (or rest of line to right) 2: /*
multi-line – all comment until matching*/
3: /**multi-line Javadoc comments
*/
q Use comments at the beginning of each program, and to clarify details of the code
Use comments toadd explanations forhumans who readyour code. Thecompiler ignorescomments.
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 26
![Page 27: FUNDAMENTAL DATA TYPES](https://reader030.fdocuments.net/reader030/viewer/2022012805/61bd360c61276e740b107388/html5/thumbnails/27.jpg)
Java Comment Example
q Lines 1 - 4 are Javadoc comments for the class Volume1q Lines 10 and 17 use single-line comment to clarify the unit
of measurementCopyright © 2013 by John Wiley & Sons. All rights reserved. Page 27
![Page 28: FUNDAMENTAL DATA TYPES](https://reader030.fdocuments.net/reader030/viewer/2022012805/61bd360c61276e740b107388/html5/thumbnails/28.jpg)
Common Error 2.1q Undeclared Variables
§ You must declare a variable before you use it: (i.e. above in the code)double canVolume = 12 * literPerOunce; // ??double literPerOunce = 0.0296;
q Uninitialized Variables§ You must initialize (i.e. set) a variable’s
contents before you use itint bottles;int bottleVolume = bottles * 2; // ??
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 28
![Page 29: FUNDAMENTAL DATA TYPES](https://reader030.fdocuments.net/reader030/viewer/2022012805/61bd360c61276e740b107388/html5/thumbnails/29.jpg)
Common Error 2.2q Overflow means that storage for a variable
cannot hold the resultint fiftyMillion = 50000000;System.out.println(100 * fiftyMillion);
// Expected: 5000000000Will print out 705032704
q Why?§ The result (5 billion) overflowed int capacity§ Maximum value for an int is +2,147,483,647
q Use a long instead of an int (or a double)
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 29
![Page 30: FUNDAMENTAL DATA TYPES](https://reader030.fdocuments.net/reader030/viewer/2022012805/61bd360c61276e740b107388/html5/thumbnails/30.jpg)
Common Error 2.3q Roundoff Errors
§ Floating point values are NOT exact• This is a limitations of binary values (no fractions):
double price = 4.35;double quantity = 100;double total = price * quantity; // Should be 100 * 4.35 = 435.00
System.out.println(total); // Prints 434.99999999999999
q You can deal with roundoff errors by § rounding to the nearest integer (see Section 2.2.5) § or by displaying a fixed number of digits after the
decimal separator (see Section 2.3.2).
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 30
There is no exact representation for 4.35 in the binary system
![Page 31: FUNDAMENTAL DATA TYPES](https://reader030.fdocuments.net/reader030/viewer/2022012805/61bd360c61276e740b107388/html5/thumbnails/31.jpg)
2.2 Arithmeticq Java supports all of the same basic math as a
calculator:§ Addition +§ Subtraction -
q You write your expressions a bit differently though.. Algebra Java
q Precedence is similar to Algebra:§ PEMDAS
• Parenthesis, Exponent, Multiply/Divide, Add/Subtract
§ Multiplication *§ Division /
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 31
An expression
![Page 32: FUNDAMENTAL DATA TYPES](https://reader030.fdocuments.net/reader030/viewer/2022012805/61bd360c61276e740b107388/html5/thumbnails/32.jpg)
Incrementing a Variable
q Step by Step:counter = counter + 1;
1.Do the right hand side of the assignment first:Find the value stored in counter, and add 1 to it
2. Store the result in the variable named on the left side of the assignment operator (counter in this case)
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 32
![Page 33: FUNDAMENTAL DATA TYPES](https://reader030.fdocuments.net/reader030/viewer/2022012805/61bd360c61276e740b107388/html5/thumbnails/33.jpg)
Shorthand for Incrementingq Incrementing (+1) and decrementing (-1)
integer types is so common that there are shorthand version for each
Long Way Shortcut
counter = counter + 1; counter++ ;
counter = counter - 1; counter-- ;
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 33
![Page 34: FUNDAMENTAL DATA TYPES](https://reader030.fdocuments.net/reader030/viewer/2022012805/61bd360c61276e740b107388/html5/thumbnails/34.jpg)
What is the value of n after the following sequence of statements?
n--; n++; n--;
Answer: One less than it was before.
Self Check
![Page 35: FUNDAMENTAL DATA TYPES](https://reader030.fdocuments.net/reader030/viewer/2022012805/61bd360c61276e740b107388/html5/thumbnails/35.jpg)
Mixing Numeric Typesq It is safe to convert a value from an integer type
to a floating-point type§ No ‘precision’ is lost
q But going the other way can be dangerous§ All fractional information is lost§ The fractional part is discarded (not rounded)
q If you mix types integer and floating-point types in an expression, no precision is lost:double area, pi = 3.14;int radius = 3;area = radius * radius * pi;
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 35
Mixing integers and floating-point values in an arithmetic expression yields a floating-point value.
![Page 36: FUNDAMENTAL DATA TYPES](https://reader030.fdocuments.net/reader030/viewer/2022012805/61bd360c61276e740b107388/html5/thumbnails/36.jpg)
Integer Division and Remainderq When both parts of division are integers, the
result is an integer. § All fractional information is lost (no rounding)int result = 7 / 4;
§ The value of result: 1§ Otherwise7.0 / 4.07 / 4.0
7.0 / 4
The value of result: 1.75q The remainder of dividing two integers, use the %
operator (called modulus):int remainder = 7 % 4;
§ The value of remainder: 3Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 36
Integer division loses all fractional parts of the result and does not round
![Page 37: FUNDAMENTAL DATA TYPES](https://reader030.fdocuments.net/reader030/viewer/2022012805/61bd360c61276e740b107388/html5/thumbnails/37.jpg)
Integer Division and Remainder Examples
q Handy to use for making change:int pennies = 1729;int dollars = pennies / 100; // 17int cents = pennies % 100; // 29
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 37
![Page 38: FUNDAMENTAL DATA TYPES](https://reader030.fdocuments.net/reader030/viewer/2022012805/61bd360c61276e740b107388/html5/thumbnails/38.jpg)
Powers and Rootsq In Java, there are no symbols for power and roots
§ You must call methods§ The Java library declares many Mathematical functions,
such as Math.sqrt (square root) and Math.pow (raising to a power).
• Ex. √x is written as Math.sqrt (x)and xn Is written as Math.pow(x, n)
• Ex.
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 38
b * Math.pow(1 + r / 100, n)
![Page 39: FUNDAMENTAL DATA TYPES](https://reader030.fdocuments.net/reader030/viewer/2022012805/61bd360c61276e740b107388/html5/thumbnails/39.jpg)
Big Java by Cay HorstmannCopyright © 2009 by John Wiley & Sons. All rights reserved.
Syntax 4.3 Static Method Call
![Page 40: FUNDAMENTAL DATA TYPES](https://reader030.fdocuments.net/reader030/viewer/2022012805/61bd360c61276e740b107388/html5/thumbnails/40.jpg)
Big Java by Cay HorstmannCopyright © 2009 by John Wiley & Sons. All rights reserved.
• A static method does not operate on an objectdouble x = 4;double root = x.sqrt(); // Error
• Static methods are declared inside classes
• Naming convention: Classes start with an uppercase letter; Objects start with a lowercase letter:
Calling Static MethodsWill discuss more in later chapters
Should use Math.sqrt
Math
System.out
![Page 41: FUNDAMENTAL DATA TYPES](https://reader030.fdocuments.net/reader030/viewer/2022012805/61bd360c61276e740b107388/html5/thumbnails/41.jpg)
Mathematical Methods
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 41
className.methodname
![Page 42: FUNDAMENTAL DATA TYPES](https://reader030.fdocuments.net/reader030/viewer/2022012805/61bd360c61276e740b107388/html5/thumbnails/42.jpg)
Tip 2.2 Java API Documentationq Lists the classes and methods of the Java API
§ On the web at: https://docs.oracle.com/javase/8/docs/api/
Classes
Packages
Methods
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 42
![Page 43: FUNDAMENTAL DATA TYPES](https://reader030.fdocuments.net/reader030/viewer/2022012805/61bd360c61276e740b107388/html5/thumbnails/43.jpg)
Copyright © 2011 by John Wiley & Sons. All rights reserved. Page 43
![Page 44: FUNDAMENTAL DATA TYPES](https://reader030.fdocuments.net/reader030/viewer/2022012805/61bd360c61276e740b107388/html5/thumbnails/44.jpg)
q https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html
q b * Math.pow(1 + r / 100, n)
Copyright © 2011 by John Wiley & Sons. All rights reserved. Page 44
![Page 45: FUNDAMENTAL DATA TYPES](https://reader030.fdocuments.net/reader030/viewer/2022012805/61bd360c61276e740b107388/html5/thumbnails/45.jpg)
Floating-Point to Integer Conversion
q The Java compiler does not allow direct assignment of a floating-point value to an integer variable
double balance = total + tax;int dollars = balance; // Error
q You can use the ‘cast’ operator: (int) to force the conversion:
double balance = total + tax;int dollars = (int) balance; // no Error
q You lose the fractional part of the floating-point value (no rounding occurs)
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 45
![Page 46: FUNDAMENTAL DATA TYPES](https://reader030.fdocuments.net/reader030/viewer/2022012805/61bd360c61276e740b107388/html5/thumbnails/46.jpg)
Cast Syntax
q To round a floating-point number to the nearest whole number, use the Math.round method § This method returns a long integer, because large
floating-point numbers cannot be stored in an intlong rounded = Math.round(balance);int roundedNum = (int) Math.round(balance);
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 46
(typeName) expression
![Page 47: FUNDAMENTAL DATA TYPES](https://reader030.fdocuments.net/reader030/viewer/2022012805/61bd360c61276e740b107388/html5/thumbnails/47.jpg)
Copyright © 2011 by John Wiley & Sons. All rights reserved. Page 47
![Page 48: FUNDAMENTAL DATA TYPES](https://reader030.fdocuments.net/reader030/viewer/2022012805/61bd360c61276e740b107388/html5/thumbnails/48.jpg)
Arithmetic Expressions
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 48
![Page 49: FUNDAMENTAL DATA TYPES](https://reader030.fdocuments.net/reader030/viewer/2022012805/61bd360c61276e740b107388/html5/thumbnails/49.jpg)
Common Error 2.4q Unintended Integer Division
System.out.print("Please enter your last three test scores: ");
int s1 = in.nextInt();int s2 = in.nextInt()int s3 = in.nextInt();double average = (s1 + s2 + s3) / 3; // Not as expected
q Why?§ All of the calculation on the right happens first
• Since all are ints, the compiler uses integer division§ Then the result (an int) is assigned to the double
q There is no fractional part of the int result, so zero (.0) is assigned to the fractional part of the double
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 49
s1 = 10s2 = 4s3 = 9average = 7.0, not 7.666
![Page 50: FUNDAMENTAL DATA TYPES](https://reader030.fdocuments.net/reader030/viewer/2022012805/61bd360c61276e740b107388/html5/thumbnails/50.jpg)
The Remedy q Make the numerator or denominator into a
floating-point number:double total = score1+ score2+score3;double average = total / 3;
Ordouble average =(score1+score2+score3) / 3.0;
Copyright © 2011 by John Wiley & Sons. All rights reserved. Page 50
double = 23.0So average = 23.0 / 3, not integer division
![Page 51: FUNDAMENTAL DATA TYPES](https://reader030.fdocuments.net/reader030/viewer/2022012805/61bd360c61276e740b107388/html5/thumbnails/51.jpg)
Common Error 2.5q Unbalanced Parenthesis
§ Which is correct?(-(b * b - 4 * a * c) / (2 * a) // 3 (, 2 )-(b * b - (4 * a * c))) / 2 * a) // 2 (, 2 )
q The count of ( and ) must matchq Unfortunately, it is hard for humans to keep track
§ Here’s a handy trick§ Count ( as +1, and ) as -1: Goal: 0-(b * b - (4 * a * c) ) ) / 2 * a)1 2 1 0 -1 -2
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 51
![Page 52: FUNDAMENTAL DATA TYPES](https://reader030.fdocuments.net/reader030/viewer/2022012805/61bd360c61276e740b107388/html5/thumbnails/52.jpg)
2.3 Input and OutputReading Inputq You might need to ask for input (aka prompt for input) and then save what was entered.
§ We will be reading input from the keyboard§ For now, don’t worry about the details (ch 8)
q Output is sent to System.out(but is NOT use System.in for input)
§ To read keyboard input, you can use a class called Scanner, and obtain a Scanner object
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 52
![Page 53: FUNDAMENTAL DATA TYPES](https://reader030.fdocuments.net/reader030/viewer/2022012805/61bd360c61276e740b107388/html5/thumbnails/53.jpg)
Reading InputReading InputqThis is a three step process in Java
1) Import the Scanner class from its ‘package’java.util import java.util.Scanner;
2) Setup an object of the Scanner classScanner in = new Scanner(System.in);
3) Use methods of the new Scanner object to get inputint bottles = in.nextInt();double price = in.nextDouble();
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 53
• Package: a collection of classes with a related purpose.All classes in the Java library are contained in packages.• e.g. The System class belongs to the package java.lang
The Scanner class belongs to the package java.util
Later chapter
![Page 54: FUNDAMENTAL DATA TYPES](https://reader030.fdocuments.net/reader030/viewer/2022012805/61bd360c61276e740b107388/html5/thumbnails/54.jpg)
Syntax 2.3: Input Statementq The Scanner class allows you to read
keyboard input from the user§ It is part of the Java API util package
Java classes are grouped into packages. Use the import statement to use classes from packages.
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 54
![Page 55: FUNDAMENTAL DATA TYPES](https://reader030.fdocuments.net/reader030/viewer/2022012805/61bd360c61276e740b107388/html5/thumbnails/55.jpg)
Big Java by Cay HorstmannCopyright © 2009 by John Wiley & Sons. All rights reserved.
• System.in has minimal set of features — it can only read one byte at a time
• Therefore, in Java 5.0, Scanner class was added to read keyboard input in a convenient manner
• Scanner in = new Scanner(System.in);
System.out.print("Enter quantity:"); int quantity = in.nextInt();
• nextDouble reads a double
• nextLine reads a line (until user hits Enter) • next reads a word (until any white space)
Reading Input
A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace.
![Page 56: FUNDAMENTAL DATA TYPES](https://reader030.fdocuments.net/reader030/viewer/2022012805/61bd360c61276e740b107388/html5/thumbnails/56.jpg)
Formatted Outputq Outputting floating point values can look strange:
Price per liter: 1.21997
q To control the output appearance of numeric variables, use formatted output tools format specifierSystem.out.printf(“%.2f”, price);The output: 1.22
System.out.printf(“%10.2f”, price);The output: 1.22
10 spaces 2 spaces
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 56
Specify the number of digits after the decimal point
Specify a field width
![Page 57: FUNDAMENTAL DATA TYPES](https://reader030.fdocuments.net/reader030/viewer/2022012805/61bd360c61276e740b107388/html5/thumbnails/57.jpg)
Format Typesq Formatting is handy to align columns of output
q You can also include text inside the quotes:System.out.printf(“Price per liter: %10.2f”, price);
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 57
These methods, format and printf, are equivalent to one another.
e.g. System.out.format(“%.2f”, price);
Price per liter: 1.22
![Page 58: FUNDAMENTAL DATA TYPES](https://reader030.fdocuments.net/reader030/viewer/2022012805/61bd360c61276e740b107388/html5/thumbnails/58.jpg)
Format Flagsq You can also use format flags to change the way
text and numeric values are output:
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 58
![Page 59: FUNDAMENTAL DATA TYPES](https://reader030.fdocuments.net/reader030/viewer/2022012805/61bd360c61276e740b107388/html5/thumbnails/59.jpg)
Format Flag Examplesq Left Justify a String:
System.out.printf(“%-10s”, “Total:”);
q Right justify a number with two decimal placesSystem.out.printf(“%10.2f”, price);
q And you can print multiple values:System.out.printf(“%-10s%10.2f”, “Total:”, price);
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 59
![Page 60: FUNDAMENTAL DATA TYPES](https://reader030.fdocuments.net/reader030/viewer/2022012805/61bd360c61276e740b107388/html5/thumbnails/60.jpg)
Volume2.java
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 60
2.3 Input and Output 51
You use the printf method to line up your output in neat columns.
A format string contains format specifiers and literal characters. Any characters that are not format specifiers are printed verbatim. For example, the command
System.out.printf("Price per liter:%10.2f", price);
printsPrice per liter: 1.22
You can print multiple values with a single call to the printf method. Here is a typical example:
System.out.printf("Quantity: %d Total: %10.2f", quantity, total);
The printf method, like the print method, does not start a new line after the output. If you want the next output to be on a separate line, you can call System.out.println(). Alternatively, Section 2.5.4 shows you how to add a newline character to the format string.
Our next example program will prompt for the price of a six-pack and the volume of each can, then print out the price per ounce. The program puts to work what you just learned about reading input and formatting output.
section_3/Volume2.java
1 import java.util.Scanner;2 3 /**4 This program prints the price per ounce for a six-pack of cans.5 */6 public class Volume27 {8 public static void main(String[] args)9 {
Q u a n t i t y : 2 4 :latoT 1 7 . 2 9
Two digits after the decimal point
The printf method does not start a new line here.width 10
No field width was specified, so no padding added
bjlo_ch02_9.indd 51 10/17/11 4:57 PM
![Page 61: FUNDAMENTAL DATA TYPES](https://reader030.fdocuments.net/reader030/viewer/2022012805/61bd360c61276e740b107388/html5/thumbnails/61.jpg)
2.4 Problem Solving: First By Handq A very important step for developing an algorithm
is to first carry out the computations by hand. Example Problem: § A row of black and white tiles needs to be placed along a
wall. For aesthetic reasons, the architect has specified that the first and last tile shall be black.
§ Your task is to compute the number of tiles needed and the gap at each end, given the space available and the width of each tile.
Copyright © 2013 1 by John Wiley & Sons. All rights reserved. Page 61
![Page 62: FUNDAMENTAL DATA TYPES](https://reader030.fdocuments.net/reader030/viewer/2022012805/61bd360c61276e740b107388/html5/thumbnails/62.jpg)
Start with example valuesq GivensTotal width: 100 inchesTile width: 5 inchesq Test your values§ Let’s see… 100/5 = 20, perfect! 20 tiles. No gap.§ But wait… BW…BW “…first and last tile shall be black.”q Look more carefully at the problem….§ Start with one black, then some number of WB pairs
§ Observation: each pair is 2x width of 1 tile• In our example, 2 x 5 = 10 inches
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 62
![Page 63: FUNDAMENTAL DATA TYPES](https://reader030.fdocuments.net/reader030/viewer/2022012805/61bd360c61276e740b107388/html5/thumbnails/63.jpg)
Keep Applying Your SolutionTotal width: 100 inchesTile width: 5 inches
q Calculate total width of all tiles§One black tile: 5”§9 pairs of BWs: 90”§Total tile width: 95”
q Calculate gaps (one on each end)§100 – 95 = 5” total gap§5” gap / 2 = 2.5” at each end
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 63
![Page 64: FUNDAMENTAL DATA TYPES](https://reader030.fdocuments.net/reader030/viewer/2022012805/61bd360c61276e740b107388/html5/thumbnails/64.jpg)
Now Devise an Algorithmq Use your example to see how you calculated
valuesq How many pairs?
§ Note: must be a whole numberInteger part of:
(total width – tile width) / 2 x tile widthq How many tiles?
1 + 2 x the number of pairsq Gap at each end
(total width – number of tiles x tile width) / 2
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 64
![Page 65: FUNDAMENTAL DATA TYPES](https://reader030.fdocuments.net/reader030/viewer/2022012805/61bd360c61276e740b107388/html5/thumbnails/65.jpg)
2.5 Stringsq Text consists of characters: letters, numbers, punctuation, spaces, etc.q A string is a sequence of characters
q The String Type:Type Variable Literal
String name = “Harry”
q Once you have a String variable, you can use methods such as:int n = name.length(); // n will be assigned 5
q A String’s length is the number of characters inside:§An empty String (length 0) is shown as “”§The maximum length is quite large (an int)
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 65
Integer.MAX_VALUE (2147483647 (231 - 1)
![Page 66: FUNDAMENTAL DATA TYPES](https://reader030.fdocuments.net/reader030/viewer/2022012805/61bd360c61276e740b107388/html5/thumbnails/66.jpg)
String Concatenation (+)q You can ‘add’ one String onto the end of
anotherString fName = “Harry”String lName = “Morgan“String name = fname + lname; // HarryMorgan
q You wanted a space in between?String name = fname + “ “ + lname; // Harry Morgan
q To concatenate a numeric variable to a String:String a = “Agent“;int n = 7;String bond = a + n; // Agent7
q Concatenate Strings and numerics inside println:System.out.println("The total is " + total);
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 66
![Page 67: FUNDAMENTAL DATA TYPES](https://reader030.fdocuments.net/reader030/viewer/2022012805/61bd360c61276e740b107388/html5/thumbnails/67.jpg)
String Inputq You can read a String from the console with:
System.out.print("Please enter your name: ");String name = in.next();
§ The next method reads one word at a time§ It looks for ‘white space’ delimiters
q You can read an entire line from the console with:System.out.print("Please enter your address: ");String address = in.nextLine();
§ The nextLine method reads until the user hits ‘Enter’q Converting a String variable to a number
System.out.print("Please enter your age: ");String input = in.nextLine();int age = Integer.parseInt(input); // only digits!
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 67
if Harry Morgan is entered
only Harry is catched
![Page 68: FUNDAMENTAL DATA TYPES](https://reader030.fdocuments.net/reader030/viewer/2022012805/61bd360c61276e740b107388/html5/thumbnails/68.jpg)
String Escape Sequencesq How would you print a double quote?
§ Preface the " with a \ inside the double quoted StringSystem.out.print("He said \"Hello\"");
• The backslash (\) is not included in the string• \" is called an escape sequence
q OK, then how do you print a backslash?§ Preface the \ with another \, use the escape sequence \\System.out.print("C:\\Temp\\Secret.txt");
q Special characters inside Strings§ Output a newline with a ‘\n’System.out.print("*\n**\n***\n");
******
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 68
![Page 69: FUNDAMENTAL DATA TYPES](https://reader030.fdocuments.net/reader030/viewer/2022012805/61bd360c61276e740b107388/html5/thumbnails/69.jpg)
Strings and Charactersq Strings are sequences of characters
§ Unicode characters to be exact§ Characters have their own type: char§ Characters have numeric values
• See the ASCII code chart in Appendix B• For example, the letter ‘H’ has a value of 72 if it were a
number
q Use single quotes around a charchar initial = ‘B’;
q Use double quotes around a StringString initials = “BRL”;
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 69
![Page 70: FUNDAMENTAL DATA TYPES](https://reader030.fdocuments.net/reader030/viewer/2022012805/61bd360c61276e740b107388/html5/thumbnails/70.jpg)
Copying a char from a Stringq Each char inside a String has an index number:
q The first char is index zero (0)q The charAt method returns a char at a given
index inside a String:String greeting = "Harry";char start = greeting.charAt(0);char last = greeting.charAt(4);
0 1 2 3 4 5 6 7 8 9
c h a r s h e r e
0 1 2 3 4H a r r y
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 70
![Page 71: FUNDAMENTAL DATA TYPES](https://reader030.fdocuments.net/reader030/viewer/2022012805/61bd360c61276e740b107388/html5/thumbnails/71.jpg)
Copying portion of a Stringq A substring is a portion of a Stringq The substring method returns a portion of a String at a given index for a number of chars, starting at an index:
String greeting = "Hello!";String sub = greeting.substring(0, 2);
String sub2 = greeting.substring(3, 5);
0 1 2 3 4 5H e l l o !0 1
H e
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 71
![Page 72: FUNDAMENTAL DATA TYPES](https://reader030.fdocuments.net/reader030/viewer/2022012805/61bd360c61276e740b107388/html5/thumbnails/72.jpg)
Table 9: String Operations (1)
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 72
2.5 Strings 63
12 // Get the names of the couple13 14 System.out.print("Enter your first name: ");15 String first = in.next();16 System.out.print("Enter your significant other's first name: ");17 String second = in.next();18 19 // Compute and display the inscription20 21 String initials = first.substring(0, 1) 22 + "&" + second.substring(0, 1);23 System.out.println(initials);24 }25 }
Program Run
Enter your first name: RodolfoEnter your significant other's first name: SallyR&S
Table 9 String Operations
Statement Result Comment
string str = "Ja";str = str + "va";
str is set to "Java" When applied to strings, + denotes concatenation.
System.out.println("Please" + " enter your name: ");
Prints Please enter your name:
Use concatenation to break up strings that don’t fit into one line.
team = 49 + "ers" team is set to "49ers" Because "ers" is a string, 49 is converted to a string.
String first = in.next();String last = in.next();(User input: Harry Morgan)
first contains "Harry"last contains "Morgan"
The next method places the next word into the string variable.
String greeting = "H & S";int n = greeting.length();
n is set to 5 Each space counts as one character.
String str = "Sally";char ch = str.charAt(1);
ch is set to 'a' This is a char value, not a String. Note that the initial position is 0.
String str = "Sally";String str2 = str.substring(1, 4);
str2 is set to "all" Extracts the substring starting at position 1 and ending before position 4.
String str = "Sally";String str2 = str.substring(1);
str2 is set to "ally" If you omit the end position, all characters from the position until the end of the string are included.
String str = "Sally";String str2 = str.substring(1, 2);
str2 is set to "a" Extracts a String of length 1; contrast with str.charAt(1).
String last = str.substring( str.length() - 1);
last is set to the string containing the last character in str
The last character has position str.length() - 1.
bjlo_ch02_9.indd 63 10/17/11 4:57 PM
![Page 73: FUNDAMENTAL DATA TYPES](https://reader030.fdocuments.net/reader030/viewer/2022012805/61bd360c61276e740b107388/html5/thumbnails/73.jpg)
Table 9: String Operations (2)
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 73
![Page 74: FUNDAMENTAL DATA TYPES](https://reader030.fdocuments.net/reader030/viewer/2022012805/61bd360c61276e740b107388/html5/thumbnails/74.jpg)
Summary: Variablesq A variable is a storage location with a name.q When declaring a variable, you usually specify an initial
value.q When declaring a variable, you also specify the type of its
values.q Use the int type for numbers that cannot have a
fractional part.q Use the double type for floating-point numbers.q By convention, variable names should start with a lower
case letter.q An assignment statement stores a new value in a
variable, replacing the previously stored value
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 74
![Page 75: FUNDAMENTAL DATA TYPES](https://reader030.fdocuments.net/reader030/viewer/2022012805/61bd360c61276e740b107388/html5/thumbnails/75.jpg)
Summary: Operators q The assignment operator = does not denote mathematical
equality.q You cannot change the value of a variable that is defined
as final. q The ++ operator adds 1 to a variable; the -- operator
subtracts 1.q If both arguments of / are integers, the remainder is
discarded.q The % operator computes the remainder of an integer
division.
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 75
![Page 76: FUNDAMENTAL DATA TYPES](https://reader030.fdocuments.net/reader030/viewer/2022012805/61bd360c61276e740b107388/html5/thumbnails/76.jpg)
Summary: Java APIq The Java library declares many mathematical functions,
such as Math.sqrt and Math.pow.q You use a cast (typeName) to convert a value to a
different type.q Java classes are grouped into packages. Use the import
statement to use classes from packages.q Use the Scanner class to read keyboard input in a console
window.q Use the printf method to specify how values should be
formatted.q The API (Application Programming Interface)
documentation lists the classes and methods of the Java library.
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 76
![Page 77: FUNDAMENTAL DATA TYPES](https://reader030.fdocuments.net/reader030/viewer/2022012805/61bd360c61276e740b107388/html5/thumbnails/77.jpg)
Summary: Stringsq Strings are sequences of characters.q The length method yields the number of characters in a
String.q Use the + operator to concatenate Strings; that is, to put
them together to yield a longer String.q Use the next (one word) or nextLine (entire line)
methods of the Scanner class to read a String.q Whenever one of the arguments of the + operator is a
String, the other argument is converted to a String.q If a String contains the digits of a number, you use the
Integer.parseInt or Double.parseDouble method to obtain the number value.
q String index numbers are counted starting with 0.q Use the substring method to extract a part of a String
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 77