📄 ch01.htm
字号:
<TD ALIGN="LEFT"><I>Data Type</I></TD> <TD ALIGN="LEFT"><I>Size in Bytes</I></TD> <TD ALIGN="LEFT"><I>Possible Range of Values</I></TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">ShortInt</TD> <TD ALIGN="LEFT">1</TD> <TD ALIGN="LEFT">-128 to 127</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">Byte</TD> <TD ALIGN="LEFT">1</TD> <TD ALIGN="LEFT">0 to 255</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">Char</TD> <TD ALIGN="LEFT">1</TD> <TD ALIGN="LEFT">0 to 255 (same as Byte)</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">WideChar</TD> <TD ALIGN="LEFT">2</TD> <TD ALIGN="LEFT">0 to 65,535 (same as Word)</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">SmallInt</TD> <TD ALIGN="LEFT">2</TD> <TD ALIGN="LEFT">-32,768 to 32,767</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">Word</TD> <TD ALIGN="LEFT">2</TD> <TD ALIGN="LEFT">0 to 65,535</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">LongInt</TD> <TD ALIGN="LEFT">4</TD> <TD ALIGN="LEFT">-2,147,483,648 to 2,147,483,647</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">Int64</TD> <TD ALIGN="LEFT">8</TD> <TD ALIGN="LEFT">-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">Integer</TD> <TD ALIGN="LEFT">4</TD> <TD ALIGN="LEFT">Same as LongInt</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">Cardinal</TD> <TD ALIGN="LEFT">4</TD> <TD ALIGN="LEFT">0 to 2,147,483,647</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">Single</TD> <TD ALIGN="LEFT">4</TD> <TD ALIGN="LEFT">1.5 ¥ 10<SUP>-45</SUP> to 3.4 ¥ 10<SUP>38</SUP></TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">Double</TD> <TD ALIGN="LEFT">8</TD> <TD ALIGN="LEFT">5.0 ¥ 10<SUP>-324</SUP> to 1.7 ¥ 10<SUP>308</SUP></TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">Real</TD> <TD ALIGN="LEFT">8</TD> <TD ALIGN="LEFT">5.0 ¥ 10<SUP>-324</SUP> to 1.7 ¥ 10<SUP>308</SUP> (same as Double)</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">Extended</TD> <TD ALIGN="LEFT">10</TD> <TD ALIGN="LEFT">3.4 ¥ 10<SUP>-4932</SUP> to 1.1 ¥ 10<SUP>4932</SUP></TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">Comp</TD> <TD ALIGN="LEFT">8</TD> <TD ALIGN="LEFT">-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">Currency</TD> <TD ALIGN="LEFT">8</TD> <TD ALIGN="LEFT">-922,337,203,685,477.5808 to 922,337,203,685,477.5807</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">Boolean</TD> <TD ALIGN="LEFT">1</TD> <TD ALIGN="LEFT">True or False</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">Variant</TD> <TD ALIGN="LEFT">16</TD> <TD ALIGN="LEFT">Varies</TD> </TR></TABLE></P><P>Examining Table 1.1, you might notice that an Integer is the same as a LongInt.So why does Object Pascal have two different data types that are exactly the same?Essentially, it's a holdover from days gone by. In a 16-bit programming environment,an Integer requires 2 bytes of storage and a LongInt requires 4 bytes of storage.</P><P>In a 32-bit programming environment, however, both require 4 bytes of storageand have the same range of values. Delphi 4 produces only 32-bit programs, so anInteger and a LongInt are identical. Most programmers use Integer rather than LongInt.</P><P>You might also notice that the Int64 and Comp (computational) types have an identicalrange of values. The difference between these two types is in the way they are treatedinternally by the compiler. The Int64 type is an integer type, whereas the Comp typeis a real type. Probably you will have very little reason to use the Comp type inyour programs.</P><P>Notice also that the Real and Double data types are identical. In previous versionsof Delphi, the Real type was a 6-byte variable. Now it is an 8-byte variable. Thischange was made to make the Real data type compatible with today's processors. TheReal type is considered obsolete and you should use Double rather than Real in yourDelphi applications.</P><BLOCKQUOTE> <P><HR><strong>NOTE:</strong> The Int64 data type is new to Delphi 4. There are many reasons for an integer type of this size. One of the most compelling is the need for an integer value that can hold the huge values required by today's larger hard drives. For example, Windows contains a function called GetDiskFreeSpaceEx, which can return values much larger than 2,147,483,647 (the maximum value of an Integer). A 64-bit integer data type was needed for reasons like this. <HR><BR> <HR><strong>NOTE:</strong> The Single, Double, Extended, and Currency data types use floating-point numbers (numbers with decimal places). The other data types deal only with integer values. You cannot assign a value containing a decimal fraction to an integer data type. For example, the following code will generate a compiler error:</P> <PRE>var X : Integer;{ Later... }X := 3.75;</PRE></BLOCKQUOTE><PRE></PRE><BLOCKQUOTE> <P>You don't really have to worry about this too much, because the compiler is very good at telling you what you can and cannot do. By the way, you'd be surprised how few times you need floating-point numbers in most Windows programs. <HR></BLOCKQUOTE><H3></H3><H4>Converting Between Data Types</H4><P>Object Pascal performs conversion between different data types when possible.Take the following code snippet for an example:</P><P><PRE>var Res : SmallInt; Num1 : Integer; Num2 : Integer;{ Later... } Num1 := 200; Num2 := 200; Res := Num1 * Num2;</PRE><P>In this case I am trying to assign the result of multiplying two Integers to aSmallInt. Even though this formula mixes two data types, Object Pascal is able toperform a conversion. Would you like to take a guess at the result of this calculation?You might be surprised to find out that the result is -25,536. What!? If you lookat Table 1.1, you'll see that a SmallInt can have a maximum value of 32,767. Whathappens if you take a SmallInt with a value of 32,767 and add 1 to it? You will geta value of -32,768. This is essentially the same as the odometer on a car turningover from 99,999 to 00,000 when you drive that last mile. To illustrate, performthe following steps:</P><DL> <DT></DT> <DD><B>1. </B>Start with a new application and place a label and button on the form. <P> <DT></DT> <DD><B>2. </B>Double-click the button to create an event handler for the button's OnClick event. <P> <DT></DT> <DD><B>3. </B>Modify the event handler so that it looks like this: <P></DL><BLOCKQUOTE> <PRE>procedure TForm1.Button1Click(Sender: TObject);var X : SmallInt;begin X := 32767; X := X + 1; Label1.Caption := IntToStr(X);end; </PRE></BLOCKQUOTE><PRE></PRE><DL> <DT></DT> <DD><B>4. </B>Run the program and click the button. <P></DL><P>You should see the caption of the label change to -32768 when you click the button(in case you wondering, the IntToStr function translates an integer value to a string).This exercise illustrates that 32767 plus 1 equals -32768! Okay, maybe not quite.</P><P>This example really illustrates what is known as <I>overflow</I> or <I>wrapping</I>.You should be aware of the maximum possible values your variables can contain andchoose the data type that is large enough to guarantee that the variable will containthe value without overflowing. For the most part, you won't go too far wrong if youuse the Integer data type as your data type of choice. You are unlikely to run intothe problem of wrapping because the Integer data type gives you an approximate rangeof -2 billion to +2 billion.</P><P>Okay, where was I? Oh, yes, I was talking about automatic type conversion. Insome cases, Object Pascal cannot perform a conversion. If that is the case, you willget a compiler error that says something along the lines of Incompatible types: `Integer'and `Real'. This compiler error is telling you that you are trying to assign a valuethat cannot be stored by this particular data type. Another compiler error you mightsee has to do with what is called <I>range checking</I>. Take this code, for instance:</P><P><PRE>var X : Byte;begin X := 1000;end; </PRE><P>This code will generate a compiler error that states Constant expression violatessubrange bounds. The compiler is telling you that you can't assign a value of 1000to the variable X because X is declared as a Byte and a Byte can only hold valuesfrom 0 to 255.</P><BLOCKQUOTE> <P><HR><strong>TIP:</strong> Learn to treat compiler hints and warnings as errors. The compiler is trying to tell you that something is not quite right in your code, and you need to respect that warning. Ultimately, you should strive for warning-free compiles. In rare cases, a warning cannot be avoided, but be sure to examine all warnings closely. Do your best to understand the reason for the warning and correct it if possible. <HR></BLOCKQUOTE><H3></H3><H3><A NAME="Heading18"></A>Object Pascal Operators</H3><P><I>Operators</I> are used to manipulate data. Operators perform calculations,check for equality, make assignments, manipulate variables, and perform other, moreesoteric duties that most programmers never do. There are a lot of operators in ObjectPascal. Rather than present them all here, I will list only the most commonly usedones. Table 1.2 contains a list of those operators.</P><P><H4>TABLE 1.2. COMMONLY USED OBJECT PASCAL OPERATORS.</H4><P><TABLE BORDER="1"> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT"><I>Operator</I></TD> <TD ALIGN="LEFT"><I>Description</I></TD> <TD ALIGN="LEFT"><I>Example</I></TD> </TR> <TR ALIGN="CENTER" VALIGN="TOP"> <TD ALIGN="CENTER" COLSPAN="3"> <P ALIGN="CENTER"><B>Mathematical Operators</B> </TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">+</TD> <TD ALIGN="LEFT">Addition</TD> <TD ALIGN="LEFT">x := y + z;</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">-</TD> <TD ALIGN="LEFT">Subtraction</TD> <TD ALIGN="LEFT">x := y - z;</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">*</TD> <TD ALIGN="LEFT">Multiplication</TD> <TD ALIGN="LEFT">x := y * z;</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">/</TD> <TD ALIGN="LEFT">Real number division</TD> <TD ALIGN="LEFT">x := y / 3.14;</TD> </TR
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -