Tuesday, September 5, 2017

Book - Core Java for Impatiens - Chapter 1 - Fundamental Programming Structure


* Integer Types
















* You write long integer literals with a suffix L (for example, 4000000000L). There is
no syntax for literals of type byte or short.

* Hexadecimal literals have a prefix 0x (for example, 0xCAFEBABE).

*Binary values have a prefix 0b. For example, 0b1001 is 9.

*Octal numbers have a prefix 0. For example, 011 is 9. This can be confusing, so it seems best to stay away from octal literals and leading zeroes.

*You can add underscores to number literals, such as 1_000_000(or 0b1111_0100_0010_0100_0000) to denote one million. The underscores are for human eyes only, the Java compiler simply removes them.

*Integer types in Java are signed. However, if you work with values that can never be negative and you really need an additional bit, you can use methods that interpret values as unsigned. For example, instead of having a byte value b represent the range from –128 to 127, you may want a range
from 0 to 255.You can store it in a byte, and, due to the nature of binary arithmetic, certain operations such as addition and subtraction will work. For other operations, call Byte.toUnsignedInt(b) and get an int value between 0 and 255.


Floating-Point Types













* Numbers of type float have a suffix F (for example, 3.14F). Floating-point literals without an F suffix (such as 3.14) have type double. You can optionally supply the D suffix (for example, 3.14D).

*You can specify floating-point literals in hexadecimal. For example, 0.0009765625 = 2–10 can be written as 0x1.0p-10. In hexadecimal notation, you use a p, not an e, to denote the exponent. (An e is a hexadecimal digit.) Note that, even though the digits are written in hexadecimal, the exponent
(that is, the power of 2) is written in decimal.

*There are special floating-point values Double.POSITIVE_INFINITY for , Double.
NEGATIVE_INFINITY for – , and Double.NaN for “not a number.” For example, the result of computing 1.0 / 0.0 is positive infinity. Computing 0.0 / 0.0 or the square root of a negative number yields NaN.

*All “not a number” values are considered to be distinct from each other.Therefore, you cannot use the test if (x == Double.NaN) to check whether x is a NaN. Instead, call if (Double.isNaN(x)). There are also methods Double. isInfinite to test for ± , and Double.isFinite to check that a floating-point
number is neither infinite nor a NaN.

*All “not a number” values are considered to be distinct from each other.Therefore, you cannot use the test if (x == Double.NaN) to check whether x is a NaN. Instead, call if (Double.isNaN(x)). There are also methods Double. isInfinite to test for ± , and Double.isFinite to check that a floating-point
number is neither infinite nor a NaN.

* Floating-point numbers are not suitable for financial calculations in which roundoff errors cannot be tolerated. For example, the command System.out.println(2.0 - 1.1) prints 0.8999999999999999, not 0.9 as you would expect. Such roundoff errors are caused by the fact that floating-point numbers are represented in the binary number system. There is no precise binary representation of the
fraction 1/10, just as there is no accurate representation of the fraction 1/3 in the decimal system. If you need precise numerical computations without roundoff errors, use the BigDecimal class

* An integer division by zero gives rise to an exception which, if not caught, will terminate your program

* A floating-point division by zero yields an infinite value or NaN

* You need to be careful with the / operator. If both operands are integer types, it denotes integer division, discarding the remainder. For example, 17 / 5 is 3, whereas 17.0 / 5 is 3.4

*There is no operator for raising numbers to a power. Instead, call the Math.pow
method: Math.pow(x, y) yields xy. To compute the square root of x, call Math.sqrt(x).

*The Math class provides several methods to make integer arithmetic safer. The mathematical operators quietly return wrong results when a computation overflows. For example, one billion times three (1000000000 * 3) evaluates to -1294967296 because the largest int value is just over two billion. If you call Math.multiplyExact(1000000000, 3) instead, an exception is generated. You can catch that exception or let the program terminate rather than quietly continue with a wrong result. There are also methods addExact, subtractExact, incrementExact, decrementExact, negateExact, all with int and long parameters.

*When an operator combines operands of different number types, the numbers are converted to a common type before they are combined. Conversion occurs in this order:

1. If either of the operands is of type double, the other one is converted to double.
2. If either of the operands is of type float, the other one is converted to float.
3. If either of the operands is of type long, the other one is converted to long.
4. Otherwise, both operands are converted to int

*Logical AND operator, If the first condition is false, the second condition is not evaluated. This “short circuit” . Same for logical OR operator. If first condition is true then second is not evaluated. It is also called "short circuit"

*you saw that the result of the floating-point subtraction 2.0 - 1.1 is 0.8999999999999999. The BigDecimal class can compute the result accurately. The call BigDecimal.valueOf(n, e) returns a BigDecimal instance with value n × 10–e. The result of BigDecimal.valueOf(2,0).subtract(BigDecimal.valueOf(11, 1)) is exactly 0.9.

*It is somewhat inefficient to concatenate a large number of strings if all you need is the final result. In that case, use a StringBuilder instead

* To compare two strings without regard to case, use the equalsIgnoreCase method.

*When you call System.out.println, output is sent to the “standard output stream” and shows up in a terminal window. Reading from the “standard input stream” isn’t quite as simple because the corresponding System.in object only has methods to read individual bytes. To read strings and numbers, construct a Scanner that is attached to System.in

*To read a password, you do not want to use the Scanner class since the
input is visible in the terminal. Instead, use the Console class:
Console terminal = System.console();
String username = terminal.readLine("User name: ");
char[] passwd = terminal.readPassword("Password: ");
The password is returned in an array of characters. This is marginally more
secure than storing the password in a String because you can overwrite the
array when you are done

*Each of the format specifiers that start with a % character is replaced with the corresponding
argument. The conversion character that ends a format specifier indicates the type of the value to be formatted: f is a floating-point number, s a string, and d a decimal integer. Table 1–5 shows all conversion characters







































* In addition, you can specify flags to control the appearance of the formatted output. Table 1–6 shows all flags. For example, the comma flag adds grouping separators, and + yields a sign for positive numbers. The statement
System.out.printf("%,+.2f", 100000.0 / 3.0);
        prints
          +33,333.33

* You can use the String.format method to create a formatted string without printing it:
String message = String.format("Hello, %s. Next year, you'll be %d.\n", name, age);








In order to buy book : Core Java for Impatiens









2 comments:

  1. https://dzone.com/articles/never-use-float-and-double-for-monetary-calculatio

    ReplyDelete
  2. What Are Precision and Scale?
    Precision is the total number of digits (or significant digits) of a real number.

    Scale specifies the number of digits after the decimal place. For example, 12.345 has the precision of 5 (total digits) and the scale of 3 (number of digits right of the decimal).

    ReplyDelete

Thank you for your comment!