Ch4 -Fundamental Data Types
If you want to compute with really large numbers, you can use
big number objects,
To overcome the former problem, you can
convert the floating-point value to an integer with a cast
If one of the arguments of the +operator is a string, the other is
converted to a string
In Java, constants are identified with the keyword
fina1
The BigDecima1 type carries out....
floating-point computation without round off errors
13.Assuming the String variables holds the value "Agent", what is the effect of the assignment s = s + s.length()?
s is set to the string Agent5D
To convert a string to a number, use the
static parseInt ()or parseDouble ()methods of the Integer or Double classes:
The substring ()method computes
substrings of a string.
If you want to round a floating-point number to the nearest whole number, use
the Math.round () method
A numeric computation overflows if ....
the result falls outside the range for the number type
To convert a number to a string you could use
the static toString () method of the Integer or Double classes, but also the empty string with the +operator:
The printf()method of the PrintStreamclass allows
to give special format to the numbers to be printed
Use the +operator
to put strings together to form a longer string
Convention:
use all-uppercase namesfor constants
8. What is the value of 1729 / 100? Of 1729 % 100?
17 and 29
The charAt () method of the String class returns ......
a code unit from a string
5.What is wrong with the following statement? double circumference = 3.14 * diameter;
(1) You should use a named constant, not the "magic number" 3.14 (2) 3.14 is not an accurate representation of
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?
By using a cast: (int) Math.round (x),
12.Is the call System.out.println(4)a static method call?
No-the printlnmethod is called on the object System.out
7.What is the value of n after the following sequence of statements? n--; n++; n--;
One less than it was before
9. Why doesn't the following statement compute the average of s1, s2, and s3? double average = s1 + s2 + s3 / 3; // Error
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
15. Why can't input be read directly from System.in?
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.
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;
The first definition is used inside a method, the second inside a class
6.What is the meaning of the following statement? balance = balance+ amount;
The statement adds the amountvalue to the balance variable
14.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)?
The strings "i"and "ssissi"
16. 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?
The value is "John". The next method reads the next word.
2.When does the cast (long) x yield a different result from the call Math.round(x)?
When the fractional part of xis .0.5,
Another escape sequence occasionally used is
\n, which denotes a newlineor line feed character
Java uses the Unicode encoding scheme; you can include any international character inside a string by writing
\u followed by its Unicode encoding):
A constant is ......
a variable which value cannot be changed once it has been set
The backslash character, \, is used as .....
an escape character.
A sequence like \"is called
an escape sequence.
The backslash does not denote itself; instead, it is used to
encode other characters that would otherwise be difficult to include in a string
1.Which are the most commonly used number types in Java?
int and double
Named constants
make programs easier to read and maintain (do not use literal constants like 3.1416 through your code, use named constants instead)
You can't use the familiar arithmetic operators (+, -,*, etc.) with big number objects. Instead, you have to use
methods called add(), subtract(), multiply(),
Rounding errors occur .......
when an exact conversion between numbers is not possible:
11.Why can't you call x.pow(y)to compute xy?
x is a number, not an object, and you cannot invoke methods on numbers
If you actually want to print a backslash,
you must enter two \\in a row: