FUNADAMENTAL DATA TYPES LEARNING OBJECTIVES: TO UNDERSTAND INTEGEr AND FLOATING POINT NUMBERS TO RECOGNIZE THE LIMITATIONS OF THE NUMERIC TYPES TO UNDERSTAND THE USE OF CONSTANTS
NUMBER TYPES TYPEDESCRIPTIONSIZE intThe integer type with range -2,147,483,648 – 2,147483,647 4 bytes byteA single byte, with range -128 ….1271 byte short the short integer type, with range - 32, ,767 2 bytes longThe long integer type, with range - 9,223,372,036,854,775,808 …9,223,372,036,854,775,807 8 bytes floatThe double-precision floating-point the with range of about bytes charThe character type represent code units in the Unicode encoding scheme 2 bytes booleanThe type with the two truth values false and true 1 bit
Overflow using ints Calculations involving integers can overflow. This happens if the result of the computation exceeds the range for the number type. For example:int n= ; System.out.println(n*n); //prints the result is truncated to fit into an int,
Floating point number limitations The double type has about 15 significant digits, and there are many numbers that cannot be accurately represented as double numbers. Not every value can be represented prisely. Therefore numbers are rounded to the nearest match. For example: double f=4.35; System.out.println(100*f); prints
How do computers represent numbers? Binary There is no exact representation to the fractional parts of a number. 1/3= Therefore the double type is not appropriate for financial calculations. Professional Programmers usethe BigDecimal type
Big Numbers Big numbers are objects of the BigInteger and BigDecimal classes in the java.math package. Big number objects have essentially no limits on their size and precision. Computation is slower than those that involve number types. You cannot use (+,*,-), instead you have to use methods called add, subtract, and multiply
Constants Constant values do not change and have special significance for computation. Once the value is set, it cannot change. Constants are declared using all CAPs final double QUARTER_VALUE =.025; final double DIME_VALUE=0.1;