⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch5.htm

📁 Java_by_Example,初级经典例子哦,珍藏版本
💻 HTM
📖 第 1 页 / 共 4 页
字号:
<HTML><HEAD><TITLE>developer.com - Reference</TITLE><META NAME="Author" CONTENT="developer.com"><META NAME="Keywords" CONTENT="javascript, ActiveX, VRML, Push, channel,push channel, applet, directory, news, community, developer, internet,intranet, java, java applet, J++, javabeans, java products, tools, javatools, JDK, java development kit, java development environment, JIT,JavaPlan, enterprise tools, JVM, Java Virtual Machine, Java resources, API,browser, java documentation, CDF, Common Desktop Format, RDF, ResourceDescription Framework, Netcaster, Netcast, Castanet, meta-language, metadata,solaris, Solaris, SUN, CGI, Perl, Oracle, database, network, Netscape API,Apache, apache, SSL, mSQL, Netscape, Fasttrack, FastTrack,industry standard, ActiveX bridge, web, internet, site, sitedesign, microsoft, active server pages, asp, iis, nt, win95, 95, html, cgi,frontpage, business, corporate, vbscript, javascript, activex, isapi,database, access, sql, server, www, 3.0, 3.01, 3.02, 3.03, 3.04, 4.0,4.0b2, 4.01, 4.03, 4.04, connection, online, commerce, publisher,publishing, publish, webmaster, dynamic html, dhtml, windows 98, netscape,netscape software, shareware, magazines, jobs, japan, free, freeware, freestuff"><META NAME="description" CONTENT="Developer.com is the leading onlineservice for developers. It includes Gamelan, the official directory for Java, as wellas news, information, tutorials, and directories for other Internet technologiesincluding ActiveX, JavaScript, perl, VRML, Java Beans, push technologies, andother Internet and intranet technologies. Developer.com is also the home ofDeveloper Direct, the Professional Developer's Store, where developers canpurchase and download thousands of Web, authoring and other developmentproducts at some of today's lowest prices."><META NAME="distribution" CONTENT="global"><META NAME="resource-type" CONTENT="document"><META NAME="language" CONTENT="en"></HEAD><BODY BACKGROUND="/images/curve_r-wide.gif" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#DD0000" VLINK="#999999" ALINK="#FF0000"><TABLE><TR><TD VALIGN=TOP WIDTH=120><A HREF="/" ><IMG SRC="/images/masthead_r.gif" WIDTH="100" HEIGHT="78" BORDER="0"ALT="Developer.com Logo"></a></TD><TD><a target="resource window" href="http://adserver.developer.com/cgi-bin/accipiter/adclick.exe/AREA=DCBAN.REF/DT=null"><img src="http://adserver.developer.com/cgi-bin/accipiter/adserver.exe/AREA=DCBAN.REF/DT=null" HEIGHT="60" WIDTH="468" ALT="Click here to support our advertisers"><BR><CENTER>Click here to support our advertisers</CENTER></a></TD></TR></TABLE><!--outer table--><TABLE><!--outer table--><TR><!--outer table--><TD VALIGN="TOP"><!--navbar info --><table><tr><td><A HREF="http://www.developerdirect.com/" ><IMG SRC="/images/button_s.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><b>SHOPPING</b></font></a></td></tr><tr><td><A HREF="/community/jobs/c_jobs.html"><IMG SRC="/images/button_d.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><B>JOB BANK</B></font></a></td></tr><tr><td><A HREF="http://www.classifieds2000.com/cgi-cls/display.exe?Developer+computer+searchcomputercategories"><IMG SRC="/images/button_d.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><B>CLASSIFIEDS</B></font></a></td></tr><tr><td><A HREF="/directories/directories.html"><IMG SRC="/images/button_d.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><B>DIRECTORIES</B></font></a></td></tr><tr><td><A HREF="/reference/"><IMG SRC="/images/button_r.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><B>REFERENCE</B></font></a><br><a href="/reference/r_library.html"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000">Online Library</a></font></td></tr><tr><td><A HREF="/classroom/"><IMG SRC="/images/button_r.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><B>LEARNING CENTER</B></font></a></td></tr><tr><td><A HREF="/journal/"><IMG SRC="/images/button_n.gif" width="15" height="11" BORDER="0"><B><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000">JOURNAL</font></B></a></td></tr><tr><td><A HREF="/news/"><IMG SRC="/images/button_d.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><b>NEWS CENTRAL</b></font></a></td></tr><tr><td><A HREF="/downloads/"><IMG SRC="/images/button_d.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><b>DOWNLOADS</b></font></a></td></tr><tr><td><A HREF="/community/" ><IMG SRC="/images/button_c.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><B>COMMUNITY</B></font></a></td></tr><tr><td><A HREF="/calendar/"><IMG SRC="/images/button_d.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><b>CALENDAR</b></font></a></td></tr><tr><td><A HREF="/about/"><IMG SRC="/images/button_a.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><b>ABOUT US</b></a></font></td></tr></table><table><tr><td><form action="/subscribe/list.cgi" method=post ><img src="/images/rule.gif" width="100" height="2"><br><B><FONT FACE="Arial, Helvetica" SIZE="-1">Journal:</b></font><br><input name='email' value='your e-mail' size="11"><input type='hidden'  name='state' value='Subscribe'></td></tr><tr><td><input type='submit' value='Subscribe'><br><font size="-1">Get the weekly email highlights from the most popular journal for developers!<br><a href="http://www.developer.com/subscribe.html">Current issue</a><img src="/images/rule.gif" width="100" height="2"><input type='hidden' name='name' value='New Subscriber'></FORM></td></tr></table><!--/navbar--><font size="-1"><A HREF="http://www.developer.com">developer.com</a><BR><A HREF="http://www.developerdirect.com">developerdirect.com</a><BR><A HREF="http://www.htmlgoodies.com">htmlgoodies.com</a><BR><A HREF="http://www.javagoodies.com">javagoodies.com</a><br><A HREF="http://www.jars.com">jars.com</a><BR><a href="http://www.intranetjournal.com">intranetjournal.com</a><br><a href="http://www.javascripts.com">javascripts.com</a><br></font><!--outer table--></TD><!--outer table--><TD VALIGN="TOP"><BLOCKQUOTE><P><img src="/images/header_r.gif" width="440" height="50"><P><MAP NAME ="header_r2.gif.map"><AREA SHAPE ="RECT"      HREF ="/reference/author.html"      COORDS ="361,0,440,29"><AREA SHAPE ="RECT"      HREF ="/reference/basic.html"      COORDS ="254,0,360,29"><AREA SHAPE ="RECT"      HREF ="/reference/help.html"      COORDS ="183,0,251,29"><AREA SHAPE ="RECT"      HREF ="/reference/cool.html"      COORDS ="100,0,181,29"><AREA SHAPE ="RECT"      HREF ="/reference/mybook.html"      COORDS ="0,0,100,29"></MAP><MAP NAME ="header_r1.gif.map"><AREA SHAPE ="RECT"      HREF ="/reference/personalize.html"      COORDS ="339,0,439,29"></MAP><p><font face="Arial,Helvetica" size="-1" color="#006666"><A HREF="/reference/r_library.html"><B>All Categories</A> :</B><b><A HREF="/reference/r_java.html">Java</A></b></font><p><META><H1><FONT SIZE=6 COLOR=#Ff0000>Chapter&nbsp;5</FONT></H1><H1><FONT SIZE=6 COLOR=#Ff0000>Constants and Variables</FONT></H1><HR><P><CENTER><B><FONT SIZE=5><A NAME="CONTENTS">CONTENTS</A></FONT></B></CENTER><UL><LI><A HREF="#Constants">Constants</A><LI><A HREF="#Variables">Variables</A><LI><A HREF="#NamingConstantsandVariables">Naming Constants and Variables</A><LI><A HREF="#ExampleCreatingYourOwnIdentifiers">Example: Creating Your Own Identifiers</A><LI><A HREF="#DataTypes">Data Types</A><UL><LI><A HREF="#IntegerValues">Integer Values</A><LI><A HREF="#FloatingPointValues">Floating-Point Values</A><LI><A HREF="#CharacterValues">Character Values</A><LI><A HREF="#BooleanValues">Boolean Values</A></UL><LI><A HREF="#VariableScope">Variable Scope</A><UL><LI><A HREF="#ExampleDeterminingaVariablesScope">Example: Determining a Variable's Scope</A></UL><LI><A HREF="#Summary">Summary</A><LI><A HREF="#ReviewQuestions">Review Questions</A><LI><A HREF="#ReviewExercises">Review Exercises</A></UL><HR><P>If there's one thing that every computer program has in common,it's that they always process data input and produce some sortof output based on that data. And because data is so importantto a computer program, it stands to reason that there must beplenty of different ways to store data so that programs can dotheir processing correctly and efficiently. In order to keep trackof data, programs use constants and variables. In this chapter,you discover what constants and variables are, as well as learnto use them in the Java language.<H2><A NAME="Constants"><FONT SIZE=5 COLOR=#Ff0000>Constants</FONT></A></H2><P>If you think about the term &quot;constant&quot; for a few moments,you might conclude that constants must have something to do withdata that never changes. And your conclusion would be correct.A constant is nothing more than a value, in a program, that staysthe same throughout the program's execution. However, while thedefinition of a constant is fairly simple, constants themselvescan come in many different guises. For example, the numeral 2,when it's used in a line of program code, is a constant. If youplace the word &quot;Java&quot; in a program, the characters thatcomprise the word are also constants. In fact, these constantcharacters taken together are often referred to as a string constant.<P><CENTER><TABLE BORDER=1 WIDTH=80%><TR VALIGN=TOP><TD><B>NOTE</B></TD></TR><TR VALIGN=TOP><TD><BLOCKQUOTE>To be entirely accurate, I should say that text and numerals that are placed in program code are actually called <I>literals</I>, because the value is literally, rather than symbolically, in the program. If this literal and symbolic stuff is confusing you, you'll probably have it figured out by the end of this chapter. For now, just know that I'm lumping literals in with constants to simplify the discussion.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><P>Such values as the numeral 2 and the string constant &quot;Java&quot;are sometimes called hard-coded values because the values thatrepresent the constants are placed literally in the program code.For example, suppose you were writing a program and wanted tocalculate the amount of sales tax on a purchase. Suppose furtherthat the total purchase in question is $12.00 and the sales taxin your state is 6 percent. The calculation that'll give you thesales tax would look like this:<BLOCKQUOTE><PRE>tax = 12 * .06;</PRE></BLOCKQUOTE><P>Suppose now that you write a large program that uses the salestax percentage in many places. Then, after you've been happilyusing your program for a few months, the state suddenly decidesto raise the sales tax to seven percent. In order to get yourprogram working again, you have to go through every line of code,looking for the .06 values that represent the sales tax and changingthem to .07. Such a modification can be a great deal of work ina large program. Worse, you may miss one or two places in thecode that need to be changed, leaving your program with some seriousbugs.<P>To avoid these situations, programmers often use something calledsymbolic constants, which are simply words that represent valuesin a program. In the case of your sales tax program, you couldchoose a word like <TT>SALESTAX</TT> (no spaces) to representthe current sales tax percentage for your state. Then, at thebeginning of your program, you set <TT>SALESTAX</TT> to be equalto the current state sales tax. In the Java language, such a lineof program code might look like this:<BLOCKQUOTE><PRE>final float SALESTAX = 0.06;</PRE></BLOCKQUOTE><P>In the preceding line, the word <TT>final</TT> tells Java thatthis data object is going to be a constant. The <TT>float</TT>is the data type, which, in this case, is a floating point. (You'lllearn more about data types later in this chapter.) The word <TT>SALESTAX</TT>is the symbolic constant. The equals sign tells Java that theword on the left should be equal to the value on the right, which,in this case, is 0.06.<P>After defining the symbolic constant <TT>SALESTAX</TT>, you canrewrite any lines that use the current sales tax value to usethe symbolic constant rather than the hard-coded value. For example,the calculation for the sales tax on that $12.00 purchase mightnow look something like this:<BLOCKQUOTE><PRE>tax = 12 * SALESTAX;<BR></PRE></BLOCKQUOTE><P><CENTER><TABLE BORDER=1 WIDTH=80%><TR VALIGN=TOP><TD><B>TIP</B></TD></TR><TR VALIGN=TOP><TD><BLOCKQUOTE>In order to differentiate symbolic constants from other values in a program, programmers often use all uppercase letters when naming these constants.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><P>Now, when your state changes the sales tax to 7 percent, you needonly change the value you assign to the symbolic constant andthe rest of the program automatically fixes itself. The changewould look like this:<BLOCKQUOTE><PRE>final float SALESTAX = 0.07;</PRE></BLOCKQUOTE><H2><A NAME="Variables"><FONT SIZE=5 COLOR=#Ff0000>Variables</FONT></A></H2><P>If constants are program values that cannot be changed throughoutthe execution of a program, what are variables? Variables arevalues that can change as much as needed during the executionof a program. Because of a variable's changing nature, there'sno such thing as a hard-coded variable. That is, hard-coded valuesin a program are always constants (or, more accurately, literals).<P>Why do programs need variables? Think back to the sales tax programfrom the previous section. You may recall that you ended up witha program line that looked like this:<BLOCKQUOTE><PRE>tax = 12 * SALESTAX;</PRE></BLOCKQUOTE><P>In this line, the word <TT>tax</TT> is a variable. So, one reason

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -