📄 ch36.htm
字号:
<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 36</FONT></H1><H1><FONT SIZE=6 COLOR=#Ff0000>The Java Class Libraries</FONT></H1><HR><P><CENTER><B><FONT SIZE=5><A NAME="CONTENTS">CONTENTS</A></FONT></B></CENTER><UL><LI><A HREF="#ThePackages">The Packages</A><LI><A HREF="#TheIjavalangIPackage">The <I>java.lang</I> Package</A><UL><LI><A HREF="#DataTypeWrappers">Data-Type Wrappers</A><LI><A HREF="#ExampleUsingtheDataTypeWrappers">Example: Using the Data-Type Wrappers</A><LI><A HREF="#TheISystemIClass">The <I>System</I> Class</A><LI><A HREF="#ExampleGettingSystemProperties">Example: Getting System Properties</A><LI><A HREF="#TheIMathIClass">The <I>Math</I> Class</A><LI><A HREF="#TheIStringIClass">The <I>String </I>Class</A><LI><A HREF="#ExampleUsingtheIStringIClass">Example: Using the <I>String</I> Class</A></UL><LI><A HREF="#TheIioIPackage">The <I>io</I> Package</A><UL><LI><A HREF="#ExampleReadingaFile">Example: Reading a File</A></UL><LI><A HREF="#TheIawtIPackage">The <I>awt</I> Package</A><LI><A HREF="#Summary">Summary</A><LI><A HREF="#ReviewQuestions">Review Questions</A><LI><A HREF="#ReviewExercises">Review Exercises</A></UL><HR><P>This book has given you a peek into the process of creating appletswith Java. However, the key word is "peek" because Javais a huge development system that couldn't be fully covered ina book twice this size. For this reason, now that you have someJava programming experience under your belt, it's time to setoff on your own to discover how much more you can do with Java.The first step in that journey is to explore the class librariesthat come with Java. You'll discover all sorts of treasures there.<P>To give you a nudge in the right direction, this final chapterprovides a brief overview of Java's most important class libraries.However, you should take it upon yourself to explore the latestdocumentation available from Sun at their Web site, as well asto peruse Java's source code. The language and its classes arechanging constantly, so you have to make an effort to keep up.<H2><A NAME="ThePackages"><FONT SIZE=5 COLOR=#Ff0000>The Packages</FONT></A></H2><P>The Java class libraries are divided into two groups. The firstgroup is the Java packages, which include the libraries for theJava programming language. These packages include the following:<UL><LI>java.lang<LI>java.util<LI>java.io</UL><P>The second group is called the HotJava packages and includes thelibraries needed to create applets and to communicate over theInternet. The HotJava packages include the following:<UL><LI>java.awt<LI>java.applet<LI>java.net</UL><P>In this chapter, you'll get a brief look at some of these packagesand the classes they contain.<H2><A NAME="TheIjavalangIPackage"><FONT SIZE=5 COLOR=#Ff0000>The <I>java.lang</I> Package</FONT></A></H2><P>Although you may not been aware of it, you've been using the <TT>lang</TT>package since the beginning of this book. That's because thisis the one package that Java automatically imports into everyprogram. Without the <TT>lang</TT> package, you wouldn't be ableto write Java programs, because this package contains the librariesthat make Java what it is. Table 36.1 is a list of the commonlyused classes included in the <TT>lang</TT> package.<BR><P><CENTER><B>Table 36.1 Commonly Used Classes in the</B><I>java.lang</I><B> Package.</B></CENTER><P><CENTER><TABLE BORDER=1 WIDTH=80%><TR VALIGN=TOP><TD WIDTH=138><I><B>Class</B></I></TD><TD WIDTH=453><I><B>Description</B></I></TD></TR><TR VALIGN=TOP><TD WIDTH=138><TT>Boolean</TT></TD><TD WIDTH=453>Represents the <TT>boolean</TT> data type.</TD></TR><TR VALIGN=TOP><TD WIDTH=138><TT>Character</TT></TD><TD WIDTH=453>Represents the <TT>char</TT> data type.</TD></TR><TR VALIGN=TOP><TD WIDTH=138><TT>Double</TT></TD><TD WIDTH=453>Represents the <TT>double</TT> data type.</TD></TR><TR VALIGN=TOP><TD WIDTH=138><TT>Float</TT></TD><TD WIDTH=453>Represents the <TT>float</TT> data type.</TD></TR><TR VALIGN=TOP><TD WIDTH=138><TT>Integer</TT></TD><TD WIDTH=453>Represents the <TT>int</TT> data type.</TD></TR><TR VALIGN=TOP><TD WIDTH=138><TT>Long</TT></TD><TD WIDTH=453>Represents the <TT>long</TT> data type.</TD></TR><TR VALIGN=TOP><TD WIDTH=138><TT>Math</TT></TD><TD WIDTH=453>Contains methods that implement mathematical functions.</TD></TR><TR VALIGN=TOP><TD WIDTH=138><TT>Number</TT></TD><TD WIDTH=453>The superclass for all number-related classes, such as <TT>Float</TT> and <TT>Integer</TT>.</TD></TR><TR VALIGN=TOP><TD WIDTH=138><TT>Object</TT></TD><TD WIDTH=453>The root of the entire class library. All Java classes can trace their ancestry back to <TT>Object</TT>.</TD></TR><TR VALIGN=TOP><TD WIDTH=138><TT>String</TT></TD><TD WIDTH=453>Represents text strings.</TD></TR><TR VALIGN=TOP><TD WIDTH=138><TT>StringBuffer</TT></TD><TD WIDTH=453>Represents a string buffer that can grow dynamically.</TD></TR><TR VALIGN=TOP><TD WIDTH=138><TT>System</TT></TD><TD WIDTH=453>Contains methods for performing system-level function calls.</TD></TR><TR VALIGN=TOP><TD WIDTH=138><TT>Thread</TT></TD><TD WIDTH=453>The superclass from which thread objects are derived.</TD></TR></TABLE></CENTER><P><P>Of these classes, the ones that are most useful to you at thistime are the data-type wrappers-<TT>Boolean</TT>, <TT>Character</TT>,<TT>Double</TT>, <TT>Float</TT>, <TT>Integer</TT>, <TT>Long</TT>-,as well as <TT>String</TT>, <TT>Math</TT>, <TT>System</TT>, and<TT>Thread</TT>. The following sections provide general descriptionsand usage tips for these classes-except for <TT>Thread</TT>, whichyou learned about in <A HREF="ch31.htm" >Chapter 31</A>, "Threads."<P><CENTER><TABLE BORDER=1 WIDTH=80%><TR VALIGN=TOP><TD><B>NOTE</B></TD></TR><TR VALIGN=TOP><TD><BLOCKQUOTE>The java.lang package also includes the <TT>Runnable</TT> interface, which is used to convert classes into threads. For more information on this topic, see <A HREF="ch31.htm" >Chapter 31</A>, "Threads."</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><H3><A NAME="DataTypeWrappers">Data-Type Wrappers</A></H3><P>The data-type wrapper classes enable you to perform various operationson values in your programs. For example, in previous programsin this book, you've used the <TT>Integer.parseInt()</TT> methodto convert strings containing digits to integer values, like this:<BLOCKQUOTE><PRE>int value = Integer.parseInt(str);</PRE>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -