NumbersBits and bytesNumber systemsEverything is a number?CountingCounting in three systemsComputers use binary numbersUsing octal and hex numbersWriting octal and hex integersInteger typesFloating-point literalsFloating point typesNumber “width”Widening and narrowingCastsExample castsThe fifth integer typeMixed typesMath methodsMethod parametersThe EndJan 13, 2019NumbersBits and bytesA bit is a single two-valued quantity: yes or no, true or false, on or off, high or low, good or badOne bit can distinguish between two cases: T, FTwo bits can distinguish between four cases: TT, TF, FT, FFThree bits can distinguish between eight cases: TTT, TTF, TFT, TFF, FTT, FTF, FFT, FFFIn general, n bits can distinguish between 2n casesA byte is 8 bits, therefore 28 = 256 casesNumber systemsThe binary (base 2) number system uses two “binary digits, ” (abbreviation: bits) -- 0 and 1The octal (base 8) number system uses eight digits:0, 1, 2, 3, 4, 5, 6, 7The decimal (base 10) number system uses ten digits:0, 1, 2, 3, 4, 5, 6, 7, 8, 9The hexadecimal, or “hex” (base 16) number system uses sixteen digits:0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, FEverything is a number?Everything in the computer is stored as a pattern of bitsBinary distinctions are easy for hardware to work withNumbers are stored as a pattern of bitsComputers use the binary number systemCharacters are stored as a pattern of bitsOne byte (8 bits) can represent one of 256 charactersSo, is everything in the computer stored as a number?No it isn’t, it’s stored as a bit patternThere are many ways to interpret a bit patternCountingTo count up in any number system,add 1 to the rightmost digitif the result is higher than the largest digit,set that digit to zero and carry to the next placerepeat addition of 1 and carrying as many times as necessaryExample: In hex, 4A6FF + 1 = 4A700Counting in three systemsDec Bin Hex 0 0 0 1 1 1 2 10 2 3 11 3 4 100 4 5 101 5 6 110 6 7 111 7 8 1000 8 9 1001 910 1010 A Dec Bin Hex 11 1011 B 12 1100 C 13 1101 D 14 1110 E 15 1111 F 16 10000 10 17 10001 11 18 10010 12 19 10011 13 20 10100 14Computers use binary numbersPeople like to use decimal numbersComputers use binary numbersJava translates decimal numbers into binaryThe computer does all its arithmetic in binaryJava translates binary results back into decimalYou occasionally have to use numbers in other number systemsColors are usually specified in hexadecimal notation:#FF0000, #669966,Using octal and hex numbersComputers use binary, but the numbers are too long and confusing for people--it’s easy to lose your placeOctal or hex is better for peopleTranslation between binary and octal or hex is easyOne octal digit equals three binary digits 101101011100101000001011 5 5 3 4 5 0 1 3One hexadecimal digit equals four binary digits 101101011100101000001011 B 5 C A 0 BWriting octal and hex integersIntegers are usually written in decimal notation:7, 532, -28To write a number in octal, just start with a zero:02, 0657, -077...but don’t use the digits 8 or 9 !To write a number in hexadecimal, start with 0x or 0X: 0xA, 0X43AB5, -0xFFFFThe “digits” A through F can be upper or lower caseUppercase is usually preferredLowercase is more readable for long numbersInteger typesThere are four integer typesbyte – occupies one byte (surprise!)Can hold numbers in range –128 to 127short – occupies two bytesCan hold numbers in range –32768 to 32767int – occupies four bytesCan hold numbers up to + or – 2 billionlong – occupies eight bytesCan hold numbers up to about 19 digitsLiterals are written with an L suffix: 123456789LA lowercase L can be used, but it’s a bad idea: 123456789lFloating-point literalsFloating-point literals are written with a decimal point: 8.5 -7.923 5.000Floating-point numbers may also be written in “scientific notation”– times a power of 10We use E to represent “times 10 to the”Example: 4.32E5 means 4.32 x 105float literals are written with an F suffixExamples: 8.5F -7.923F 5.000F 4.32E5FIf you don’t have the F suffix, type double is assumedFloating point typesThere are two floating-point typesfloat – occupies four bytesCan hold numbers in the range 3.4E38 to 1.4E-45Accuracy is about nine digitsdouble – occupies eight bytesCan hold numbers in the range 1.7E308 to 4.9E-324Accuracy is seventeen or eighteen digitsNumber “width”Numeric types are considered wider or narrower than other numeric typesThis is based partly on number of bytes occupiedAlso based on how large a number it can holdJava doesn’t mind if you assign a narrow value to a wide variable: int n = 3;Java is not happy if you assign a wide value to a narrow variable: byte b = 7139946; // illegalWidening and narrowingYou can always widen (assign a narrower type to a wider type): double wide;int narrow;wide = narrow;But if you want to narrow (assign a wider type to a narrower type), you have to cast it: narrow = (int)wide;doublefloatlongintshortbyteCastsYou can convert (cast) one numeric type to anotherWhen you widen, no explicit cast is necessaryBut it doesn’t hurtWhen you narrow, an explicit cast is requiredThis requirement is made to help avoid errorsCasting tells Java that the value in the wider type will fit in the narrower typeJava checks to make sure that the cast works, and gives you an error if it didn’tExample casts short s = 0;int i = 0;double d = 0.0; d = i; // legald = s; // legali = s; //legal i = d; // illegals = d; // illegals = i; // illegal i = (int) d; // legals = (short) d; // legals = (short) i; // legal d = 3.7E20;i = 50000; // The following give// runtime errors:s = (short) i;i = (int) d;The fifth integer typeThe primitive type char refers to a single, two-byte Unicode characterThere is no good reason this should be a numeric type......but characters were numbers in CYou can use characters in arithmetic (they will automatically be converted to int)char
View Full Document