data types copyright © 2006 patrick mcdermott college of alameda [email protected]
TRANSCRIPT
Fact CRC Cards: (Attribute) ERD: Attribute Relational: Attribute Database: Column Programmer: Variable UML: Property Etc., etc., etc.…: Field, Item, Element
The Rules C++
• Allowed/Forbidden characters– Letters, Numerals, _– NO Spaces
• Break_character: Underscore _
– No initial numeral
• Length, Very Long• Case SeNsItiVe• Cannot be a Keyword (Keywords are Reserved Words)
Nouns for Variables/ClassesVerbs for Functions/Methods
The Rules Java
• Allowed/Forbidden characters– Letters, Numerals, $, _– NO Spaces
• Break_character: Underscore _
– No initial numeral
• Length, Very Long• Case SeNsItiVe• Cannot be a Keyword (Keywords are Reserved Words)
Nouns for Variables/ClassesVerbs for Functions/Methods
Numbers, Text & Truth
• Numbers– int or double
• Text– char or string– C#: char is 2 bytes in C++ wchar_t
• Booleans– Logical: true or false.
• We might not have beauty, but it does have truth.
• Decimal (.NET)– ± $792,281,625,142,643,375,935,439,503.35m – ± 79,228,162,514,264,337,593,543,950,335M
2 Kinds of NumbersIntegers
• int• A counting number• A whole number• Discrete• No fraction• No decimal• 2 3 9
Floating Point
• float, but use double• A measuring number• A fractional number• Continuous• Fraction• Decimal• 2½ , 3.14159, 99.99• 3.0 too!
Numbers• Integers: int
@ 4 billion
–2,147,483,648 to +2,147,483,647• 0-4.3 billion if unsigned
Not enough bils for Bill’s fortune
• Double-Precision Floating-Point: double– Range: Big enough
• You can enumerate all the atoms in the Universe.
– Precision: at least 15 decimal digits• Just enough for $Trillion to the cent.• $1,234,567,890,123.45• Not quite enough for a credit card number
I Declare!String s;String s = "Hi";int x = 0;double x, y;int x = 0, y = 1;final int MYNUMBER = 0;
The Unchanging constant
ALLUPPERCASE or ALL_UPPER_CASE
Number versus Character Sort
As numbers, one & two & three are binary 1 and 10 and 11.As text, 1 & 2 & 3 are ASCII characters 49 & 50 & 51.
Numbers sort by value.Text sorts character by character.
You can be richer than Bill Gates!As text, $9 is greater than $80,000,000,000!So, just keep $9, and sort net worth as text!
# Raw Numbers Text Text Sort1 1 1 1 12 111 2 11 113 2 11 111 1114 49 17 123 1235 423 22 17 176 11 49 2 27 22 111 22 228 313 123 313 3139 17 313 423 423
10 123 423 49 49
Precision• Values of type float have seven digits of
precision.• Values of type double have 15-16 digits
of precision.• Values of type decimal are represented as
integer values that are scaled by a power of 10. Values between -1.0 and 1.0 are represented exactly to 28 digits.
Size not Standard
Each computer system is different.Types are based on the system Word.We are on a 32-bit (4-byte) system,So int is a word (4 bytes), double is 2 words (8 bytes)…
Type Size
char, unsigned char, signed char 1 byte
short, unsigned short 2 bytes
int, unsigned int 4 bytes
long, unsigned long 4 bytes
float 4 bytes
double 8 bytes
long double 8 bytes
The incredible Growing int
Windows 3.11(and earlier)
The 16-bit WindowsA 2-byte system word
Windows 95(and later)
The 32-bit WindowsA 4-byte system DWord
C Standard for int:The short must not be longer than the intThe long must not be shorter than the int
short
int
long
short
int
long
Windows 7 & Java(and later)
The 64-bit WindowsA 8-byte system QWord
short
int
long
Alphanumeric Text Strings
• Single character or a string of characters?
'a' is the single character known as a
"a" is a character string consisting of the letter a• A C string is actually a null-terminated array
It needs a space for the null (binary zeroes)
"a" is 2 characters long: a + null• lpsz “long pointer to a null-terminated string” [yuck!]
• String TheoryStrings have been a sore point, with me & others
Unnecessarily overcomplicated
What were they thinking???