📄 ch5.htm
字号:
byte identifier;</PRE></BLOCKQUOTE><P>In the preceding line, <TT>byte</TT> is the data type for thevalue, and identifier is the variable's name. You can also simultaneouslydeclare and assign a value to a variable like this:<BLOCKQUOTE><PRE>byte count = 100;</PRE></BLOCKQUOTE><P>After Java executes the preceding line, your program will havea variable named <TT>count</TT> that currently holds the valueof 100. Of course, you can change the contents of <TT>count</TT>at any time in your program. It only starts off holding the value100.<P>The next biggest type of Java integer is <TT>short</TT>. A variabledeclared as <TT>short</TT> can hold a value from -32,768 to 32,767.You declare a <TT>short</TT> value like this:<BLOCKQUOTE><PRE>short identifier;</PRE></BLOCKQUOTE><P>or<BLOCKQUOTE><PRE>short identifier = value;</PRE></BLOCKQUOTE><P>In the preceding line, value can be any value from -32,768 to32,767, as described previously. In Java, <TT>short</TT> valuesare twice as big in memory-16 bits (or two bytes)-as <TT>byte</TT>values.<P>Next in the integer data types is <TT>int</TT>, which can holda value from -2,147,483,648 to 2,147,483,647. Now you're gettinginto some big numbers! The <TT>int</TT> data type can hold suchlarge numbers because it takes up 32 bits (four bytes) of computermemory. You declare <TT>int</TT> values like this:<BLOCKQUOTE><PRE>int identifier;</PRE></BLOCKQUOTE><P>or<BLOCKQUOTE><PRE>int identifier = value;</PRE></BLOCKQUOTE><P>The final integer data type in the Java language is <TT>long</TT>,which takes up a whopping 64 bits (eight bytes) of computer memoryand can hold truly immense numbers. Unless you're calculatingthe number of molecules in the universe, you don't even have toknow how big a <TT>long</TT> number can be. I'd figure it outfor you, but I've never seen a calculator that can handle numbersthat big. You declare a <TT>long</TT> value like this:<BLOCKQUOTE><PRE>long identifier;</PRE></BLOCKQUOTE><P>or<BLOCKQUOTE><PRE>long identifier = value;<BR></PRE></BLOCKQUOTE><P><CENTER><TABLE BORDER=1 WIDTH=80%><TR VALIGN=TOP><TD><B>TIP</B></TD></TR><TR VALIGN=TOP><TD><BLOCKQUOTE>How do you know which integer data type to use in your program? Choose the smallest data type that can hold the largest numbers you'll be manipulating. Following this rule keeps your programs running as fast as possible. However, having said that, I should tell you that most programmers (including me) use the <TT>int</TT> data type a lot, even when they can get away with a <TT>byte</TT>.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><H3><A NAME="FloatingPointValues">Floating-Point Values</A></H3><P>Whereas integer values can hold only whole numbers, the floating-pointdata types can hold values with both whole number and fractionalparts. Examples of floating-point values include 32.9, 123.284,and -43.436. As you can see, just like integers, floating-pointvalues can be either positive or negative.<P>Java includes two floating-point types, which are <TT>float</TT>and <TT>double</TT>. Each type allows greater precision in calculations.What does this mean? Floating-point numbers can become very complexwhen they're used in calculations, particularly in multiplicationand division. For example, when you divide 3.9 by 2.7, you get1.44444444. In actuality, though, the fractional portion of thenumber goes on forever. That is, if you were to continue the divisioncalculation, you'd discover that you keep getting more and morefours in the fractional part of the answer. The answer to 3.9divided by 2.7 is not really 1.44444444, but rather somethingmore like 1.4444444444444444. But even that answer isn't completelyaccurate. A more accurate answer would be 1.44444444444444444444444444444444.The more 4s you add to the answer the more accurate the answerbecomes-yet, because the 4s extend on into infinity, you can neverarrive at a completely accurate answer.<P>Dealing with floating-point values frequently means deciding howmany decimal places in the answer is accurate enough. That's wherethe difference between the <TT>float</TT> and <TT>double</TT>data types shows up. In Java, a value declared as <TT>float</TT>can hold a number in the range from around -3.402823 x 10 38 toaround 3.402823 x 10 38. These types of values are also knownas single-precision floating-point numbers and take up 32 bits(four bytes) of memory. You declare a single-precision floating-pointnumber like this:<BLOCKQUOTE><PRE>float identifier;</PRE></BLOCKQUOTE><P>or<BLOCKQUOTE><PRE>float identifier = value;</PRE></BLOCKQUOTE><P>In the second line, value must be a value in the range given inthe previous paragraph, followed by an upper- or lowercase <TT>F</TT>.However, you can write floating-point numbers in a couple of ways,using regular digits and a decimal point or using scientific notation.This value is the type of floating-point number you're used toseeing:<BLOCKQUOTE>356.552</BLOCKQUOTE><P>Now, here's the same number written using Java's rules, in boththe number's normal form and in the form of scientific notation:<BLOCKQUOTE>356.552f<BR>3.56552e2f</BLOCKQUOTE><P>Both of the preceding values are equivalent, and you can use eitherform in a Java program. The <TT>e2</TT> in the second exampleis the equivalent of writing <TT>x 102</TT> and is a short formof scientific notation that's often used in programming languages.<P><CENTER><TABLE BORDER=1 WIDTH=80%><TR VALIGN=TOP><TD><B>NOTE</B></TD></TR><TR VALIGN=TOP><TD><BLOCKQUOTE>If you're not familiar with scientific notation, the value 3.402823 x 10 38 is equal to 3.402823 times a number that starts with a 1 and is followed by 38 zeroes. Computer languages shorten this scientific notation to 3.402823e38.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><P>The second type of floating-point data, <TT>double</TT>, representsa double-precision value, which is a much more accurate representationof floating-point numbers because it allows for more decimal places.A <TT>double</TT> value can be in the range from -1.79769313486232x 10 308 to 1.79769313486232 x 10 308 and is declared like this:<BLOCKQUOTE><PRE>double identifier;</PRE></BLOCKQUOTE><P>or<BLOCKQUOTE><PRE>double identifier = value;</PRE></BLOCKQUOTE><P>Floating-point values of the <TT>double</TT> type are writtenexactly as their <TT>float</TT> counterparts, except you use anupper- or lowercase <TT>D</TT> as the suffix, rather than an <TT>F</TT>.Here's a few examples:<BLOCKQUOTE>3.14d<BR>344.23456D<BR>3.4423456e2d<BR></BLOCKQUOTE><P><CENTER><TABLE BORDER=1 WIDTH=80%><TR VALIGN=TOP><TD><B>TIP</B></TD></TR><TR VALIGN=TOP><TD><BLOCKQUOTE>When using floating-point numbers in your programs, the same rule that you learned about integers applies: Use the smallest data type you can. This is especially true for floating-point numbers, which are notorious for slowing computer programs to a crawl. Unless you're doing highly precise programming, such as 3-D modeling, the single-precision <TT>float</TT> data type should do just fine.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><H3><A NAME="CharacterValues">Character Values</A></H3><P>Often in your programs, you'll need a way to represent charactervalues rather than just numbers. A character is a symbol that'sused in text. The most obvious examples of characters are theletters of the alphabet, in both upper- and lowercase varieties.There are, however, many other characters, including not onlythings such as spaces, exclamation points, and commas, but alsotabs, carriage returns, and line feeds. The symbols 0 through9 are also characters when they're not being used in mathematicalcalculations.<P>In order to provide storage for character values, Java featuresthe <TT>char</TT> data type, which is 16 bits. However, the sizeof the <TT>char</TT> data type has little to do with the valuesit can hold. Basically, you can think of a <TT>char</TT> as beingable to hold a single character. (The 16 bit length accommodatesUnicode characters, which you don't need to worry about in thisbook.) You declare a <TT>char</TT> value like this:<BLOCKQUOTE><PRE>char c;</PRE></BLOCKQUOTE><P>or<BLOCKQUOTE><PRE>char c = 'A';</PRE></BLOCKQUOTE><P>In the second example, you're not only declaring the variable<TT>c</TT> as a <TT>char</TT>, but also setting its value to anuppercase A. Notice that the character that's being assigned isenclosed in single quotes.<P>Some characters cannot be written with only a single symbol. Forexample, the tab character is represented in Java as <TT>\t</TT>,which is a backslash followed by a lowercase <TT>t</TT>. Thereare several of these special characters, as shown in Table 5.1.<BR><P><CENTER><B>Table 5.1 Special Character Literals.</B></CENTER><P><CENTER><TABLE BORDER=1 WIDTH=80%><TR VALIGN=TOP><TD WIDTH=193><I><B>Character</B></I></TD><TD WIDTH=150><CENTER><I><B>Symbol</B></I></CENTER></TD></TR><TR VALIGN=TOP><TD WIDTH=193>Backslash</TD><TD WIDTH=150><CENTER><TT>\\</TT></CENTER></TD></TR><TR VALIGN=TOP><TD WIDTH=193>Backspace</TD><TD WIDTH=150><CENTER><TT>\b</TT></CENTER></TD></TR><TR VALIGN=TOP><TD WIDTH=193>Carriage return</TD><TD WIDTH=150><CENTER><TT>\r</TT></CENTER></TD></TR><TR VALIGN=TOP><TD WIDTH=193>Double quote</TD><TD WIDTH=150><CENTER><TT>\"</TT></CENTER></TD></TR><TR VALIGN=TOP><TD WIDTH=193>Form feed</TD><TD WIDTH=150><CENTER><TT>\f</TT></CENTER></TD></TR><TR VALIGN=TOP><TD WIDTH=193>Line feed</TD><TD WIDTH=150><CENTER><TT>\n</TT></CENTER></TD></TR><TR VALIGN=TOP><TD WIDTH=193>Single quote</TD><TD WIDTH=150><CENTER><TT>\'</TT></CENTER></TD></TR><TR VALIGN=TOP><TD WIDTH=193>Tab</TD><TD WIDTH=150><CENTER><TT>\t</TT></CENTER></TD></TR></TABLE></CENTER><P>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -