COMP 14 Introduction to Programming Adrian Ilie June 27 2005 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Review Exercise Execution of c 2 a b in a computer 2 Take the value of a Multiply it by 2 Take the value of b Add b to the previous result Put final result into c Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Today in COMP 14 Java 3 special symbols identifiers data types operators expressions strings Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Reading Check Up syntax rules of a language 1 The determine which instructions are valid 2 True or False Hello is an example False of a legal Java identifier 3 If an operator has an integer and a floating point operand the result of the operation isfloating point a number 4 The expression int 9 2 evaluates to 9 4 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Introduction Computer program a sequence of statements whose objective is to accomplish a task Programming process of planning and creating a program Programming language a set of rules symbols and special words 5 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Sample Java Program public class Hello public static void main String args System out println Hi There Upon execution this program would display Hi There 6 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Programming Languages Programming languages have rules of grammar just as English does syntax rules which statements are legal and which are not semantic rules determine the meaning of the instructions token smallest individual unit of a program special symbols word symbols identifiers 7 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Special Symbols 8 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Word Symbols aka reserved words or keywords int float double char void public static throws return reserved words are always all lowercase each word symbol is considered to be a single symbol cannot be used for anything other than their intended purpose in a program shown in blue typewriter font in textbook full table in Appendix A 9 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Identifiers Names of things variables constants methods in your programs Can be composed of any combination of letters digits underscore and dollar sign Cannot begin with a digit May be any length Java is case sensitive Total total and TOTAL are different identifiers 10 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Illegal Identifiers 11 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Questions 1 2 3 4 5 6 12 Classify the following as legal or illegal identifiers My First Program illegal legal my1stProgram 1stProgram illegal legal money an identifier legal illegal Jane sProgram Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Primitive Data Types What s A Data Type A set of values and the operations that can be performed on those values Primitive data are fundamental values such as numbers and characters Operations are performed on primitive types using built in operators 13 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Primitive Data Types 8 primitive data types in Java 4 represent integers byte short int long 2 represent floating point numbers float double 1 represents characters char 1 represents boolean values boolean 14 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Primitive Data Types Numeric Types The difference between the various numeric primitive types is their size and therefore the values they can store Type Storage Min Value Max Value byte 8 bits 128 127 short 16 bits 32 768 32 767 int 32 bits 2 147 483 648 2 147 483 647 long 64 bits 9 x 1018 9 x 1018 float 32 bits double 64 bits 15 Adrian Ilie 3 4 x 1038 with 7 significant digits 1 7 x 10308 with 15 significant digits The UNIVERSITY of NORTH CAROLINA at CHAPEL Integers Examples 6728 67 0 78 36782 Positive integers do not have a sign in front of them but they can No commas are used in an integer commas in Java are used to separate items in a list 16 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Primitive Data Types Characters A char stores a single character from the Unicode character set an ordered list of characters and each character corresponds to a unique number uses 16 bits per character allowing for 65 536 unique characters Character literals are delimited by single quotes a X 7 n newline character we ll discuss later 17 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Primitive Data Types Booleans Only two valid values true or false uses 1 bit for storage Represent any situation that has 2 states on off true false true and false are reserved words 18 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Arithmetic Expressions Expression a combination of one or more operands and their operators Arithmetic expressions compute numeric results and make use of the arithmetic operators Addition Subtraction Multiplication Division Remainder If either or both operands associated with an arithmetic operator are floating point the result is a floating point 19 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Division and Remainder If both operands to the division operator are integers the result is an integer the fractional part is discarded 8 12 equals 0 14 3 equals 4 The remainder or modulus operator returns the remainder after dividing the second operand into the first only works with integer types 8 8 12 equals 14 3 equals 2 20 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Unary vs Binary Operators Unary operators has only one operand example negative not subtraction 5 Binary operators has two operands example subtraction 5 3 21 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Operator Precedence Determines the order in which operators are evaluated 1 multiplication division and remainder 2 addition subtraction and string concatenation 3 arithmetic operators with the same precedence are evaluated from left to right Parentheses can be used to force the evaluation order just like in math 22 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Operator Precedence What is the order of evaluation in the following expressions a b c d e 1 2 3 4 a b c d e 3 1 4 2 a b c d e 2 1 4 3 a b c d e 4 3 2 1 23 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Integral Expressions All operands are integers Result is an integer Examples 2 3 5 3 x y 7 x 2 y z 18 24 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Floating point Expressions All
View Full Document