Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
SYS-1A4Y Programming - Languages and Software Construction Dr Gavin Cawley Lecture 4 - Variables, Constants and Primitive Data Types Today’s Lecture • Last lecture: – Resources/syllabus – What is programming, how is it done? – Two Java programs • This lecture: – – – – Investigate the Java primitive data types. Learn how to declare variables. Learn how to initialise variables using constants. Compare decimal and binary number systems. Lecture 4 : Slide 2 Java Primitive Data Types • Java provides eight primitive data types: – Integral types (byte,short,int,long) • e.g. 42, 593258, 0, -7 – A character type (char) • e.g. ‘a’, ‘b’, ‘c’, ‘ ’, ‘\n’ – A Boolean type (boolean) • true, false – Floating point types (float,double) • e.g. 3.141592654, 7.0, -829.9 Lecture 4 : Slide 3 1 Types and Declarations • Before a variable can be used in a Java program it must be declared. – A variable declaration specifies the name of the variable, also known as an identifier. – A variable declaration specifies the type of the variable. – A variable declaration may optionally specify a numeric value, known as a literal constant, used to inititialise the variable. Lecture 4 : Slide 4 Example of a Simple Declaration int a = 42; Lecture 4 : Slide 5 A More Complex Declaration int a = 42, b, c = 0; • This declaration creates: – An integer variable called ‘a’ that is initialised to hold the value 42 – An uninitialised integer variable called ‘b’ – An integer variable called ‘c’ initialised with a value of 0 Lecture 4 : Slide 6 2 Java Integral Data Types • The int is the basic integer data type • Stores numeric values that do not have a fractional part (i.e. whole numbers) • Can be positive or negative. • Stored using a binary representation. Lecture 4 : Slide 7 Decimal Numbers Decimal - a number system based on multiples of integer powers of ten: 593258 = 5 × 10 5 (10 5 = 100000) + 9 × 10 4 (10 4 = 10000) + 3 × 10 3 (10 3 = 1000) + 2 × 10 2 (10 2 = 100) + 5 × 10 1 (10 1 = 10) 0 + 8 × 10 (10 0 = 1) Lecture 4 : Slide 8 Binary Numbers Binary - a number system based on multiples of integer powers of two: 4210 = 101010 2 = 1 × 2 5 (2 5 = 32) + 0×2 4 (2 4 = 16) + 1×2 3 (2 3 = 8) + 0×2 2 (2 2 = 4) + 1×2 1 (2 1 = 2) 0 + 0×2 (2 0 = 1) Lecture 4 : Slide 9 3 Addition Decimal Binary 95 + 42 01011111 + 00101010 137 1 10001001 11111 Lecture 4 : Slide 10 Dealing with Negative Numbers • “Two’s complement” representation – Invert bits – Add 1 42 10 = 00101010 11010101 + 1 11010110 = - 42 10 1 Lecture 4 : Slide 11 Subtraction Decimal Binary 95 - 42 01011111 + 11010110 53 1 00110101 1 1 1111 Lecture 4 : Slide 12 4 Integer Literal Constants • Integer literal constants can be specified in three bases: – Decimal (base 10) • E.g: 0, 42, -7, +87, 4096 – Hexadecimal (base 16) • E.g. 0xFAB0, 0xa, 0x1234 – Octal (base 8) • E.g: 037, 042 • Hardly ever used Lecture 4 : Slide 13 Hexadecimal Literals Decimal Binary 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 Hex 0 1 2 3 4 5 6 7 8 9 A B C D E F Example: 0x4A7E 0100 1010 0111 1110 4 A 7 E Lecture 4 : Slide 14 Integer Types • Java defines an int to be a signed 32-bit integer: – Range: -2147483648 to +2147483647 • Sometimes we need to store even larger integer values, or an integer that takes up less memory or disk space. • There are three other types of integer variables: – byte – short – long Lecture 4 : Slide 15 5 Java Integral Types Declaration Bits Minimum Maximum -128 +127 16 -32768 +32767 int i; 32 -2147483648 +2147483647 long l; 64 - a big number + a big number byte b; 8 short s; Lecture 4 : Slide 16 Long Integral Literals • We can specify the type of an integral literal. • Use the suffixes l or L. • This indicates that the literal is a long. long a = 123456789L; • Use L beacause l is easily confused with 1. Lecture 4 : Slide 17 Character Data Type • Java includes a data type called char used to store character data (UNICODE character set) • Declarations: – char c; – char c = ‘a‘; • Character literals: – ‘a‘,‘z‘,‘A‘,‘Z‘,‘0‘,‘9‘,‘ ‘,‘\t‘,‘\n‘,‘\\‘ Lecture 4 : Slide 18 6 Special Character Literals ‘\a‘ ‘\b‘ ‘\f‘ ‘\n‘ ‘\r‘ ‘\t‘ ‘\v‘ alert backspace formfeed new line carriage return horizontal tab vertical tab ‘\\‘ ‘\?‘ ‘\‘‘ ‘\”‘ backslash question mark single quote double quote ‘\uXXXX‘ UNICODE character XXXX = code Lecture 4 : Slide 19 Character Data Type • A char is really an integer • Characters are normally represented by UNNICODE codes – E.g. ‘\n‘ is represented by the number 0x000A • Allows Internationalisation – Diacritics – Alternate character set Lecture 4 : Slide 20 Boolean Data Type • A Boolean variable is one that represents a quantity that can be true or false • Declarations boolean a; boolean a = true; • Literal constants: – true – false Lecture 4 : Slide 21 7 Floating-Point Data Types • Floating point data types are used to store real numbers (including a fractional part) • Stored as two signed binary numbers: – mantissa – exponent value = mantissa × 2 exponent • Conforms to standard IEEE 754 Lecture 4 : Slide 22 Declarations and Literals • Declarations: float a; float a = 3.14159; float a = 1.23e10; • Literal constants: – 1.23 .23 1. 1.0 1.2e9 1.23e-15 Lecture 4 : Slide 23 Java Floating Point Types float double Bits 32 Maximum 3.40282e+38 1.79769e+308 Minimum 1.40240e-45 4.94656e-324 Accuracy 6 d.p. 64 15 d.p. Lecture 4 : Slide 24 8 Variable Names • Java identifiers (names) : – Consist a letter followed by a sequence of zero or more underscores, letters or digits (this is not the whole truth!) – Cannot be Java keywords • Always use meaningful identifiers, follow conventions – Identifiers for constants are capitalised double PI = 3.14159; – Identifiers for variables start with a lower case letter int bananas = 42; – Capitalise first letter of words in multi-word identifiers int runsScored = 100; Lecture 4 : Slide 25 Syntax Diagram Java Letter Java Letter Java Digit Lecture 4 : Slide 26 Java Keywords abstract boolean break byte byvalue* case cast* catch char class const* continue default do double else extends false final finally float for future* generic* goto* if implements import inner* instanceof int interface long native new null operator* outer* package private protected public rest* return short static super switch synchronized this throw throws transient true try var* void volatile while * reserved for possible future use Lecture 4 : Slide 27 9 Symbolic Constants • Magic numbers make programs difficult to understand double volume = 4.188790204*area; • Use a symbolic constant final double FOUR_THIRDS_PI = 4.188790204; double volume = FOUR_THIRDS_PI*area; • Code is then easier to understand • Used meaningful, capitalised identifier with underscores between words Lecture 4 : Slide 28 Further Reading • John Lewis and William Loftus, “Java Software Solutions: Foundations of Program Design (third edition)”, AddisonWesley, ISBN 0-201-78129-8, 2002. – Sections 2.3-2.4. • David Flanagan, “Java in a Nutshell - a Desktop Quick Reference (third edition)”, O’Reilly, ISBN 1-56592-487-8, 2000. – Pages 20-26. Lecture 4 : Slide 29 Conclusions • In this lecture we have seen: – Java primitive data types – How to declare variables – How to initialise variables with literal constants • In the next lecture: – Operators – Expressions Lecture 4 : Slide 30 10