# BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.

Post on 03-Jan-2016

213 views

TRANSCRIPT

Chapter 2a Primitive Data Types and Operations

Building Java ProgramsChapter 2PRIMITIVE DATA TYPES AND OPERATIONS##ObjectivesIdentify Javas primitive data types and operators.Evaluate complex expressions using rules of precedence and mixed types.

####Data TypesA name for a category of data values that are all related.Different data types support different operations and behave differently.You can multiply numbers, but can you multiply strings? Can you add them?Stored differently in computer memory as well.27 = 00011011hello = 01101000 00000101 1101100 1101100 01101111

####3Javas Primitive Data TypesNameDescriptionExampleintintegers (whole numbers)42, -3, 18, 20493, 0doublereal numbers7.35, -19.83423, 18.0charsingle characters'a', 'X', '3', '\n'booleanlogical valuestrue, falseVariations of int: byte, short, longVariations of double: float####4Javas Arithmetic OperatorsOperatorMeaningExampleResult+addition2 + 24subtraction53 1835*multiplication3 * 824/division4.8 / 2.02.4%mod (remainder)19 % 54Most behave exactly like you would expect them to in math class, but well learn about the exceptions soon.####ExpressionsAn expression is a simple value or a set of operations that produces a value.Simplest expressions are literals of the data types we know. E.g. 27, Seattle... Yay!", -1.7, falseMore complex expressions can use operators and parentheses. E.g.: 5 * 27 - 3 * (1.3 (5.7 3)) System.out.println( 5 * 2 / 3 + 6 );####6Floating-Point DivisionWhen dividing double or float values, its just like math class, but with some rounding errors here and there5.0 / 2.0 = 2.510.0 / 3.0 = 3.3333333333333335 (rounding error!)4.0 / 5.0 = 0.88.0 / 3.0 * 3.0 = 8.0####7Integer Division weirdWhen dividing integers, we keep the whole part, but discard the fractional part.5 / 2 = 2not 2.510 / 3 = 3not 3.3334 / 5 = 0not 0.88 / 3 * 3 =?2 * 3 =?6not 8!####8Integer mod (%)The mod operator computes the remainder of a division6 / 4 = 1, but it would have had a remainder 2Therefore, 6 % 4 = 220 % 7 =15 % 4 =13857 % 2 =13856 % 2 =8374 % 10 =8374 % 100 =36 % 6 =7 % 9 =631047407####9In your notebookWrite four expressions using only % and / that will get me the four individual digits of 7382.

7382738273827382*** Will your expressions work for other four-digit numbers? ***/ 1000 % 10/ 100 % 10/ 10 % 10Think, Pair, Share= 2= 8= 3= 7???????????????% 10?????####10Operator PrecedenceUsually, we evaluate expressions left-to-right, but certain operations take precedence over others and get evaluated first.Parentheses can always override precedence.Precedence table:DescriptionOperatorsunary operators+, -multiplicative operators*, /, %additive operators+, -####11Precedence Examples2 + 2 * 5 = 12not 203 * 3 + 2 * 2 = 13not 227 + 5 / 2 * 3 4 = 9not 14+3 * -4 = -123 + -4 = -13 - -4 = 7####Mixing TypesWhen doing an operation on an int and a double, the int gets promoted to a double.1 * 4.682 = 4.6827 / 2.0 = 3.55 * 1.0 / 2 = 3.07.0 / 2 7 / 2 =?3.5 3 =?0.5####CastingYou can explicitly convert a value from one data type to another by casting.(double) 99 = 99.0(int) 2.5 = 2((double) 7) / 2 = 3.5(int) 2.5 * 3.0 = 6.0(int) (2.5 * 3.0) = 7####In your notebook4.0 / 2 * 9 / 2 2.0 * 9 / 2 18.0 / 2 9.0####15In your notebook12 / 7 * 4.4 * 2 / 4 1 * 4.4 * 2 / 4 4.4 * 2 / 4 8.8 / 4 2.2####16In your notebook9 / 2.0 + 7 / 3 3.0 / 2 4.5 + 7 / 3 3.0 / 2 4.5 + 2 3.0 / 2 4.5 + 2 1.5 6.5 1.5 5.0####17In your notebook53 / 5 / (0.6 + 1.4) / 2 + 13 / 253 / 5 / 2.0 / 2 + 13 / 2 10 / 2.0 / 2 + 13 / 2 5.0 / 2 + 13 / 2 2.5 + 13 / 2 2.5 + 6 8.5####18

Recommended