﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>语源科技BlogJava-活在JAVA岛的快乐日子</title><link>http://www.blogjava.net/hmily1211/</link><description /><language>zh-cn</language><lastBuildDate>Thu, 30 Apr 2026 13:12:03 GMT</lastBuildDate><pubDate>Thu, 30 Apr 2026 13:12:03 GMT</pubDate><ttl>60</ttl><item><title>JSP内建对象- - </title><link>http://www.blogjava.net/hmily1211/archive/2006/02/12/30332.html</link><dc:creator>活在JAVA岛的日子</dc:creator><author>活在JAVA岛的日子</author><pubDate>Sun, 12 Feb 2006 03:35:00 GMT</pubDate><guid>http://www.blogjava.net/hmily1211/archive/2006/02/12/30332.html</guid><wfw:comment>http://www.blogjava.net/hmily1211/comments/30332.html</wfw:comment><comments>http://www.blogjava.net/hmily1211/archive/2006/02/12/30332.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/hmily1211/comments/commentRss/30332.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/hmily1211/services/trackbacks/30332.html</trackback:ping><description><![CDATA[JSP内建对象- -&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
<P>
<P><FONT face="Courier New"><STRONG>① out - javax.servlet.jsp.jspWriter</STRONG><BR>&nbsp;&nbsp; out对象用于把结果输出到网页上。</FONT></P>
<P><FONT face="Courier New">方法：<BR>1. void clear() ;<BR>&nbsp;&nbsp; 清除输出缓冲区的内容，但是不输出到客户端。</FONT></P>
<P><FONT face="Courier New">2. void clearBuffer() ;<BR>&nbsp;&nbsp; 清除输出缓冲区的内容，并输出到客户端。</FONT></P>
<P><FONT face="Courier New">3. void close() ;<BR>&nbsp;&nbsp; 关闭输出流，清除所有内容。</FONT></P>
<P><FONT face="Courier New">4. void flush() ;<BR>&nbsp;&nbsp; 输出缓冲区里面的数据。</FONT></P>
<P><FONT face="Courier New">5. int getBufferSize() ;<BR>&nbsp;&nbsp; 获取以kb为单位的目前缓冲区大小。</FONT></P>
<P><FONT face="Courier New">6. int getRemaining() ;<BR>&nbsp;&nbsp; 获取以kb为单位的缓冲区中未被占用的空间大小。</FONT></P>
<P><FONT face="Courier New">7. boolean isAutoFlush() ;<BR>&nbsp;&nbsp; 是否自动刷新缓冲区。</FONT></P>
<P><FONT face="Courier New">8. void newLine() ;<BR>&nbsp;&nbsp; 输出一个换行字符。</FONT></P>
<P><FONT face="Courier New">9. void print( boolean b ) ;<BR>&nbsp;&nbsp; void print( char c ) ;<BR>&nbsp;&nbsp; void print( char[] s ) ;<BR>&nbsp;&nbsp; void print( double d ) ;<BR>&nbsp;&nbsp; void print( float f ) ;<BR>&nbsp;&nbsp; void print( int i ) ;<BR>&nbsp;&nbsp; void print( long l ) ;<BR>&nbsp;&nbsp; void print( Object obj ) ;<BR>&nbsp;&nbsp; void print( String s ) ;<BR>&nbsp;&nbsp; 将指定类型的数据输出到Http流，不换行。</FONT></P>
<P><FONT face="Courier New">10. void println( boolean b ) ;<BR>&nbsp;&nbsp;&nbsp; void println( char c ) ;<BR>&nbsp;&nbsp;&nbsp; void println( char[] s ) ;<BR>&nbsp;&nbsp;&nbsp; void println( double d ) ;<BR>&nbsp;&nbsp;&nbsp; void println( float f ) ;<BR>&nbsp;&nbsp;&nbsp; void println( int i ) ;<BR>&nbsp;&nbsp;&nbsp; void println( long l ) ;<BR>&nbsp;&nbsp;&nbsp; void println( Object obj ) ;<BR>&nbsp;&nbsp;&nbsp; void println( String s ) ;<BR>&nbsp;&nbsp;&nbsp; 将指定类型的数据输出到Http流，并输出一个换行符。<BR>&nbsp;&nbsp;&nbsp; <BR>11. Appendable append( char c ) ;<BR>&nbsp;&nbsp;&nbsp; Appendable append( CharSequence cxq, int start, int end ) ;<BR>&nbsp;&nbsp;&nbsp; Appendable append( CharSequence cxq ) ;<BR>&nbsp;&nbsp;&nbsp; 将一个字符或者实现了CharSequence接口的对象添加到输出流的后面。</FONT></P>
<P><FONT face="Courier New">成员：<BR>int DEFAULT_BUFFER = 0&nbsp;&nbsp;&nbsp; - 缺省缓冲区大小<BR>int NO_BUFFER = -1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; - writer是否处于缓冲输出状态<BR>int UNBOUNDED_BUFFER = -2 - 是否限制缓冲区大小</FONT></P>
<P><BR><FONT face="Courier New"><STRONG>② request - javax.servlet.http.HttpServletRequest<BR></STRONG>&nbsp;&nbsp; request对象包含所有请求的信息，如请求的来源、标头、cookies和请求相关的参数值等。</FONT></P>
<P><FONT face="Courier New">方法：<BR>1. Object getAttribute( String name ) ;<BR>&nbsp;&nbsp; 返回由name指定的属性值，该属性不存在时返回null。</FONT></P>
<P><FONT face="Courier New">2. Enumeration getAttributeNames() ;<BR>&nbsp;&nbsp; 返回request对象的所有属性名称的集合。</FONT></P>
<P><FONT face="Courier New">3. String getAuthType() ;<BR>&nbsp;&nbsp; 返回用来保护servlet的认证方法的名称，未受保护时返回null。</FONT></P>
<P><FONT face="Courier New">4. String getCharacterEncoding() ;<BR>&nbsp;&nbsp; 返回请求中的字符编码方法，可以在response对象中设置。</FONT></P>
<P><FONT face="Courier New">5. int getContentLength() ;<BR>&nbsp;&nbsp; 返回请求的BODY的长度，不能确定长度时返回-1。可以在response中设置。</FONT></P>
<P><FONT face="Courier New">6. String getContentType() ;<BR>&nbsp;&nbsp; 返回在response中定义的内容类型。</FONT></P>
<P><FONT face="Courier New">7. String getContentPath() ;<BR>&nbsp;&nbsp; 返回请求的路径。</FONT></P>
<P><FONT face="Courier New">8. Cookie[] getCookies() ;<BR>&nbsp;&nbsp; 返回客户端所有的Cookie的数组。</FONT></P>
<P><FONT face="Courier New">9. Enumeration getHeaderNames() ;<BR>&nbsp;&nbsp; 返回所有HTTP头的名称的集合。</FONT></P>
<P><FONT face="Courier New">10. Enumeration getHeaders( String name ) ;<BR>&nbsp;&nbsp;&nbsp; 返回指定HTTP头的所有值的集合。</FONT></P>
<P><FONT face="Courier New">11. String getHeader( String name ) ;<BR>&nbsp;&nbsp;&nbsp; 返回指定名称的HTTP头的信息。</FONT></P>
<P><FONT face="Courier New">12. long getDateHeader( String name ) ;<BR>&nbsp;&nbsp;&nbsp; 返回指定名称的Data类型的HTTP头的信息。</FONT></P>
<P><FONT face="Courier New">13. int getIntHeader( String name ) ;<BR>&nbsp;&nbsp;&nbsp; 返回指定名称的Int类型的HTTP头的信息。</FONT></P>
<P><FONT face="Courier New">14. ServletInputStream getInputStream() ;<BR>&nbsp;&nbsp;&nbsp; 返回请求的输入流。</FONT></P>
<P><FONT face="Courier New">15. Locale getLocale() ;<BR>&nbsp;&nbsp;&nbsp; 返回当前页的Locale对象，可以在response中设定。</FONT></P>
<P><FONT face="Courier New">16. Enumeration getLocales() ;<BR>&nbsp;&nbsp;&nbsp; 返回请求中所有的Locale对象的集合。</FONT></P>
<P><FONT face="Courier New">17. String getLocalName() ;<BR>&nbsp;&nbsp;&nbsp; 获取响应请求的服务器端主机名。</FONT></P>
<P><FONT face="Courier New">18. String getLocalAddr() ;<BR>&nbsp;&nbsp;&nbsp; 获取响应请求的服务器端地址。</FONT></P>
<P><FONT face="Courier New">19. int getLocalPort() ;<BR>&nbsp;&nbsp;&nbsp; 获取响应请求的服务器端端口</FONT></P>
<P><FONT face="Courier New">20. String getMethod() ;<BR>&nbsp;&nbsp;&nbsp; 获取客户端向服务器端发送请求的方法(GET、POST)。</FONT></P>
<P><FONT face="Courier New">21. String getParameter( String name ) ;<BR>&nbsp;&nbsp;&nbsp; 获取客户端发送给服务器端的参数值。</FONT></P>
<P><FONT face="Courier New">22. Map getParameterMap() ;<BR>&nbsp;&nbsp;&nbsp; 该方法返回包含请求中所有参数的一个Map对象。</FONT></P>
<P><FONT face="Courier New">23. Enumeration getParameterNames() ;<BR>&nbsp;&nbsp;&nbsp; 返回请求中所有参数的集合。</FONT></P>
<P><FONT face="Courier New">24. String[] getParameterValues( String name ) ;<BR>&nbsp;&nbsp;&nbsp; 获得请求中指定参数的所有值。</FONT></P>
<P><FONT face="Courier New">25. String getQueryString() ;<BR>&nbsp;&nbsp;&nbsp; 返回get方法传递的参数字符串，该方法不分解出单独的参数。</FONT></P>
<P><FONT face="Courier New">26. String getPathInfo() ;<BR>&nbsp;&nbsp;&nbsp; 取出请求中处于ServletPath和QueryString之间的额外信息。</FONT></P>
<P><FONT face="Courier New">27. String getPathTranslated() ;<BR>&nbsp;&nbsp;&nbsp; 返回用getPathInfo()方法取得的路径信息的实际路径。</FONT></P>
<P><FONT face="Courier New">28. String getProtocol() ;<BR>&nbsp;&nbsp;&nbsp; 返回请求使用的协议。可以是HTTP1.1或者HTTP1.0。</FONT></P>
<P><FONT face="Courier New">29. BufferedReader getReader() ;<BR>&nbsp;&nbsp;&nbsp; 返回请求的输入流对应的Reader对象，该方法和getInputStream()方法在一个页面中只能调用一个。</FONT></P>
<P><FONT face="Courier New">30. String getRemoteAddr() ;<BR>&nbsp;&nbsp;&nbsp; 获取发出请求的客户端IP地址。</FONT></P>
<P><FONT face="Courier New">31. String getRemoteHost() ;<BR>&nbsp;&nbsp;&nbsp; 获取发出请求的客户端主机名</FONT></P>
<P><FONT face="Courier New">32. String getRemoteUser() ;<BR>&nbsp;&nbsp;&nbsp; 返回经过客户端验证的用户名，未经验证返回null。</FONT></P>
<P><FONT face="Courier New">33. int getRemotePort() ;<BR>&nbsp;&nbsp;&nbsp; 返回发出请求的客户端主机端口。</FONT></P>
<P><FONT face="Courier New">34. String getRealPath( String path ) ;<BR>&nbsp;&nbsp;&nbsp; 返回给定虚拟路径的物理路径。</FONT></P>
<P><FONT face="Courier New">35. RequestDispatcher getRequestDispatcher( String path ) ;<BR>&nbsp;&nbsp;&nbsp; 按给定的路径生成资源转向处理适配器对象。</FONT></P>
<P><FONT face="Courier New">36. String getRequestedSessionId() ;<BR>&nbsp;&nbsp;&nbsp; 返回请求的session的标识。</FONT></P>
<P><FONT face="Courier New">37. String RequestURI() ;<BR>&nbsp;&nbsp;&nbsp; 返回发出请求的客户端地址，但是不包括请求的参数字符串。</FONT></P>
<P><FONT face="Courier New">38. StringBuffer getRequestURI() ;<BR>&nbsp;&nbsp;&nbsp; 返回响应请求的服务器端地址</FONT></P>
<P><FONT face="Courier New">39. String getScheme() ;<BR>&nbsp;&nbsp;&nbsp; 获取协议名称，缺省值为HTTP协议。</FONT></P>
<P><FONT face="Courier New">40. String getServerName() ;<BR>&nbsp;&nbsp;&nbsp; 返回响应请求的服务器名称。</FONT></P>
<P><FONT face="Courier New">41. String getServletPath() ;<BR>&nbsp;&nbsp;&nbsp; 获取客户端所请求的脚本文件的文件路径。</FONT></P>
<P><FONT face="Courier New">42. int getServerPort() ;<BR>&nbsp;&nbsp;&nbsp; 获取响应请求的服务器端主机端口号。</FONT></P>
<P><FONT face="Courier New">43. void removeAttribute( String name ) ;<BR>&nbsp;&nbsp;&nbsp; 在属性列表中删除指定名称的属性。</FONT></P>
<P><FONT face="Courier New">44. void setAttribute( String name, Object value ) ;<BR>&nbsp;&nbsp;&nbsp; 在属性列表中添加/删除指定的属性。</FONT></P>
<P><FONT face="Courier New">45. void setCharacterEncoding( String name ) ;<BR>&nbsp;&nbsp;&nbsp; 设置请求的字符编码格式。</FONT></P>
<P><FONT face="Courier New">46. HttpSession getSession() ;<BR>&nbsp;&nbsp;&nbsp; HttpSession getSession( boolean create ) ;<BR>&nbsp;&nbsp;&nbsp; 获取session，如果create为true，在无session的情况下创建一个。<BR>&nbsp;&nbsp;&nbsp; <BR>47. boolean isRequestedSessionIdFromCookie() ;<BR>&nbsp;&nbsp;&nbsp; 检查请求的会话ID是否为通过Cookie传入。</FONT></P>
<P><FONT face="Courier New">48. boolean isRequestedSessionIdFromURL() ;<BR>&nbsp;&nbsp;&nbsp; 检查请求的会话ID是否为通过URL传入。</FONT></P>
<P><FONT face="Courier New">49. boolean isRequestedSessionIdValid() ;<BR>&nbsp;&nbsp;&nbsp; 检查请求的会话ID是否仍然有效。</FONT></P>
<P><FONT face="Courier New">50. boolean isSecure() ;<BR>&nbsp;&nbsp;&nbsp; 检查请求是否使用安全链接，如果HTTPS等。</FONT></P>
<P><FONT face="Courier New">51. boolean isUserInRole( String role ) ;<BR>&nbsp;&nbsp;&nbsp; 检查已经通过验证的用户是否在是role所指定的角色。</FONT></P>
<P><FONT face="Courier New">52. Principal getUserPrincipal() ;<BR>&nbsp;&nbsp;&nbsp; 返回包含用户登陆名的一个java.security.Principal对象。</FONT></P>
<P><FONT face="Courier New">成员：<BR>String BASIC_AUTH = "BASIC"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; - <BR>String CLIENT_CERT_AUTH = "CLIENT_CERT" - <BR>String DIGEST_AUTH = "DIGEST"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; - <BR>String FORM_AUTH = "FORM"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; - </FONT></P>
<P><BR><FONT face="Courier New"><STRONG>③ response - javax.servlet.http.HttpServletResponse</STRONG><BR>&nbsp;&nbsp; response对象主要将JSP容器处理后的结果传回到客户端。</FONT></P>
<P><FONT face="Courier New">方法：<BR>1. void addCookie( Cookie cookie ) ;<BR>&nbsp;&nbsp; 添加一个Cookie对象，保存客户端信息。</FONT></P>
<P><FONT face="Courier New">2. void addDateHeader( String name, long value ) ;<BR>&nbsp;&nbsp; 添加一个日期类型的HTTP头信息，覆盖同名的HTTP头信息。</FONT></P>
<P><FONT face="Courier New">3. void addHeader( String name, String value ) ;<BR>&nbsp;&nbsp; 添加一个HTTP头，覆盖同名的旧HTTP头。</FONT></P>
<P><FONT face="Courier New">4. void addIntHeader( String name, int value ) ;<BR>&nbsp;&nbsp; 添加一个整型的HTTP头，覆盖同名的旧HTTP头。</FONT></P>
<P><FONT face="Courier New">5. boolean containsHeader( String name ) ;<BR>&nbsp;&nbsp; 判断指定的HTTP头是否存在。</FONT></P>
<P><FONT face="Courier New">6. String encodeRedirectURL( String url ) ;<BR>&nbsp;&nbsp; 对sendRedirect()方法使用的URL进行编码。</FONT></P>
<P><FONT face="Courier New">7. String encodeURL( String url ) ;<BR>&nbsp;&nbsp; 将URL予以编码，回传包含session ID的URL。<BR>&nbsp;&nbsp; <BR>8. void flushBuffer() ;<BR>&nbsp;&nbsp; 强制把当前缓冲区的内容发送到客户端。</FONT></P>
<P><FONT face="Courier New">9. int getBufferSize() ;<BR>&nbsp;&nbsp; 取得以kb为单位的缓冲区大小。</FONT></P>
<P><FONT face="Courier New">10. String getCharacterEncoding() ;<BR>&nbsp;&nbsp;&nbsp; 获取响应的字符编码格式。</FONT></P>
<P><FONT face="Courier New">11. String getContentType() ;<BR>&nbsp;&nbsp;&nbsp; 获取响应的类型。</FONT></P>
<P><FONT face="Courier New">12. Locale getLocale() ;<BR>&nbsp;&nbsp;&nbsp; 获取响应的Locale对象。</FONT></P>
<P><FONT face="Courier New">13. ServletOutputStream getOutputStream() ;<BR>&nbsp;&nbsp;&nbsp; 返回客户端的输出流对象。</FONT></P>
<P><FONT face="Courier New">14. PrintWriter getWriter() ;<BR>&nbsp;&nbsp;&nbsp; 获取输出流对应的writer对象。</FONT></P>
<P><FONT face="Courier New">15. boolean isCommitted() ;<BR>&nbsp;&nbsp;&nbsp; 判断服务器端是否已经将数据输出到客户端。</FONT></P>
<P><FONT face="Courier New">16. void reset() ;<BR>&nbsp;&nbsp;&nbsp; 清空buffer中的所有内容。</FONT></P>
<P><FONT face="Courier New">17. void resetBuffer() ;<BR>&nbsp;&nbsp;&nbsp; 情况buffer中所有的内容，但是保留HTTP头和状态信息。</FONT></P>
<P><FONT face="Courier New">18. void sendError( int xc, String msg ) ;<BR>&nbsp;&nbsp;&nbsp; void sendError( int xc ) ;<BR>&nbsp;&nbsp;&nbsp; 发送错误，包括状态码和错误信息。</FONT></P>
<P><FONT face="Courier New">19. void sendRedirect( String locationg ) ;<BR>&nbsp;&nbsp;&nbsp; 把响应发送到另外一个位置进行处理。</FONT></P>
<P><FONT face="Courier New">20. void setBufferSize( int size ) ;<BR>&nbsp;&nbsp;&nbsp; 设置以kb为单位的缓冲区大小。</FONT></P>
<P><FONT face="Courier New">21. void setCharacterEncoding( String charset ) ;<BR>&nbsp;&nbsp;&nbsp; 设置响应使用的字符编码格式。</FONT></P>
<P><FONT face="Courier New">22. void setContentLength( int length ) ;<BR>&nbsp;&nbsp;&nbsp; 设置响应的BODY长度。</FONT></P>
<P><FONT face="Courier New">23. void setContentType( String type ) ;<BR>&nbsp;&nbsp;&nbsp; 设置响应的类型。</FONT></P>
<P><FONT face="Courier New">24. void setDateHeader( String name, long value ) ;<BR>&nbsp;&nbsp;&nbsp; 设置指定名称的Data类型的HTTP头的值。</FONT></P>
<P><FONT face="Courier New">25. void setHeader( String name, String value ) ;<BR>&nbsp;&nbsp;&nbsp; 设置指定名称的HTTP头的值。</FONT></P>
<P><FONT face="Courier New">26. void setIntHeader( String name, int value ) ;<BR>&nbsp;&nbsp;&nbsp; 设置指定名称的int类型的HTTP头的值。</FONT></P>
<P><FONT face="Courier New">27. void setStatus( int xc ) ;<BR>&nbsp;&nbsp;&nbsp; 设置响应状态码，新值会覆盖当前值。</FONT></P>
<P><FONT face="Courier New">成员(HTTP状态码)：<BR>int SC_CONTINUE = 100&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int SC_SWITCHING_PROTOCOLS = 101<BR>int SC_OK = 200&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int SC_NON_AUTHORITATIVE_INFORMATION = 203<BR>int SC_ACCEPTED = 202&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int SC_CREATED = 201<BR>int SC_NO_CONTENT = 204&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int SC_RESET_CONTENT = 205<BR>int SC_PARTIAL_CONTENT = 206&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int SC_MULTIPLE_CHOICES = 300<BR>int SC_MOVED_PERMANENTLY = 301&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int SC_MOVED_TEMPORARILY = 302<BR>int SC_FOUND = 302&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int SC_SEE_OTHER = 303<BR>int SC_NOT_MODIFIED = 304&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int SC_USE_PROXY = 305<BR>int SC_TEMPORARY_REDIRECT = 307&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int SC_BAD_REQUEST = 400<BR>int SC_UNAUTHORIZED = 401&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int SC_PAYMENT_REQUIRED = 402<BR>int SC_FORBIDDEN = 403&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int SC_NOT_FOUND = 404<BR>int SC_METHOD_NOT_ALLOWED = 405&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int SC_NOT_ACCEPTABLE = 406<BR>int SC_PROXY_AUTHENTICATION_REQUIRED = 407 int SC_REQUEST_TIMEOUT = 408<BR>int SC_CONFLICT = 409&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int SC_GONE = 410<BR>int SC_LENGTH_REQUIRED = 411&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int SC_PRECONDITION_FAILED = 412<BR>int SC_REQUEST_ENTITY_TOO_LARGE = 413&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int SC_REQUEST_URI_TOO_LONG = 414<BR>int SC_UNSUPPORTED_MEDIA_TYPE = 415&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int SC_REQUESTED_RANGE_NOT_SATISFIABLE = 416<BR>int SC_EXPECTATION_FAILED = 417&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int SC_INTERNAL_SERVER_ERROR = 500<BR>int SC_NOT_IMPLEMENTED = 501&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int SC_BAD_GATEWAY = 502<BR>int SC_SERVICE_UNAVAILABLE = 503&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int SC_GATEWAY_TIMEOUT = 504<BR>int SC_HTTP_VERSION_NOT_SUPPORTED = 505</FONT></P>
<P><BR><FONT face="Courier New"><STRONG>④ session - javax.servlet.http.HttpSession</STRONG><BR>&nbsp;&nbsp; session对象表示目前个别用户的会话状态，用来识别每个用户。</FONT></P>
<P><FONT face="Courier New">方法：<BR>1. Object getAttribute( String name ) ;<BR>&nbsp;&nbsp; 获取与指定名字相关联的session属性值。</FONT></P>
<P><FONT face="Courier New">2. Enumeration getAttributeNames() ;<BR>&nbsp;&nbsp; 取得session内所有属性的集合。</FONT></P>
<P><FONT face="Courier New">3. long getCreationTime() ;<BR>&nbsp;&nbsp; 返回session的创建时间，最小单位千分之一秒。</FONT></P>
<P><FONT face="Courier New">4. String getId() ;<BR>&nbsp;&nbsp; 取得session标识。</FONT></P>
<P><FONT face="Courier New">5. long getLastAccessedTime() ;<BR>&nbsp;&nbsp; 返回与当前session相关的客户端最后一次访问的时间，由1970-01-01算起，单位毫秒。</FONT></P>
<P><FONT face="Courier New">6. int getMaxInactiveInterval( int interval ) ;<BR>&nbsp;&nbsp; 返回总时间，以秒为单位，表示session的有效时间(session不活动时间)。-1为永不过期。</FONT></P>
<P><FONT face="Courier New">7. ServletContext getServletContext() ;<BR>&nbsp;&nbsp; 返回一个该JSP页面对应的ServletContext对象实例。</FONT></P>
<P><FONT face="Courier New">8. HttpSessionContext getSessionContext() ;<BR>&nbsp;&nbsp; </FONT></P>
<P><FONT face="Courier New">9. Object getValue( String name ) ;<BR>&nbsp;&nbsp; 取得指定名称的session变量值，不推荐使用。</FONT></P>
<P><FONT face="Courier New">10. String[] getValueNames() ;<BR>&nbsp;&nbsp;&nbsp; 取得所有session变量的名称的集合，不推荐使用。</FONT></P>
<P><FONT face="Courier New">11. void invalidate() ;<BR>&nbsp;&nbsp;&nbsp; 销毁这个session对象。</FONT></P>
<P><FONT face="Courier New">12. boolean isNew() ;<BR>&nbsp;&nbsp;&nbsp; 判断一个session是否由服务器产生，但是客户端并没有使用。</FONT></P>
<P><FONT face="Courier New">13. void pubValue( String name, Object value ) ;<BR>&nbsp;&nbsp;&nbsp; 添加一个session变量，不推荐使用。</FONT></P>
<P><FONT face="Courier New">14. void removeValue( String name ) ;<BR>&nbsp;&nbsp;&nbsp; 移除一个session变量的值，不推荐使用。</FONT></P>
<P><FONT face="Courier New">15. void setAttribute( String name, String value ) ;<BR>&nbsp;&nbsp;&nbsp; 设置指定名称的session属性值。</FONT></P>
<P><FONT face="Courier New">16. void setMaxInactiveInterval( int interval ) ;<BR>&nbsp;&nbsp;&nbsp; 设置session的有效期。</FONT></P>
<P><FONT face="Courier New">17. void removeAttribute( String name ) ;<BR>&nbsp;&nbsp;&nbsp; 移除指定名称的session属性。</FONT></P>
<P><BR><FONT face="Courier New"><STRONG>⑤ pageContext - javax.servlet.jsp.PageContext</STRONG><BR>&nbsp;&nbsp; pageContext对象存储本JSP页面相关信息，如属性、内建对象等。</FONT></P>
<P><FONT face="Courier New">方法：<BR>1. void setAttribute( String name, Object value, int scope ) ;<BR>&nbsp;&nbsp; void setAttribute( String name, Object value ) ;<BR>&nbsp;&nbsp; 在指定的共享范围内设置属性。</FONT></P>
<P><FONT face="Courier New">2. Object getAttribute( String name, int scope ) ;<BR>&nbsp;&nbsp; Object getAttribute( String name ) ;<BR>&nbsp;&nbsp; 取得指定共享范围内以name为名字的属性值。</FONT></P>
<P><FONT face="Courier New">3. Object findAttribute( String name ) ;<BR>&nbsp;&nbsp; 按页面、请求、会话和应用程序共享范围搜索已命名的属性。</FONT></P>
<P><FONT face="Courier New">4. void removeAttribute( String name, int scope ) ;<BR>&nbsp;&nbsp; void removeAttribute( String name ) ;<BR>&nbsp;&nbsp; 移除指定名称和共享范围的属性。</FONT></P>
<P><FONT face="Courier New">5. void forward( String url ) ;<BR>&nbsp;&nbsp; 将页面导航到指定的URL。</FONT></P>
<P><FONT face="Courier New">6. Enumeration getAttributeNamesScope( int scope ) ;<BR>&nbsp;&nbsp; 取得指定共享范围内的所有属性名称的集合。</FONT></P>
<P><FONT face="Courier New">7. int getAttributeScope( String name ) ;<BR>&nbsp;&nbsp; 取得指定属性的共享范围。</FONT></P>
<P><FONT face="Courier New">8. ErrorData getErrorDate() ;<BR>&nbsp;&nbsp; 取得页面的errorData对象。</FONT></P>
<P><FONT face="Courier New">9. Exception getException() ;<BR>&nbsp;&nbsp; 取得页面的exception对象。</FONT></P>
<P><FONT face="Courier New">10. ExpressionEvaluator getExpressionEvaluator() ;<BR>&nbsp;&nbsp;&nbsp; 取得页面的expressionEvaluator对象。</FONT></P>
<P><FONT face="Courier New">11. JspWriter getOut() ;<BR>&nbsp;&nbsp;&nbsp; 取得页面的out对象。</FONT></P>
<P><FONT face="Courier New">12. Object getPage() ;<BR>&nbsp;&nbsp;&nbsp; 取得页面的page对象。</FONT></P>
<P><FONT face="Courier New">13. ServletRequest getRequest() ;<BR>&nbsp;&nbsp;&nbsp; 取得页面的request对象。</FONT></P>
<P><FONT face="Courier New">14. ServletResponse getResponse() ;<BR>&nbsp;&nbsp;&nbsp; 取得页面的response对象。</FONT></P>
<P><FONT face="Courier New">15. ServletConfig getConfig() ;<BR>&nbsp;&nbsp;&nbsp; 取得页面的config对象。</FONT></P>
<P><FONT face="Courier New">16. ServletContext getServletContext() ;<BR>&nbsp;&nbsp;&nbsp; 取得页面的servletContext对象。</FONT></P>
<P><FONT face="Courier New">17. HttpSession getSession() ;<BR>&nbsp;&nbsp;&nbsp; 取得页面的session对象。</FONT></P>
<P><FONT face="Courier New">18. VariableResolver getVariableResolver() ;<BR>&nbsp;&nbsp;&nbsp; 取得页面的variableResolver对象。</FONT></P>
<P><FONT face="Courier New">19. void include( String url, boolean flush ) ;<BR>&nbsp;&nbsp;&nbsp; void include( String url ) ;<BR>&nbsp;&nbsp;&nbsp; 包含其他的资源，并指定是否自动刷新。</FONT></P>
<P><FONT face="Courier New">20. void release() ;<BR>&nbsp;&nbsp;&nbsp; 重置pageContext内部状态，释放所有内部引用。</FONT></P>
<P><FONT face="Courier New">21. void initialize( Servlet servlet, ServletRequest request, ServletResponse response,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String errorPageURL, boolean needSession, int bufferSize, boolean autoFlush ) ;<BR>&nbsp;&nbsp;&nbsp; 初始化未经初始化的pageContext对象。</FONT></P>
<P><FONT face="Courier New">22. BodyContext pushBody() ;<BR>&nbsp;&nbsp;&nbsp; BodyContext pushBody( Writer writer ) ;<BR>&nbsp;&nbsp;&nbsp; 保存当前的out对象，并更新pageContext中page范围内的out对象。</FONT></P>
<P><FONT face="Courier New">23. JspWrite popBody() ;<BR>&nbsp;&nbsp;&nbsp; 取出由pushBody()方法保存的out对象。</FONT></P>
<P><FONT face="Courier New">24. void handlePageException( Exception e ) ;<BR>&nbsp;&nbsp;&nbsp; void handlePageException( Thrwoable t ) ;<BR>&nbsp;&nbsp;&nbsp; </FONT></P>
<P><FONT face="Courier New">成员：<BR>int PAGE_SCOPE = 1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; - 页面共享范围<BR>int REQUEST_SCOPE = 2&nbsp;&nbsp;&nbsp;&nbsp; - 请求共享范围<BR>int SESSION_SCOPE = 3&nbsp;&nbsp;&nbsp;&nbsp; - 会话共享范围<BR>int APPLICATION_SCOPE = 4 - 应用程序共享范围<BR>String PAGE = "javax.servlet.jsp.jspPage"<BR>String PAGECONTEXT = "javax.servlet.jsp.jspPageContext"<BR>String REQUEST = "javax.servlet.jsp.jspRequest"<BR>String RESPONSE = "javax.servlet.jsp.jspResponse"<BR>String CONFIG = "javax.servlet.jsp.jspConfig"<BR>String SESSION = "javax.servlet.jsp.jspSession"<BR>String OUT = "javax.servlet.jsp.jspOut"<BR>String APPLICATION = "javax.servlet.jsp.jspApplication"<BR>String EXCEPTION = "javax.servlet.jsp.jspException"</FONT></P>
<P><BR><FONT face="Courier New"><STRONG>⑥ application - javax.servlet.ServletContext</STRONG><BR>&nbsp;&nbsp; application主要功用在于取得或更改Servlet的设定。</FONT></P>
<P><FONT face="Courier New">方法：<BR>1. Object getAttribute( String name ) ;<BR>&nbsp;&nbsp; 返回由name指定的application属性。</FONT></P>
<P><FONT face="Courier New">2. Enumeration getAttributes() ;<BR>&nbsp;&nbsp; 返回所有的application属性。</FONT></P>
<P><FONT face="Courier New">3. ServletContext getContext( String uripath ) ;<BR>&nbsp;&nbsp; 取得当前应用的ServletContext对象。</FONT></P>
<P><FONT face="Courier New">4. String getInitParameter( String name ) ;<BR>&nbsp;&nbsp; 返回由name指定的application属性的初始值。</FONT></P>
<P><FONT face="Courier New">5. Enumeration getInitParameters() ;<BR>&nbsp;&nbsp; 返回所有的application属性的初始值的集合。</FONT></P>
<P><FONT face="Courier New">6. int getMajorVersion() ;<BR>&nbsp;&nbsp; 返回servlet容器支持的Servlet API的版本号。</FONT></P>
<P><FONT face="Courier New">7. String getMimeType( String file ) ;<BR>&nbsp;&nbsp; 返回指定文件的类型，未知类型返回null。一般为"text/html"和"image/gif"。</FONT></P>
<P><FONT face="Courier New">8. int getMinorVersion() ;<BR>&nbsp;&nbsp; 返回servlet容器支持的Servlet API的副版本号。</FONT></P>
<P><FONT face="Courier New">9. String getRealPath( String path ) ;<BR>&nbsp;&nbsp; 返回给定虚拟路径所对应物理路径。</FONT></P>
<P><FONT face="Courier New">10. RequestDispatcher getNamedDispatcher( String name ) ;<BR>&nbsp;&nbsp;&nbsp; 为指定名字的Servlet对象返回一个RequestDispatcher对象的实例。</FONT></P>
<P><FONT face="Courier New">11. RequestDispatcher getRequestDispatcher( String path ) ;<BR>&nbsp;&nbsp;&nbsp; 返回一个RequestDispatcher对象的实例。</FONT></P>
<P><FONT face="Courier New">12. URL getResource( String path ) ;<BR>&nbsp;&nbsp;&nbsp; 返回指定的资源路径对应的一个URL对象实例，参数要以"/"开头。</FONT></P>
<P><FONT face="Courier New">13. InputStream getResourceAsStream( String path ) ;<BR>&nbsp;&nbsp;&nbsp; 返回一个由path指定位置的资源的InputStream对象实例。</FONT></P>
<P><FONT face="Courier New">14. Set getResourcePaths( String path ) ;<BR>&nbsp;&nbsp;&nbsp; 返回存储在web-app中所有资源路径的集合。</FONT></P>
<P><FONT face="Courier New">15. String getServerInfo() ;<BR>&nbsp;&nbsp;&nbsp; 取得应用服务器版本信息。</FONT></P>
<P><FONT face="Courier New">16. Servlet getServlet( String name ) ;<BR>&nbsp;&nbsp;&nbsp; 在ServletContext中检索指定名称的servlet。</FONT></P>
<P><FONT face="Courier New">17. Enumeration getServlets() ;<BR>&nbsp;&nbsp;&nbsp; 返回ServletContext中所有servlet的集合。</FONT></P>
<P><FONT face="Courier New">18. String getServletContextName() ;<BR>&nbsp;&nbsp;&nbsp; 返回本web应用的名称。</FONT></P>
<P><FONT face="Courier New">19. Enumeration getServletContextNames() ;<BR>&nbsp;&nbsp;&nbsp; 返回ServletContext中所有servlet的名称集合。</FONT></P>
<P><FONT face="Courier New">20. void log( Exception ex, String msg ) ;<BR>&nbsp;&nbsp;&nbsp; void log( String msg, Throwable t ) ;<BR>&nbsp;&nbsp;&nbsp; void log( String msg ) ;<BR>&nbsp;&nbsp;&nbsp; 把指定的信息写入servlet log文件。</FONT></P>
<P><FONT face="Courier New">21. void removeAttribute( String name ) ;<BR>&nbsp;&nbsp;&nbsp; 移除指定名称的application属性。</FONT></P>
<P><FONT face="Courier New">22. void setAttribute( String name, Object value ) ;<BR>&nbsp;&nbsp;&nbsp; 设定指定的application属性的值。</FONT></P>
<P><BR><FONT face="Courier New"><STRONG>⑦ config - javax.servlet.ServletConfig<BR></STRONG>&nbsp;&nbsp; config对象用来存放Servlet初始的数据结构。</FONT></P>
<P><FONT face="Courier New">方法：<BR>1. String getInitParameter( String name ) ;<BR>&nbsp;&nbsp; 返回名称为name的促使参数的值。</FONT></P>
<P><FONT face="Courier New">2. Enumeration getInitParameters() ;<BR>&nbsp;&nbsp; 返回这个JSP所有的促使参数的名称集合。</FONT></P>
<P><FONT face="Courier New">3. ServletContext getContext() ;<BR>&nbsp;&nbsp; 返回执行者的servlet上下文。</FONT></P>
<P><FONT face="Courier New">4. String getServletName() ;<BR>&nbsp;&nbsp; 返回servlet的名称。</FONT></P>
<P><BR><FONT face="Courier New"><STRONG>⑧ exception - java.lang.Throwable</STRONG><BR>&nbsp;&nbsp; 错误对象，只有在JSP页面的page指令中指定isErrorPage="true"后，才可以在本页面使用exception对象。</FONT></P>
<P><FONT face="Courier New">方法：<BR>1. Throwable fillInStackTrace() ;<BR>&nbsp;&nbsp; 将当前stack信息记录到exception对象中。</FONT></P>
<P><FONT face="Courier New">2. String getLocalizedMessage() ;<BR>&nbsp;&nbsp; 取得本地语系的错误提示信息。</FONT></P>
<P><FONT face="Courier New">3. String getMessage()<BR>&nbsp;&nbsp; 取得错误提示信息。</FONT></P>
<P><FONT face="Courier New">4. StackTrackElement[] getStackTrace() ;<BR>&nbsp;&nbsp; 返回对象中记录的call stack track信息。</FONT></P>
<P><FONT face="Courier New">5. Throwable initCause( Throwable cause ) ;<BR>&nbsp;&nbsp; 将另外一个异常对象嵌套进当前异常对象中。<BR>&nbsp;&nbsp; <BR>6. Throwable getCause() ;<BR>&nbsp;&nbsp; 取出嵌套在当前异常对象中的异常。</FONT></P>
<P><FONT face="Courier New">7. void printStackTrace() ;<BR>&nbsp;&nbsp; void printStackTrace( printStream s ) ;<BR>&nbsp;&nbsp; void printStackTrace( printWriter s ) ;<BR>&nbsp;&nbsp; 打印出Throwable及其call stack trace信息。</FONT></P>
<P><FONT face="Courier New">8. void setStackTrace( StackTraceElement[] stackTrace )<BR>&nbsp;&nbsp; 设置对象的call stack trace信息。</FONT></P>
<P><BR><FONT face="Courier New"><STRONG>⑨ page - javax.servlet.jsp.HttpJspPage</STRONG><BR>&nbsp;&nbsp; page对象代表JSP对象本身，或者说代表编译后的servlet对象，<BR>&nbsp;&nbsp; 可以用( (javax.servlet.jsp.HttpJspPage)page )来取用它的方法和属性。</FONT></P><img src ="http://www.blogjava.net/hmily1211/aggbug/30332.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/hmily1211/" target="_blank">活在JAVA岛的日子</a> 2006-02-12 11:35 <a href="http://www.blogjava.net/hmily1211/archive/2006/02/12/30332.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Servlets/JSP开发技术问答(转贴)</title><link>http://www.blogjava.net/hmily1211/archive/2006/02/12/30331.html</link><dc:creator>活在JAVA岛的日子</dc:creator><author>活在JAVA岛的日子</author><pubDate>Sun, 12 Feb 2006 03:29:00 GMT</pubDate><guid>http://www.blogjava.net/hmily1211/archive/2006/02/12/30331.html</guid><wfw:comment>http://www.blogjava.net/hmily1211/comments/30331.html</wfw:comment><comments>http://www.blogjava.net/hmily1211/archive/2006/02/12/30331.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.blogjava.net/hmily1211/comments/commentRss/30331.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/hmily1211/services/trackbacks/30331.html</trackback:ping><description><![CDATA[<LI><STRONG>为什么GenericServlet在init(ServletConfig config)基础上增加了一个init()方法？ </STRONG><BR>
<BLOCKQUOTE>
<P>init()方法被GenericServlet.init(ServletConfig config)方法调用。<BR>init()方法方便了开发人员定制Servlet的初始化，而无须去维护ServletConfig对象的存储工作。<BR>重写GenericServlet.init(ServletConfig config)必须要显示的调用super.init(config)方法。 </P></BLOCKQUOTE>
<LI><STRONG>ServletContext.getContect(java.lang.String uripath)的作用是什么？ </STRONG><BR>
<BLOCKQUOTE>
<P>返回同一Server中指定的path对应的ServletContext对象,通过该对象可以实现与Server中的其他Context打交道。<BR>uripath必须是以"/"开始（该路径的含义是相对于整个Servlet文档的根路径，而不是当前ServletContext的根路径）。 </P></BLOCKQUOTE>
<LI><STRONG>Servlet生命周期是什么？ </STRONG><BR>
<BLOCKQUOTE>
<P>一般的Servlet(GenericServlet,即与协议无关的Servlet)的生命周期：init() --&gt; GenericServlet.service(ServletRequest req, ServletResponse res) --&gt; destroy. <BR>HttpServlet的生命周期: init() --&gt; GenericServlet.service(ServletRequest req, ServletResponse res)---&gt; service(HttpServletRequest req, HttpServletResponse resp) --&gt; doXXXX()--&gt;destroy. </P></BLOCKQUOTE>
<LI><STRONG>有没有必要重写GenericServlet.service()方法？ </STRONG><BR>
<BLOCKQUOTE>
<P>对于HttpServlet来说没有必要。只需要重写它的doXXXX()方法就可以了。HttpServlet中service()方法会自动的根据用户请求类型把请求转发给相应的doXXXX()方法（例如doGet()方法）。 </P></BLOCKQUOTE>
<LI><STRONG>ServletRequest.getReader()和ServletRequest.getInputStream()如何使用？ </STRONG><BR>
<BLOCKQUOTE>
<P>注意两个方法不能同时使用。 </P></BLOCKQUOTE>
<LI><STRONG>ServletRequest.getRealPath(String path)方法已经不推荐使用。 </STRONG><BR>
<BLOCKQUOTE>
<P>请使用ServletContext.getRealPath(String path)方法。 </P></BLOCKQUOTE>
<LI><STRONG>ServletResponse缺省的字符集(charset)是什么? </STRONG><BR>
<BLOCKQUOTE>
<P>ServletResponse缺省的字符集(charset)是ISO-8859-1,可以通过setContentType(java.lang.String)方法改变新的字符集。<BR>例如：setContentType("text/html; charset=Shift_JIS"). <BR>关于字符集信息，可以浏览<A href="http://info.internet.isi.edu/in-notes/rfc/files/rfc2045.txt" target=_blank>RFC 2045</A> </P></BLOCKQUOTE>
<LI><STRONG>HttpServletRequest.getRequestURI()和HttpServletRequest.getRequestURL()区别是什么? </STRONG><BR>
<BLOCKQUOTE>
<P>request.getRequestURI() 返回值类似：/xuejava/requestdemo.jsp <BR>request.getRequestURL() 返回值类似：http://localhost:8080/xuejava/requestdemo.jsp<BR></P></BLOCKQUOTE>
<LI><STRONG>HttpServletRequest.encodeURL()和HttpServletRequest.encodeRedirectURL(()区别是什么?为什么要有两个不同的方法呢？ </STRONG><BR>
<BLOCKQUOTE>
<P>当用URL-rewriting方式来管理Session的时候，需要用到以上的两个方法。<BR>两个方法的不同点是：两个方法确定是否需要包含session ID的逻辑不同。<BR>在调用HttpServletResponse.sendRedirect前，应该先调用encodeRedirectURL()方法，否则可能会丢失Sesssion信息。 ... </P></BLOCKQUOTE>
<LI><STRONG>如何使你的Servlet或者JSP实现Single Thread Model? </STRONG><BR>
<BLOCKQUOTE>
<P>对于Servlet实现javax.single.SingleThreadModel接口。<BR>对于JSP,在Page Directive中写如下的语句<!--page isThreadSafe="false"--> </P></BLOCKQUOTE>
<LI><STRONG>JSP Tag 和 JSP XML-based Tag</STRONG><BR>
<BLOCKQUOTE>
<P>... </P></BLOCKQUOTE>
<LI><STRONG>如何把某一个JSP Page定义成为Error Page?为什么要这样做？</STRONG><BR>
<BLOCKQUOTE>
<P>实现方法： <!--page isErrorPage="true"--><BR>为什么？ 因为需要获取Exception 对象（缺省情况下，在JSP Page中是不能直接使用“隐含对象” exception的）。 </P></BLOCKQUOTE>
<LI><STRONG>JSP Page的执行顺序是如何的？</STRONG><BR>
<BLOCKQUOTE>
<P>JSP Page的执行顺序如下： </P>
<LI>JSP Page Translation. JSP Page --&gt; Servlet source code. 
<LI>JSP Page Compilation. Servlet source code --&gt; Servlet class. 
<LI>Load Class(First time or the server restarted) 
<LI>Create instance(可能会很多次，如果JSP Page中声明了<!--page isThreadSafe="false"-->) 
<LI>Call jspInit method（一般的JSP Page都没有重写这个方法，重写需要在声明语句段中）。 
<LI>Call _jspService method（类似与一般HttpServlet的doGet和doPost方法，但是可以同时用来处理Post和Getq请求）。 
<LI>Call jspDestroy method（Server在卸载Servet的时候，例如当Servlet很久没有使用的情况）。 
<P></P></LI></BLOCKQUOTE>
<LI><STRONG>JSP Page中有哪些隐含对象(Implicity Object)?各自的类型和作用是什么？</STRONG><BR>
<BLOCKQUOTE>
<P>
<LI>request -- 
<LI>reponse -- 
<LI>session -- 
<LI>application -- 
<LI>out -- 
<LI>page -- 
<LI>pagecontext -- 
<LI>exception -- 只有在当前JSP Page为Error Page的时候才有效。 
<LI>config -- 
<P></P></LI></BLOCKQUOTE>
<LI><STRONG><INCLUDE %="" page="/foo/foo.jsp">和 &lt;@ include file="/foo/foo.jsp" %&gt;的区别是什么？</INCLUDE></STRONG><BR>
<BLOCKQUOTE>
<P><INCLUDE <br="" time.="" request="" --="" ...="" />&lt;@ include ... -- Page translation time. <BR></P></BLOCKQUOTE>
<LI><STRONG>Servlets/JSP Container(Engine)有几种运行方式？</STRONG><BR>
<BLOCKQUOTE>
<P>
<LI>Standalone 
<BLOCKQUOTE>Tomcat standalone mode </BLOCKQUOTE>
<LI>In-process 
<BLOCKQUOTE>Tomcat running inside Apache Web Server. </BLOCKQUOTE>
<LI>Out-of-process 
<BLOCKQUOTE>Apache + mod_jk + Tomcat </BLOCKQUOTE>
<P></P></LI></BLOCKQUOTE>
<LI><STRONG>Servlet,Servlet开发人员，Servlet API, Servlet Container的关系是什么？</STRONG><BR>
<BLOCKQUOTE>Servlet,Servlet开发人员 ---&gt;Servlet API --&gt; Servlet Container </BLOCKQUOTE>
<LI><STRONG>The parts of an HTTP message</STRONG><PRE> 
    Message part Description
    The initial line： Specifies the purpose of the request or response message
                       例子：GET /reports/sales/index.html HTTP/1.0 
    The header section:Specifies the meta-information, such as size, type, and encoding, 
                       about the content of the message
    A blank line: 
    An optional message body: The main content of the request or response message
</PRE>下面是一个Response的例子： 
<BLOCKQUOTE><PRE> 
    <HR align=left width=400>
    HTTP/1.0 200 OK
    Date: Tue, 01 Dec 2001 23:59:59 GMT
    Content-Type: text/html
    Content-Length: 52
    
    <HTML />
    
    <H1>Hello, John!</H1>
    
    </PRE>
<HR align=left width=400>
</BLOCKQUOTE>
<LI><STRONG>HTTP规范中定义了哪些方法？各自有什么用途？</STRONG><BR>
<BLOCKQUOTE>
<LI>GET 
<LI>HEAD 
<LI>POST <BR>从 Http 1.1规范开始，增加了以下的方法:<BR>
<LI>PUT 
<LI>OPTIONS 
<LI>TRACE 
<LI>DELETE 
<LI>CONNECT </LI></BLOCKQUOTE>
<LI><STRONG>ServetRequest中为什么要定义:getContentType(),getContentLength()方法。</STRONG> 
<BLOCKQUOTE>根据HTTP协议规范，Request 和 Response一样也有这些必不可少的内容！<BR>所以需要首先了解 HTTP Message的概念和其内容的格式，这些东西对于Request和Reponse是一样的。<BR>对于GET方式发送的请求，其内容类型为：null<BR>对于POST方式发送的请求，其内容类型为：application/x-www-form-urlencoded<BR><BR>
<BLOCKQUOTE>POST方式发送请求的内容类似于：username=xuejava. </BLOCKQUOTE></BLOCKQUOTE>
<LI><STRONG>RequestDispatcher.forward()和HttpServletResponse.sendRedirect()的区别是什么？</STRONG> 
<BLOCKQUOTE>RequestDispatcher.forward()是在服务器端运行；HttpServletResponse.sendRedirect()是通过向客户浏览器发送命令来完成。<BR>所以RequestDispatcher.forward()对于浏览器来说是“透明的”；而HttpServletResponse.sendRedirect()则不是。<BR>另外，还要注意RequestDispatcher.forward()在调用的时候Response不能已经Commit了（Response.isCommitted()）。 </BLOCKQUOTE>
<LI><STRONG>ServletContext.getRequestDispatcher(String url)和ServletRequest.getRequestDispatcher(String url)的区别是什么？为什么？</STRONG> 
<BLOCKQUOTE>ServletContext.getRequestDispatcher(String url)中的url只能使用绝对路径；而ServletRequest.getRequestDispatcher(String url)中的url可以使用相对路径。<BR>因为ServletRequest具有相对路径的概念；而ServletContext对象无次概念。<BR></BLOCKQUOTE>
<LI><STRONG>如何把请求转移到另外一个Web App中的某个地址？</STRONG> 
<BLOCKQUOTE>ServletContext.getRequestDispatcher(String url)和ServletRequest.getRequestDispatcher(String url)只能把请求转移到同一个Web App中的地址。<BR>如果需要把请求转移到另外一个Web App中的某个地址，可以按下面的做法：<BR>1. 获得另外一个Web App的ServletConext对象(currentServletContext.getContext(uripath)).<BR>2. 调用ServletContext.getRequestDispatcher(String url)方法。 </BLOCKQUOTE></LI><img src ="http://www.blogjava.net/hmily1211/aggbug/30331.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/hmily1211/" target="_blank">活在JAVA岛的日子</a> 2006-02-12 11:29 <a href="http://www.blogjava.net/hmily1211/archive/2006/02/12/30331.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>全面理解String 转贴</title><link>http://www.blogjava.net/hmily1211/archive/2005/12/06/22793.html</link><dc:creator>活在JAVA岛的日子</dc:creator><author>活在JAVA岛的日子</author><pubDate>Tue, 06 Dec 2005 14:28:00 GMT</pubDate><guid>http://www.blogjava.net/hmily1211/archive/2005/12/06/22793.html</guid><wfw:comment>http://www.blogjava.net/hmily1211/comments/22793.html</wfw:comment><comments>http://www.blogjava.net/hmily1211/archive/2005/12/06/22793.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/hmily1211/comments/commentRss/22793.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/hmily1211/services/trackbacks/22793.html</trackback:ping><description><![CDATA[<H2 class=diaryTitle>全面理解String(JAVA复习)- -</H2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
<P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 18pt; TEXT-INDENT: -18pt; tab-stops: list 18.0pt; mso-list: l0 level1 lfo1"><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 'Times New Roman'"><SPAN style="mso-list: Ignore">1.<SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">首先</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">String</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">不属于</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">8</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">种基本数据类型，</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">String</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">是一个对象。</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体"> 
<P></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 18pt"><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">因为对象的默认值是</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">null</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">，所以</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">String</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">的默认值也是</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">null</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">；</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体"> 
<P></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 18pt"><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">但它又是一种特殊的对象，有其它对象没有的一些特性。</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体"> 
<P></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 18pt; TEXT-INDENT: -18pt; tab-stops: list 18.0pt; mso-list: l0 level1 lfo1"><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 'Times New Roman'"><SPAN style="mso-list: Ignore">2.<SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">new String()</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">和</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">new String(“”)</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">都是申明一个新的空字符串，是空串不是</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">null</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">；</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体"> 
<P></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 18pt; TEXT-INDENT: -18pt; tab-stops: list 18.0pt; mso-list: l0 level1 lfo1"><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 'Times New Roman'"><SPAN style="mso-list: Ignore">3.<SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">String str=”kvill”</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">；</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体"> 
<P></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 18pt"><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">String str=new String (“kvill”);</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">的区别：</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体"> 
<P></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 18pt"><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">在这里，我们不谈堆，也不谈栈，只先简单引入常量池这个简单的概念。</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体"> 
<P></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 18pt"><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">常量池</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">(constant pool)</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">指的是在编译期被确定，并被保存在已编译的</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">.class</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">文件中的一些数据。它包括了关于类、方法、接口等中的常量，也包括字符串常量。</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体"> 
<P></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 18pt"><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">看例</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">1</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">：</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体"> 
<P></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 18pt"><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体"><SPAN style="mso-tab-count: 2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>String s0=”kvill”; 
<P></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 18pt"><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体"><SPAN style="mso-tab-count: 2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>String s1=”kvill”; 
<P></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 18pt"><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体"><SPAN style="mso-tab-count: 2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>String s2=”kv” + “ill”; 
<P></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 18pt"><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体"><SPAN style="mso-tab-count: 2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>System.out.println( s0==s1 ); 
<P></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 39pt; TEXT-INDENT: 3pt"><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">System.out.println( s0==s2 ); 
<P></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 18pt"><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体"><SPAN style="mso-tab-count: 2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">结果为：</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体"> 
<P></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 39pt; TEXT-INDENT: 3pt"><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">true 
<P></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 39pt; TEXT-INDENT: 3pt"><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">true 
<P></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">首先，我们要知道</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">Java</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">会确保一个字符串常量只有一个拷贝。</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体"> 
<P></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 18pt"><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">因为例子中的</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">s0</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">和</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">s1</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">中的</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">”kvill”</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">都是字符串常量，它们在编译期就被确定了，所以</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">s0==s1</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">为</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">true</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">；而</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">”kv”</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">和</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">”ill”</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">也都是字符串常量，当一个字符串由多个字符串常量连接而成时，它自己肯定也是字符串常量，所以</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">s2</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">也同样在编译期就被解析为一个字符串常量，所以</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">s2</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">也是常量池中</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">”kvill”</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">的一个引用。</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体"> 
<P></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 18pt"><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">所以我们得出</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">s0==s1==s2; 
<P></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 18pt"><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">用</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">new String() </SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">创建的字符串不是常量，不能在编译期就确定，所以</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">new String() </SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">创建的字符串不放入常量池中，它们有自己的地址空间。</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体"> 
<P></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 18pt"><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">看例</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">2</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">：</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体"> 
<P></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 18pt"><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体"><SPAN style="mso-tab-count: 2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>String s0=”kvill”; 
<P></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 18pt"><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体"><SPAN style="mso-tab-count: 2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>String s1=new String(”kvill”); 
<P></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 18pt"><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体"><SPAN style="mso-tab-count: 2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>String s2=”kv” + new String(“ill”); 
<P></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 18pt"><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体"><SPAN style="mso-tab-count: 2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>System.out.println( s0==s1 ); 
<P></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 39pt; TEXT-INDENT: 3pt"><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">System.out.println( s0==s2 ); 
<P></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 39pt; TEXT-INDENT: 3pt"><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">System.out.println( s1==s2 ); 
<P></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 18pt"><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体"><SPAN style="mso-tab-count: 2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">结果为：</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体"> 
<P></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 39pt; TEXT-INDENT: 3pt"><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">false 
<P></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 39pt; TEXT-INDENT: 3pt"><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">false 
<P></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 39pt; TEXT-INDENT: 3pt"><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">false 
<P></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">例</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">2</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">中</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">s0</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">还是常量池中</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">”kvill”</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">的应用，</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">s1</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">因为无法在编译期确定，所以是运行时创建的新对象</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">”kvill”</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">的引用，</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">s2</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">因为有后半部分</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">new String(“ill”)</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">所以也无法在编译期确定，所以也是一个新创建对象</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">”kvill”</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">的应用</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">;</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">明白了这些也就知道为何得出此结果了。</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体"> 
<P></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 18pt"><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">
<P>&nbsp;</P></SPAN>
<P></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 18pt; TEXT-INDENT: -18pt; tab-stops: list 18.0pt; mso-list: l0 level1 lfo1"><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 'Times New Roman'"><SPAN style="mso-list: Ignore">4.<SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">String.intern()</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">：</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体"> 
<P></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 18pt"><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">再补充介绍一点：存在于</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">.class</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">文件中的常量池，在运行期被</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">JVM</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">装载，并且可以扩充。</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">String</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">的</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">intern()</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">方法就是扩充常量池的一个方法；当一个</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">String</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">实例</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">str</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">调用</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">intern()</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">方法时，</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">Java</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">查找常量池中是否有相同</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">Unicode</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">的字符串常量，如果有，则返回其的引用，如果没有，则在常量池中增加一个</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">Unicode</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">等于</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">str</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">的字符串并返回它的引用；看例</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">3</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">就清楚了</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体"> 
<P></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 18pt"><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">例</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">3</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">：</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体"> 
<P></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 18pt"><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体"><SPAN style="mso-tab-count: 2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>String s0= “kvill”; 
<P></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 18pt"><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体"><SPAN style="mso-tab-count: 2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>String s1=new String(”kvill”); 
<P></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 18pt"><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体"><SPAN style="mso-tab-count: 2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>String s2=new String(“kvill”); 
<P></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 18pt"><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体"><SPAN style="mso-tab-count: 2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>System.out.println( s0==s1 ); 
<P></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 18pt"><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体"><SPAN style="mso-tab-count: 2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>System.out.println( “**********” ); 
<P></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 18pt"><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体"><SPAN style="mso-tab-count: 1"></SPAN><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>s1.intern(); 
<P></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 18pt"><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体"><SPAN style="mso-tab-count: 2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>s2=s2.intern();<SPAN style="mso-tab-count: 2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>//</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">把常量池中“</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">kvill</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">”的引用赋给</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">s2 
<P></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 18pt"><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体"><SPAN style="mso-tab-count: 2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>System.out.println( s0==s1); 
<P></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 39pt; TEXT-INDENT: 3pt"><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">System.out.println( s0==s1.intern() ); 
<P></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 39pt; TEXT-INDENT: 3pt"><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">System.out.println( s0==s2 ); 
<P></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 18pt"><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体"><SPAN style="mso-tab-count: 2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">结果为：</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体"> 
<P></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 39pt; TEXT-INDENT: 3pt"><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">false<SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>
<P></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 39pt; TEXT-INDENT: 3pt"><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">********** 
<P></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 39pt; TEXT-INDENT: 3pt"><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">false<SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>//</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">虽然执行了</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">s1.intern(),</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">但它的返回值没有赋给</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">s1 
<P></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 39pt; TEXT-INDENT: 3pt"><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">true<SPAN style="mso-tab-count: 1"> </SPAN><SPAN style="mso-spacerun: yes">&nbsp;&nbsp; </SPAN>//</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">说明</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">s1.intern()</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">返回的是常量池中</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">”kvill”</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">的引用</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体"> 
<P></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 39pt; TEXT-INDENT: 3pt"><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">true<SPAN style="mso-tab-count: 1"> </SPAN>
<P></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>
<P></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">最后我再破除一个错误的理解：</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体"> 
<P></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 18pt"><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体"><SPAN style="mso-tab-count: 1"></SPAN></SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">有人说，“使用</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">String.intern()</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">方法则可以将一个</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">String</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">类的保存到一个全局</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">String</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">表中</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体"> 
<P></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 18pt"><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">如果具有相同值的</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">Unicode</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">字符串已经在这个表中，那么该方法返回表中已有字符串的地址</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体"> 
<P></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 18pt"><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">如果在表中没有相同值的字符串，则将自己的地址注册到表中“</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体"> 
<P></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 18pt"><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">如果我把他说的这个全局的</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">String</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">表理解为常量池的话，他的最后一句话，“如果在表中没有相同值的字符串，则将自己的地址注册到表中”是错的：</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体"> 
<P></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 18pt"><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">看例</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">4</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">：</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体"> 
<P></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 18pt"><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体"><SPAN style="mso-tab-count: 2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>String s1=new String("kvill"); 
<P></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 18pt"><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体"><SPAN style="mso-tab-count: 2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>String s2=s1.intern(); 
<P></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 38.95pt; mso-para-margin-left: 3.71gd"><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">System.out.println( s1==s1.intern() ); 
<P></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 38.95pt; mso-para-margin-left: 3.71gd"><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">System.out.println( s1+"<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>"+s2 ); 
<P></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 38.95pt; mso-para-margin-left: 3.71gd"><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">System.out.println( s2==s1.intern() ); 
<P></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 38.95pt; mso-para-margin-left: 3.71gd"><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">结果：</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体"> 
<P></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 38.95pt; mso-para-margin-left: 3.71gd"><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">false 
<P></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 38.95pt; mso-para-margin-left: 3.71gd"><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">kvill<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>kvill 
<P></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 38.95pt; mso-para-margin-left: 3.71gd"><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">true 
<P></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">在这个类中我们没有声名一个</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">”kvill”</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">常量，所以常量池中一开始是没有</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">”kvill”</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">的</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体"> 
<P></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">当我们调用</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">s1.intern()</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">后就在常量池中新添加了一个</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">”kvill”</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">常量，原来的不在常量池中的</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">”kvill”</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">仍然存在，也就不是“将自己的地址注册到常量池中”了。</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体"> 
<P></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>s1==s1.intern()</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">为</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">false</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">说明原来的“</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">kvill</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">”仍然存在；</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体"> 
<P></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>s2</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">现在为常量池中“</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">kvill”</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">的地址，所以有</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">s2==s1.intern()</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">为</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">true</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">。</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体"> 
<P></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 18pt; TEXT-INDENT: -18pt; tab-stops: list 18.0pt; mso-list: l0 level1 lfo1"><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 'Times New Roman'"><SPAN style="mso-list: Ignore">5.<SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">关于</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">equals()</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">和</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">==: 
<P></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 18pt"><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">这个对于</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">String</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">简单来说就是比较两字符串的</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">Unicode</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">序列是否相当，如果相等返回</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">true;</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">而</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">==</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">是比较两字符串的地址是否相同，也就是是否是同一个字符串的引用。</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体"> 
<P></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 18pt; TEXT-INDENT: -18pt; tab-stops: list 18.0pt; mso-list: l0 level1 lfo1"><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 'Times New Roman'"><SPAN style="mso-list: Ignore">6.<SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">关于</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">String</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">是不可变的。</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体"> 
<P></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 18pt"><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">这一说又要说很多，大家只要知道</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">String</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">的实例一旦生成就不会再改变了，</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体"> 
<P></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 18pt"><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">比如说：</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">String str=”kv”+”ill”+”<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>“+”ans”; 
<P></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 18pt"><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">就是有</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">4</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">个字符串常量，首先</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">”kv”</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">和</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">”ill”</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">生成了</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">”kvill”</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">存在内存中，然后</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">”kvill”</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">又和</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">”<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>“ </SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">生成</SPAN><SPAN style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体"> <SPAN lang=EN-US>”kvill<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>“</SPAN></SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">存在内存中，最后又和生成了</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">”kvill<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>ans”;</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">并把这个字符串的地址赋给了</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">str,</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">就是因为</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">String</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">的“不可变”产生了很多临时变量，这也就是为什么建议用</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">StringBuffer</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">的原因了，因为</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 新宋体">StringBuffer</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">是可改变的</SPAN></P><img src ="http://www.blogjava.net/hmily1211/aggbug/22793.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/hmily1211/" target="_blank">活在JAVA岛的日子</a> 2005-12-06 22:28 <a href="http://www.blogjava.net/hmily1211/archive/2005/12/06/22793.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Vetor Arraylist list</title><link>http://www.blogjava.net/hmily1211/archive/2005/11/25/21360.html</link><dc:creator>活在JAVA岛的日子</dc:creator><author>活在JAVA岛的日子</author><pubDate>Thu, 24 Nov 2005 17:05:00 GMT</pubDate><guid>http://www.blogjava.net/hmily1211/archive/2005/11/25/21360.html</guid><wfw:comment>http://www.blogjava.net/hmily1211/comments/21360.html</wfw:comment><comments>http://www.blogjava.net/hmily1211/archive/2005/11/25/21360.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/hmily1211/comments/commentRss/21360.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/hmily1211/services/trackbacks/21360.html</trackback:ping><description><![CDATA[<TABLE cellSpacing=0 cellPadding=0 width="95%" border=0>
<TBODY>
<TR>
<TD align=middle width="100%">
<H2><B>Vector、ArrayList和List的异同[zz]</B></H2></TD></TR>
<TR>
<TD width="100%"><SPAN id=ArticleContent1_ArticleContent1_lblContent><FONT face=Arial size=4>线性表，链表，哈希表是常用的数据结构，在进行Java开发时，JDK已经为我们提供了一系列相应的类来实现基本的数据结构。这些类均在java.util包中。本文试图通过简单的描述，向读者阐述各个类的作用以及如何正确使用这些类。 </FONT></SPAN>
<P>
<DIV class=post>
<DIV class=postcontent><SPAN id=ArticleContent1_ArticleContent1_lblContent>
<P><FONT face=Arial size=4>Collection<BR>├List<BR>│├LinkedList<BR>│├ArrayList<BR>│└Vector<BR>│　└Stack<BR>└Set<BR>Map<BR>├Hashtable<BR>├HashMap<BR>└WeakHashMap</FONT></P>
<P><FONT face=Arial size=4>Collection接口<BR>　　Collection是最基本的集合接口，一个Collection代表一组Object，即Collection的元素（Elements）。一些Collection允许相同的元素而另一些不行。一些能排序而另一些不行。Java SDK不提供直接继承自Collection的类，Java SDK提供的类都是继承自Collection的“子接口”如List和Set。<BR>　　所有实现Collection接口的类都必须提供两个标准的构造函数：无参数的构造函数用于创建一个空的Collection，有一个Collection参数的构造函数用于创建一个新的Collection，这个新的Collection与传入的Collection有相同的元素。后一个构造函数允许用户复制一个Collection。<BR>　　如何遍历Collection中的每一个元素？不论Collection的实际类型如何，它都支持一个iterator()的方法，该方法返回一个迭代子，使用该迭代子即可逐一访问Collection中每一个元素。典型的用法如下：<BR>　　　　Iterator it = collection.iterator(); // 获得一个迭代子<BR>　　　　while(it.hasNext()) {<BR>　　　　　　Object obj = it.next(); // 得到下一个元素<BR>　　　　}<BR>　　由Collection接口派生的两个接口是List和Set。</FONT></P>
<P><FONT face=Arial size=4>List接口<BR>　　List是有序的Collection，使用此接口能够精确的控制每个元素插入的位置。用户能够使用索引（元素在List中的位置，类似于数组下标）来访问List中的元素，这类似于Java的数组。<BR>和下面要提到的Set不同，List允许有相同的元素。<BR>　　除了具有Collection接口必备的iterator()方法外，List还提供一个listIterator()方法，返回一个ListIterator接口，和标准的Iterator接口相比，ListIterator多了一些add()之类的方法，允许添加，删除，设定元素，还能向前或向后遍历。<BR>　　实现List接口的常用类有LinkedList，ArrayList，Vector和Stack。</FONT></P>
<P><FONT face=Arial size=4>LinkedList类<BR>　　LinkedList实现了List接口，允许null元素。此外LinkedList提供额外的get，remove，insert方法在LinkedList的首部或尾部。这些操作使LinkedList可被用作堆栈（stack），队列（queue）或双向队列（deque）。<BR>　　注意LinkedList没有同步方法。如果多个线程同时访问一个List，则必须自己实现访问同步。一种解决方法是在创建List时构造一个同步的List：<BR>　　　　List list = Collections.synchronizedList(new LinkedList(...));</FONT></P>
<P><FONT face=Arial size=4>ArrayList类<BR>　　ArrayList实现了可变大小的数组。它允许所有元素，包括null。ArrayList没有同步。<BR>size，isEmpty，get，set方法运行时间为常数。但是add方法开销为分摊的常数，添加n个元素需要O(n)的时间。其他的方法运行时间为线性。<BR>　　每个ArrayList实例都有一个容量（Capacity），即用于存储元素的数组的大小。这个容量可随着不断添加新元素而自动增加，但是增长算法并没有定义。当需要插入大量元素时，在插入前可以调用ensureCapacity方法来增加ArrayList的容量以提高插入效率。<BR>　　和LinkedList一样，ArrayList也是非同步的（unsynchronized）。</FONT></P>
<P><FONT face=Arial size=4>Vector类<BR>　　Vector非常类似ArrayList，但是Vector是同步的。由Vector创建的Iterator，虽然和ArrayList创建的Iterator是同一接口，但是，因为Vector是同步的，当一个Iterator被创建而且正在被使用，另一个线程改变了Vector的状态（例如，添加或删除了一些元素），这时调用Iterator的方法时将抛出ConcurrentModificationException，因此必须捕获该异常。</FONT></P>
<P><FONT face=Arial size=4>Stack 类<BR>　　Stack继承自Vector，实现一个后进先出的堆栈。Stack提供5个额外的方法使得Vector得以被当作堆栈使用。基本的push和pop方法，还有peek方法得到栈顶的元素，empty方法测试堆栈是否为空，search方法检测一个元素在堆栈中的位置。Stack刚创建后是空栈。</FONT></P>
<P><FONT face=Arial size=4>Set接口<BR>　　Set是一种不包含重复的元素的Collection，即任意的两个元素e1和e2都有e1.equals(e2)=false，Set最多有一个null元素。<BR>　　很明显，Set的构造函数有一个约束条件，传入的Collection参数不能包含重复的元素。<BR>　　请注意：必须小心操作可变对象（Mutable Object）。如果一个Set中的可变元素改变了自身状态导致Object.equals(Object)=true将导致一些问题。</FONT></P>
<P><FONT face=Arial size=4>Map接口<BR>　　请注意，Map没有继承Collection接口，Map提供key到value的映射。一个Map中不能包含相同的key，每个key只能映射一个value。Map接口提供3种集合的视图，Map的内容可以被当作一组key集合，一组value集合，或者一组key-value映射。</FONT></P>
<P><FONT face=Arial size=4>Hashtable类<BR>　　Hashtable继承Map接口，实现一个key-value映射的哈希表。任何非空（non-null）的对象都可作为key或者value。<BR>　　添加数据使用put(key, value)，取出数据使用get(key)，这两个基本操作的时间开销为常数。<BR>Hashtable通过initial capacity和load factor两个参数调整性能。通常缺省的load factor 0.75较好地实现了时间和空间的均衡。增大load factor可以节省空间但相应的查找时间将增大，这会影响像get和put这样的操作。<BR>使用Hashtable的简单示例如下，将1，2，3放到Hashtable中，他们的key分别是”one”，”two”，”three”：<BR>　　　　Hashtable numbers = new Hashtable();<BR>　　　　numbers.put(“one”, new Integer(1));<BR>　　　　numbers.put(“two”, new Integer(2));<BR>　　　　numbers.put(“three”, new Integer(3));<BR>　　要取出一个数，比如2，用相应的key：<BR>　　　　Integer n = (Integer)numbers.get(“two”);<BR>　　　　System.out.println(“two = ” + n);<BR>　　由于作为key的对象将通过计算其散列函数来确定与之对应的value的位置，因此任何作为key的对象都必须实现hashCode和equals方法。hashCode和equals方法继承自根类Object，如果你用自定义的类当作key的话，要相当小心，按照散列函数的定义，如果两个对象相同，即obj1.equals(obj2)=true，则它们的hashCode必须相同，但如果两个对象不同，则它们的hashCode不一定不同，如果两个不同对象的hashCode相同，这种现象称为冲突，冲突会导致操作哈希表的时间开销增大，所以尽量定义好的hashCode()方法，能加快哈希表的操作。<BR>　　如果相同的对象有不同的hashCode，对哈希表的操作会出现意想不到的结果（期待的get方法返回null），要避免这种问题，只需要牢记一条：要同时复写equals方法和hashCode方法，而不要只写其中一个。<BR>　　Hashtable是同步的。</FONT></P>
<P><FONT face=Arial size=4>HashMap类<BR>　　HashMap和Hashtable类似，不同之处在于HashMap是非同步的，并且允许null，即null value和null key。，但是将HashMap视为Collection时（values()方法可返回Collection），其迭代子操作时间开销和HashMap的容量成比例。因此，如果迭代操作的性能相当重要的话，不要将HashMap的初始化容量设得过高，或者load factor过低。</FONT></P>
<P><FONT face=Arial size=4>WeakHashMap类<BR>　　WeakHashMap是一种改进的HashMap，它对key实行“弱引用”，如果一个key不再被外部所引用，那么该key可以被GC回收。</FONT></P>
<P><FONT face=Arial size=4>总结<BR>　　如果涉及到堆栈，队列等操作，应该考虑用List，对于需要快速插入，删除元素，应该使用LinkedList，如果需要快速随机访问元素，应该使用ArrayList。<BR>　　如果程序在单线程环境中，或者访问仅仅在一个线程中进行，考虑非同步的类，其效率较高，如果多个线程可能同时操作一个类，应该使用同步的类。<BR>　　要特别注意对哈希表的操作，作为key的对象要正确复写equals和hashCode方法。<BR>　　尽量返回接口而非实际的类型，如返回List而非ArrayList，这样如果以后需要将ArrayList换成LinkedList时，客户端代码不用改变。这就是针对抽象编程。<BR><BR><FONT size=3><STRONG>[b]同步性<BR></STRONG>Vector是同步的。这个类中的一些方法保证了Vector中的对象是线程安全的。而ArrayList则是异步的，因此ArrayList中的对象并不是线程安全的。因为同步的要求会影响执行的效率，所以如果你不需要线程安全的集合那么使用ArrayList是一个很好的选择，这样可以避免由于同步带来的不必要的性能开销。<BR><B>数据增长</B><BR>从内部实现机制来讲ArrayList和Vector都是使用数组(Array)来控制集合中的对象。当你向这两种类型中增加元素的时候，如果元素的数目超出了内部数组目前的长度它们都需要扩展内部数组的长度，Vector缺省情况下自动增长原来一倍的数组长度，ArrayList是原来的50%,所以最后你获得的这个集合所占的空间总是比你实际需要的要大。所以如果你要在集合中保存大量的数据那么使用Vector有一些优势，因为你可以通过设置集合的初始化大小来避免不必要的资源开销。<BR><B>使用模式</B><BR>在ArrayList和Vector中，从一个指定的位置（通过索引）查找数据或是在集合的末尾增加、移除一个元素所花费的时间是一样的，这个时间我们用O(1)表示。但是，如果在集合的其他位置增加或移除元素那么花费的时间会呈线形增长：O(n-i)，其中n代表集合中元素的个数，i代表元素增加或移除元素的索引位置。为什么会这样呢？以为在进行上述操作的时候集合中第i和第i个元素之后的所有元素都要执行位移的操作。这一切意味着什么呢？<BR>这意味着，你只是查找特定位置的元素或只在集合的末端增加、移除元素，那么使用Vector或ArrayList都可以。如果是其他操作，你最好选择其他的集合操作类。比如，LinkList集合类在增加或移除集合中任何位置的元素所花费的时间都是一样的—O(1)，但它在索引一个元素的使用缺比较慢－O(i),其中i是索引的位置.使用ArrayList也很容易，因为你可以简单的使用索引来代替创建iterator对象的操作。LinkList也会为每个插入的元素创建对象，所有你要明白它也会带来额外的开销。<BR>最后，在《Practical Java》一书中Peter Haggar建议使用一个简单的数组（Array）来代替Vector或ArrayList。尤其是对于执行效率要求高的程序更应如此。因为使用数组(Array)避免了同步、额外的方法调用和不必要的重新分配空间的操作。</FONT><BR></FONT></P></SPAN></DIV></DIV></TD></TR></TBODY></TABLE><img src ="http://www.blogjava.net/hmily1211/aggbug/21360.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/hmily1211/" target="_blank">活在JAVA岛的日子</a> 2005-11-25 01:05 <a href="http://www.blogjava.net/hmily1211/archive/2005/11/25/21360.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>java.util包(转贴)</title><link>http://www.blogjava.net/hmily1211/archive/2005/11/24/21318.html</link><dc:creator>活在JAVA岛的日子</dc:creator><author>活在JAVA岛的日子</author><pubDate>Thu, 24 Nov 2005 09:55:00 GMT</pubDate><guid>http://www.blogjava.net/hmily1211/archive/2005/11/24/21318.html</guid><wfw:comment>http://www.blogjava.net/hmily1211/comments/21318.html</wfw:comment><comments>http://www.blogjava.net/hmily1211/archive/2005/11/24/21318.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/hmily1211/comments/commentRss/21318.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/hmily1211/services/trackbacks/21318.html</trackback:ping><description><![CDATA[ArrayList是List接口的一个可变长数组实现。实现了所有List接口的操作，并允许存储null值。除了没有进行同步，ArrayList基本等同于Vector。在Vector中几乎对所有的方法都进行了同步，但ArrayList仅对writeObject和readObject进行了同步，其它比如add(Object)、remove(int)等都没有同步。<BR><BR>1.存储<BR>ArrayList使用一个Object的数组存储元素。<BR>private&nbsp;transient&nbsp;Object&nbsp;elementData[];<BR>ArrayList实现了java.io.Serializable接口，这儿的transient标示这个属性不需要自动序列化。下面会在writeObject()方法中详细讲解为什么要这样作。<BR><BR>2.add和remove<BR><BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;boolean&nbsp;add(Object&nbsp;o)&nbsp;{&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;ensureCapacity(size&nbsp;+&nbsp;1);&nbsp;&nbsp;//&nbsp;Increments&nbsp;modCount!!&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;elementData[size++]&nbsp;=&nbsp;o;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;true;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;<BR><BR>注意这儿的ensureCapacity()方法，它的作用是保证elementData数组的长度可以容纳一个新元素。在“自动变长机制”中将详细讲解。<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;Object&nbsp;remove(int&nbsp;index)&nbsp;{&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;RangeCheck(index);&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;modCount++;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;Object&nbsp;oldValue&nbsp;=&nbsp;elementData[index];&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;numMoved&nbsp;=&nbsp;size&nbsp;-&nbsp;index&nbsp;-&nbsp;1;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(numMoved&nbsp;&gt;&nbsp;0)&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.arraycopy(elementData,&nbsp;index+1,&nbsp;elementData,&nbsp;index,&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;numMoved);&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;elementData[--size]&nbsp;=&nbsp;null;&nbsp;//&nbsp;Let&nbsp;gc&nbsp;do&nbsp;its&nbsp;work&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;oldValue;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;<BR><BR>RangeCheck()的作用是进行边界检查。由于ArrayList采用一个对象数组存储元素，所以在删除一个元素时需要把后面的元素前移。删除一个元素时只是把该元素在elementData数组中的引用置为null，具体的对象的销毁由垃圾收集器负责。<BR>modCount的作用将在下面的“iterator()中的同步”中说明。<BR>注：在前移时使用了System提供的一个实用方法：arraycopy()，在本例中可以看出System.arraycopy()方法可以对同一个数组进行操作，这个方法是一个native方法，如果对同一个数组进行操作时，会首先把从源部分拷贝到一个临时数组，在把临时数组的元素拷贝到目标位置。<BR><BR>3.自动变长机制<BR>在实例化一个ArrayList时，你可以指定一个初始容量。这个容量就是elementData数组的初始长度。如果你使用：<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;ArrayList&nbsp;list&nbsp;=&nbsp;new&nbsp;ArrayList();&nbsp;<BR><BR>则使用缺省的容量：10。<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;ArrayList()&nbsp;{&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;this(10);&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;<BR><BR>ArrayList提供了四种add()方法，<BR><BR>public&nbsp;boolean&nbsp;add(Object&nbsp;o)<BR><BR>public&nbsp;void&nbsp;add(int&nbsp;index,&nbsp;Object&nbsp;element)<BR><BR>public&nbsp;boolean&nbsp;addAll(Collection&nbsp;c)<BR><BR>public&nbsp;boolean&nbsp;addAll(int&nbsp;index,&nbsp;Collection&nbsp;c)<BR><BR>在每一种add()方法中，都首先调用了一个ensureCapacity(int&nbsp;miniCapacity)方法，这个方法保证elementData数组的长度不小于miniCapacity。ArrayList的自动变长机制就是在这个方法中实现的。<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp;ensureCapacity(int&nbsp;minCapacity)&nbsp;{&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;modCount++;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;oldCapacity&nbsp;=&nbsp;elementData.length;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(minCapacity&nbsp;&gt;&nbsp;oldCapacity)&nbsp;{&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Object&nbsp;oldData[]&nbsp;=&nbsp;elementData;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;newCapacity&nbsp;=&nbsp;(oldCapacity&nbsp;*&nbsp;3)/2&nbsp;+&nbsp;1;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(newCapacity&nbsp;&lt;&nbsp;minCapacity)&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;newCapacity&nbsp;=&nbsp;minCapacity;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elementData&nbsp;=&nbsp;new&nbsp;Object[newCapacity];&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.arraycopy(oldData,&nbsp;0,&nbsp;elementData,&nbsp;0,&nbsp;size);&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;<BR><BR>从这个方法实现中可以看出ArrayList每次扩容，都扩大到原来大小的1.5倍。<BR>每种add()方法的实现都大同小异，下面给出add(Object)方法的实现：<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;boolean&nbsp;add(Object&nbsp;o)&nbsp;{&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;ensureCapacity(size&nbsp;+&nbsp;1);&nbsp;&nbsp;//&nbsp;Increments&nbsp;modCount!!&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;elementData[size++]&nbsp;=&nbsp;o;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;true;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;<BR><BR><BR>4.iterator()中的同步<BR>在父类AbstractList中定义了一个int型的属性：modCount，记录了ArrayList结构性变化的次数。<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;protected&nbsp;transient&nbsp;int&nbsp;modCount&nbsp;=&nbsp;0;&nbsp;<BR><BR>在ArrayList的所有涉及结构变化的方法中都增加modCount的值，包括：add()、remove()、addAll()、removeRange()及clear()方法。这些方法每调用一次，modCount的值就加1。<BR>注：add()及addAll()方法的modCount的值是在其中调用的ensureCapacity()方法中增加的。<BR><BR>AbstractList中的iterator()方法（ArrayList直接继承了这个方法）使用了一个私有内部成员类Itr，生成一个Itr对象（Iterator接口）返回：<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;Iterator&nbsp;iterator()&nbsp;{&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;new&nbsp;Itr();&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;<BR><BR>Itr实现了Iterator()接口，其中也定义了一个int型的属性：expectedModCount，这个属性在Itr类初始化时被赋予ArrayList对象的modCount属性的值。<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;expectedModCount&nbsp;=&nbsp;modCount;&nbsp;<BR><BR>注：内部成员类Itr也是ArrayList类的一个成员，它可以访问所有的AbstractList的属性和方法。理解了这一点，Itr类的实现就容易理解了。<BR><BR>在Itr.hasNext()方法中：<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;boolean&nbsp;hasNext()&nbsp;{&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;cursor&nbsp;!=&nbsp;size();&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;<BR><BR>调用了AbstractList的size()方法，比较当前光标位置是否越界。<BR><BR>在Itr.next()方法中，Itr也调用了定义在AbstractList中的get(int)方法，返回当前光标处的元素：<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;Object&nbsp;next()&nbsp;{&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try&nbsp;{&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Object&nbsp;next&nbsp;=&nbsp;get(cursor);&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;checkForComodification();&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lastRet&nbsp;=&nbsp;cursor++;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;next;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;catch(IndexOutOfBoundsException&nbsp;e)&nbsp;{&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;checkForComodification();&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throw&nbsp;new&nbsp;NoSuchElementException();&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;<BR><BR>注意，在next()方法中调用了checkForComodification()方法，进行对修改的同步检查：<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;final&nbsp;void&nbsp;checkForComodification()&nbsp;{&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(modCount&nbsp;!=&nbsp;expectedModCount)&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throw&nbsp;new&nbsp;ConcurrentModificationException();&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;<BR><BR>现在对modCount和expectedModCount的作用应该非常清楚了。在对一个集合对象进行跌代操作的同时，并不限制对集合对象的元素进行操作，这些操作包括一些可能引起跌代错误的add()或remove()等危险操作。在AbstractList中，使用了一个简单的机制来规避这些风险。这就是modCount和expectedModCount的作用所在。<BR><BR>5.序列化支持<BR>ArrayList实现了java.io.Serializable接口，所以ArrayList对象可以序列化到持久存储介质中。ArrayList的主要属性定义如下：<BR><BR>private&nbsp;static&nbsp;final&nbsp;long&nbsp;serialVersionUID&nbsp;=&nbsp;8683452581122892189L;<BR><BR>private&nbsp;transient&nbsp;Object&nbsp;elementData[];<BR><BR>private&nbsp;int&nbsp;size;<BR><BR>可以看出serialVersionUID和size都将自动序列化到介质中，但elementData数组对象却定义为transient了。也就是说ArrayList中的所有这些元素都不会自动系列化到介质中。为什么要这样实现？因为elementData数组中存储的“元素”其实仅是对这些元素的一个引用，并不是真正的对象，序列化一个对象的引用是毫无意义的，因为序列化是为了反序列化，当你反序列化时，这些对象的引用已经不可能指向原来的对象了。所以在这儿需要手工的对ArrayList的元素进行序列化操作。这就是writeObject()的作用。<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;synchronized&nbsp;void&nbsp;writeObject(java.io.ObjectOutputStream&nbsp;s)&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throws&nbsp;java.io.IOException{&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;Write&nbsp;out&nbsp;element&nbsp;count,&nbsp;and&nbsp;any&nbsp;hidden&nbsp;stuff&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;s.defaultWriteObject();&nbsp;<BR>&nbsp;&nbsp;&nbsp;//&nbsp;Write&nbsp;out&nbsp;array&nbsp;length&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;s.writeInt(elementData.length);&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;Write&nbsp;out&nbsp;all&nbsp;elements&nbsp;in&nbsp;the&nbsp;proper&nbsp;order.&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(int&nbsp;i=0;&nbsp;i&lt;size;&nbsp;i++)&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s.writeObject(elementData[i]);&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;<BR><BR>这样元素数组elementData中的所以元素对象就可以正确地序列化到存储介质了。<BR>对应的readObject()也按照writeObject()方法的顺序从输入流中读取：<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;synchronized&nbsp;void&nbsp;readObject(java.io.ObjectInputStream&nbsp;s)&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throws&nbsp;java.io.IOException,&nbsp;ClassNotFoundException&nbsp;{&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;Read&nbsp;in&nbsp;size,&nbsp;and&nbsp;any&nbsp;hidden&nbsp;stuff&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;s.defaultReadObject();&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;Read&nbsp;in&nbsp;array&nbsp;length&nbsp;and&nbsp;allocate&nbsp;array&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;arrayLength&nbsp;=&nbsp;s.readInt();&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;elementData&nbsp;=&nbsp;new&nbsp;Object[arrayLength];&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;Read&nbsp;in&nbsp;all&nbsp;elements&nbsp;in&nbsp;the&nbsp;proper&nbsp;order.&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(int&nbsp;i=0;&nbsp;i&lt;size;&nbsp;i++)&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elementData[i]&nbsp;=&nbsp;s.readObject();&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;<BR><BR>本章介绍Java的实用工具类库java.util包。在这个包中，Java提供了一些实用的方法和数据结构。例如，Java提供日期(Data)类、日历(Calendar)类来产生和获取日期及时间，提供随机数(Random)类产生各种类型的随机数，还提供了堆栈(Stack)、向量(Vector)&nbsp;、位集合(Bitset)以及哈希表(Hashtable)等类来表示相应的数据结构。<BR>　　图1.1给出了java.util包的基本层次结构图。下面我们将具体介绍其中几个重要的类。<BR>　　　　　　　　　　　┌java.util.BitSet<BR>　　　　　　　　　　　│java.util.Calendar<BR>　　　　　　　　　　　│　　　　　　└java.util.GregorianCalendar<BR>　　　　　　　　　　　│java.util.Date<BR>　　　　　　　　　　　│java.util.Dictionary<BR>　　　　　　　　　　　│　　　　　　└java.util.Hashtable<BR>　　　　　　　　　　　│　　　　　　　　　　　　　└java.util.Properties<BR>　　　　　　　　　　　│java.util.EventObject<BR>　　　　　　　　　　　│java.util.ResourceBundle<BR>　　　　　　　┌普通类┤　　　　　　├java.util.ListResourceBundle<BR>　　　　　　　│　　　│　　　　　　└java.util.PropertyResourceBundle<BR>　　　　　　　│　　　│java.util.Local<BR>　　　　　　　│　　　│java.util.Observable<BR>　　　　　　　│　　　│java.util.Random<BR>　　　　　　　│　　　│java.util.StringTokenizer<BR>　　　　　　　│　　　│java.util.Vector<BR>　　　　　　　│　　　│　　　　　　└java.util.Stack<BR>　　Java.util┤　　　└java.util.TimeZone<BR>　　　　　　　│　　　　　　　　　　└java.util.SimpleTimeZone<BR>　　　　　　　│　　　┌java.util.Enumeration<BR>　　　　　　　├接　口┤java.util.EventListener<BR>　　　　　　　│　　　└java.util.Observer<BR>　　　　　　　│　　　┌java.util.EmptyStackException<BR>　　　　　　　└异常类┤java.util.MissingResourceException<BR>　　　　　　　　　　　│java.util.NoSuchElementException<BR>　　　　　　　　　　　└java.util.TooManyListenersException<BR>　　　　　　　图1.1&nbsp;java.util包的基本层次结构<BR><BR><BR>1.2&nbsp;日期类Date<BR><BR>　　Java在日期类中封装了有关日期和时间的信息，用户可以通过调用相应的方法来获取系统时间或设置日期和时间。Date类中有很多方法在JDK1.0公布后已经过时了，在8.3中我们将介绍JDK1.0中新加的用于替代Date的功能的其它类。<BR>　　在日期类中共定义了六种构造函数。<BR>　　(1)public&nbsp;Date()<BR>　　创建的日期类对象的日期时间被设置成创建时刻相对应的日期时间。<BR>　　例&nbsp;Date&nbsp;today=new&nbsp;Date()；//today被设置成创建时刻相对应的日期时间。<BR>　　(2)public&nbsp;Date&nbsp;(long&nbsp;date)<BR>　　long&nbsp;型的参数date可以通过调用Date类中的static方法parse(String&nbsp;s)来获得。<BR>　　例&nbsp;long&nbsp;l=Date.parse("Mon&nbsp;6&nbsp;Jan&nbsp;1997&nbsp;13:3:00");<BR>　　　　Date&nbsp;day=new&nbsp;Date(l);<BR>　　//day中时间为1997年&nbsp;1月6号星期一，13:3:00。<BR>　　(3)public&nbsp;Date(String&nbsp;s)<BR>　　按字符串s产生一日期对象。s的格式与方法parse中字符串参数的模式相同。<BR>　　例&nbsp;Date&nbsp;day=new&nbsp;Date("Mon&nbsp;6&nbsp;Jan&nbsp;1997&nbsp;13:3:00");<BR>　　//day&nbsp;中时间为1997年1月6号星期一，13:3:00.<BR>　　(4)public&nbsp;Date(int&nbsp;year,int&nbsp;month,int&nbsp;date)<BR>　　(5)public&nbsp;Date(int&nbsp;year,int&nbsp;month,int&nbsp;date,int&nbsp;hrs,int&nbsp;min)<BR>　　(6)public&nbsp;Date(int&nbsp;year,int&nbsp;month,int&nbsp;date,int&nbsp;hrs,int&nbsp;min,int&nbsp;sec)<BR>　　按给定的参数创建一日期对象。<BR>　　参数说明：<BR>　　year的值为：需设定的年份-1900。例如需设定的年份是1997则year的值应为97，即1997-1900的结果。所以Date中可设定的年份最小为1900；<BR>　　month的值域为0～11，0代表1月，11表代表12月；<BR>　　date的值域在1～31之间；<BR>　　hrs的值域在0～23之间。从午夜到次日凌晨1点间hrs=0，从中午到下午1点间hrs=12；<BR>　　min和sec的值域在0～59之间。<BR>　　例&nbsp;Date&nbsp;day=new&nbsp;Date(11,3,4);<BR>　　//day中的时间为：04-Apr-11&nbsp;12:00:00&nbsp;AM<BR>另外，还可以给出不正确的参数。<BR>　　例　设定时间为1910年2月30日，它将被解释成3月2日。<BR>　　Date&nbsp;day=new&nbsp;Date(10,1,30,10,12,34);<BR>　　System.out.println("Day's&nbsp;date&nbsp;is:"+day);<BR>　　//打印结果为：Day's&nbsp;date&nbsp;is:Web&nbsp;Mar&nbsp;02&nbsp;10:13:34&nbsp;GMT+08:00&nbsp;1910<BR>　　下面我们给出一些Date类中常用方法。<BR>　　(1)public&nbsp;static&nbsp;long&nbsp;UTC(int&nbsp;year,int&nbsp;month,int&nbsp;date,int&nbsp;hrs.&nbsp;int&nbsp;min,int&nbsp;sec)<BR>　　该方法将利用给定参数计算UTC值。UTC是一种计时体制，与GMT(格林威治时间)的计时体系略有差别。UTC计时体系是基于原子时钟的，而GTMT计时体系是基于天文学观测的。计算中使用的一般为GMT计时体系。<BR>　　(2)public&nbsp;static&nbsp;long&nbsp;parse(String&nbsp;s)<BR>　　该方法将字符串s转换成一个long型的日期。在介绍构造方法Date(long&nbsp;date)时曾使用过这个方法。<BR>　　字符串s有一定的格式，一般为：<BR>　　(星期&nbsp;日&nbsp;年&nbsp;时间GMT+时区)<BR>　　若不注明时区，则为本地时区。<BR>　　(3)public&nbsp;void&nbsp;setMonth(int&nbsp;month)<BR>　　(4)public&nbsp;int&nbsp;getMonth()<BR>　　这两个方法分别为设定和获取月份值。<BR>　　获取的月份的值域为0～11，0代表1月，11代表12月。<BR>　　(5)public&nbsp;String&nbsp;toString()<BR>　　(6)public&nbsp;String&nbsp;toLocalString()<BR>　　(7)public&nbsp;String&nbsp;toGMTString()<BR>　　将给定日期对象转换成不同格式的字符串。它们对应的具体的格式可参看例子8.1。<BR>　　(8)public&nbsp;int&nbsp;getTimezoneOffset()<BR>　　该方法用于获取日期对象的时区偏移量。<BR>　　例8.1中对上面介绍的Date类中的基本方法进行了具体的应用，并打印了相应的结果。由于使用了一些过时的方法，所以编译时会有警告信息。另外，由于本例中的时间表示与平台有关，不同的JDK版本对此处理不完全相同，因此不同版本的JDK执行本例的结果可能有细微差异。<BR>　　例1.1&nbsp;DateApp.java<BR>　　import&nbsp;java.lang.System;<BR>　　import&nbsp;java.util.Date;<BR>　　public&nbsp;class&nbsp;DateApp{<BR>　　　public&nbsp;static&nbsp;void&nbsp;main(String&nbsp;args[]){<BR>　　　　Date&nbsp;today=new&nbsp;Date();<BR>　　　　//today中的日期被设成创建时刻的日期和时间，假设创建时刻为1997年3月<BR>　　　　//23日17时51分54秒。<BR>　　　　System.out.println("Today's&nbsp;date&nbsp;is&nbsp;"+today);<BR>　　　　//返回一般的时间表示法，本例中结果为<BR>　　　　//Today's&nbsp;date&nbsp;is&nbsp;Fri&nbsp;May&nbsp;23&nbsp;17:51:54&nbsp;1997<BR>　　　　System.out.println("Today's&nbsp;date(Internet&nbsp;GMT)is:"<BR>　　　　　+today.toGMTString());<BR>　　　　//返回结果为GMT时间表示法，本例中结果为<BR>　　　　//Today's&nbsp;date(Internet&nbsp;GMT)is:&nbsp;23&nbsp;May&nbsp;1997&nbsp;09:51:54:GMT<BR>　　　　System.out.println("Today's&nbsp;date(Locale)&nbsp;is:"<BR>　　　　　+today.toLocaleString());<BR>　　　　//返回结果为本地习惯的时间表示法，结果为<BR>　　　　//Today's&nbsp;date(Locale)is:05/23/97&nbsp;17:51:54<BR>　　　　System.out.println("Today's&nbsp;year&nbsp;is:&nbsp;"+today.getYear());<BR>　　　　System.out.println("Today's&nbsp;month&nbsp;is:&nbsp;"+(today.getMonth()+1));<BR>　　　　System.out.println("Today's&nbsp;date&nbsp;is:&nbsp;"+today.getDate());<BR>　　　　//调用Date类中方法，获取年月日的值。<BR>　　　　//下面调用了不同的构造方法来创建Date类的对象。<BR>　　　　Date&nbsp;day1=new&nbsp;Date(100,1,23,10,12,34);<BR>　　　　System.out.println("Day1's&nbsp;date&nbsp;is:&nbsp;"+day1);<BR>　　　　Date&nbsp;day2=new&nbsp;Date("Sat&nbsp;12&nbsp;Aug&nbsp;1996&nbsp;13:3:00");<BR>　　　　System.out.println("Day2's&nbsp;date&nbsp;is:&nbsp;"+day2);<BR>　　　　long&nbsp;l=&nbsp;Date.parse("Sat&nbsp;5&nbsp;Aug&nbsp;1996&nbsp;13:3:00&nbsp;GMT+0800");<BR>　　　　Date&nbsp;day3=&nbsp;new&nbsp;Date(l);<BR>　　　　System.out.println("Day3's&nbsp;date(GMT)is:&nbsp;"+day3.toGMTString());<BR>　　　　System.out.println("Day3's&nbsp;date(Locale)is:&nbsp;"<BR>　　　　　+day3.toLocaleString());<BR>　　　　System.out.println("Day3's&nbsp;time&nbsp;zone&nbsp;offset&nbsp;is:"<BR>　　　　　+day3.getTimezoneOffset());<BR>　　　}<BR>　　}<BR><BR>　　运行结果(JDK1.3版，与原文不同，原文是JDK1.0版)：<BR>　　E:\java\tutorial\java01&gt;java&nbsp;DateApp<BR>　　Today's&nbsp;date&nbsp;is&nbsp;Thu&nbsp;Dec&nbsp;27&nbsp;17:58:16&nbsp;CST&nbsp;2001<BR>　　Today's&nbsp;date(Internet&nbsp;GMT)is:27&nbsp;Dec&nbsp;2001&nbsp;09:58:16&nbsp;GMT<BR>　　Today's&nbsp;date(Locale)&nbsp;is:2001-12-27&nbsp;17:58:16<BR>　　Today's&nbsp;year&nbsp;is:&nbsp;101<BR>　　Today's&nbsp;month&nbsp;is:&nbsp;12<BR>　　Today's&nbsp;date&nbsp;is:&nbsp;27<BR>　　Day1's&nbsp;date&nbsp;is:&nbsp;Wed&nbsp;Feb&nbsp;23&nbsp;10:12:34&nbsp;CST&nbsp;2000<BR>　　Day2's&nbsp;date&nbsp;is:&nbsp;Fri&nbsp;Aug&nbsp;12&nbsp;13:03:00&nbsp;CST&nbsp;1996<BR>　　Day3's&nbsp;date(GMT)is:&nbsp;5&nbsp;Aug&nbsp;1996&nbsp;05:03:00&nbsp;GMT<BR>　　Day3's&nbsp;date(Locale)is:&nbsp;1996-8-5&nbsp;13:03:00<BR>　　Day3's&nbsp;time&nbsp;zone&nbsp;offset&nbsp;is:-480<BR><BR>　　E:\java\tutorial\java01&gt;<BR><BR>1.3&nbsp;日历类Calendar<BR><BR>　　在早期的JDK版本中，日期(Date)类附有两大功能：(1)允许用年、月、日、时、分、秒来解释日期：(2)允许对表示日期的字符串进行格式化和句法分析。在JDK1.1中提供了类Calendar来完成第一种功能，类DateFormat来完成第二项功能。dateFormat是java.text包中的一个类。与Date类有所不同的是，DateFormat类接受用各种语言和不同习惯表示的日期字符串。本节将介绍java.util包中的类Calendar及其它新增加的相关的类。<BR>　　类Calendar是一个抽象类，它完成日期(Date)类和普通日期表示法(即用一组整型域如YEAR，MONTH，DAY，HOUR表示日期)之间的转换。<BR>　　由于所使用的规则不同，不同的日历系统对同一个日期的解释有所不同。在JDK1.1中提供了Calendar类一个子类GregorianCalendar??它实现了世界上普遍使用的公历系统。当然用户也可以通过继承Calendar类，并增加所需规则，以实现不同的日历系统。<BR>　　第GregorianCalendar继承了Calendar类。本节将在介绍类GregorianCalendar的同时顺带介绍Calendar类中的相关方法。<BR>　　类GregorianCalendar提供了七种构造函数：<BR>　　(1)public&nbsp;GregorianCalendar()<BR>　　创建的对象中的相关值被设置成指定时区，缺省地点的当前时间，即程序运行时所处的时区、地点的当前时间。<BR>　　(2)public&nbsp;GregorianCalendar(TimeZone&nbsp;zone)<BR>　　创建的对象中的相关值被设置成指定时区zone，缺省地点的当前时间。<BR>　　(3)public&nbsp;GregorianCalendar(Locale&nbsp;aLocale)<BR>　　创建的对象中的相关值被设置成缺省时区，指定地点aLocale的当前时间。<BR>　　(4)public&nbsp;GregorianCalendar(TimeZone&nbsp;zone,Local&nbsp;aLocale)<BR>　　创建的对象中的相关值被设置成指定时区，指定地点的当前时间。<BR>　　上面使用到的类TimeZone的性质如下：<BR>　　TimeZone是java.util包中的一个类，其中封装了有关时区的信息。每一个时区对应一组ID。类TimeZone提供了一些方法完成时区与对应ID两者之间的转换。<BR>　　(Ⅰ)已知某个特定的ID，可以调用方法<BR>　　public&nbsp;static&nbsp;synchronized&nbsp;TimeZone&nbsp;getTimeZone(String&nbsp;ID)<BR>来获取对应的时区对象。<BR>　　例&nbsp;太平洋时区的ID为PST，用下面的方法可获取对应于太平洋时区的时区对象：<BR>　　TimeZone&nbsp;tz=TimeZone.getTimeZone("PST");<BR>　　调用方法getDefault()可以获取主机所处时区的对象。<BR>　　TimeZone&nbsp;tz=TimeZone.getDefault();<BR>　　(Ⅱ)调用以下方法可以获取时区的ID<BR>　　■public&nbsp;static&nbsp;synchronized&nbsp;String[]&nbsp;getavailableIDs(int&nbsp;rawOffset)<BR>　　根据给定时区偏移值获取ID数组。同一时区的不同地区的ID可能不同，这是由于不同地区对是否实施夏时制意见不统一而造成的。<BR>　　例String&nbsp;s[]=TimeZone.getAvailableIDs(-7*60*60*1000);<BR>　　打印s，结果为s[0]=PNT，s[1]=MST<BR>　　■public&nbsp;static&nbsp;synchronized&nbsp;String[]&nbsp;getAvailableIDs()<BR>　　获取提供的所有支持的ID。<BR>　　■public&nbsp;String&nbsp;getID()<BR>　　获取特定时区对象的ID。<BR>　　例&nbsp;TimeZone&nbsp;tz=TimeZone.getDefault();<BR>　　String&nbsp;s=tz.getID();<BR>　　打印s，结果为s=CTT。<BR>　　上面使用类的对象代表了一个特定的地理、政治或文化区域。Locale只是一种机制，它用来标识一类对象，Local本身并不包含此类对象。<BR>　　要获取一个Locale的对象有两种方法：<BR>　　(Ⅰ)调用Locale类的构造方法<BR>　　Locale(String&nbsp;language,String&nbsp;country)<BR>　　Locale(String&nbsp;language,String&nbsp;country,String&nbsp;variant)<BR>　　参数说明：language??在ISO-639中定义的代码，由两个小写字母组成。<BR>　　　　　　　country??在ISO-3166中定义的代码，由两个大写字母组成。<BR>　　　　　　　variant??售货商以及特定浏览器的代码，例如使用WIN代表Windows。<BR>　　(Ⅱ)调用Locale类中定义的常量<BR>　　Local类提供了大量的常量供用户创建Locale对象。<BR>　　例&nbsp;Locale.CHINA<BR>　　　　为中国创建一个Locale的对象。<BR>　　类TimeZone和类Locale中的其它方法，读者可查阅API。<BR>　　(5)public&nbsp;GregorianCalendar(int&nbsp;year,int&nbsp;month,int&nbsp;date)<BR>　　(6)public&nbsp;GregorianCalendar(int&nbsp;year,int&nbsp;month,int&nbsp;date,int&nbsp;hour,int&nbsp;minute)<BR>　　(7)public&nbsp;GregorianCalendar(int&nbsp;year,int&nbsp;month,int&nbsp;date,int&nbsp;hour,int&nbsp;minute,int&nbsp;second)<BR>　　用给定的日期和时间创建一个GregorianCalendar的对象。<BR>　　参数说明：<BR>　　year-设定日历对象的变量YEAR；month-设定日历对象的变量MONTH；<BR>　　date-设定日历对象的变量DATE；hour-设定日历对象的变量HOUR_OF_DAY；<BR>　　minute-设定日历对象的变量MINUTE；second-设定日历对象的变量SECOND。<BR>　　与Date类中不同的是year的值没有1900这个下限，而且year的值代表实际的年份。month的含义与Date类相同，0代表1月，11代表12月。<BR>　　例&nbsp;GregorianCalendar&nbsp;cal=new&nbsp;GregorianCalendar(1991,2,4)<BR>　　cal的日期为1991年3月4号。<BR>　　除了与Date中类似的方法外，Calendar类还提供了有关方法对日历进行滚动计算和数学计算。计算规则由给定的日历系统决定。进行日期计算时，有时会遇到信息不足或信息不实等特殊情况。Calendar采取了相应的方法解决这些问题。当信息不足时将采用缺省设置，在GregorianCalendar类中缺省设置一般为YEAR=1970,MONTH=JANUARY,DATE=1。<BR>　　当信息不实时，Calendar将按下面的次序优先选择相应的Calendar的变量组合，并将其它有冲突的信息丢弃。<BR>　　MONTH+DAY_OF_MONTH<BR>　　MONTH+WEEK_OF_MONTH+DAY_OF_WEEK<BR>　　MONTH+DAY_OF_WEEK_OF_MONTH+DAY_OF_WEEK<BR>　　DAY_OF+YEAR<BR>　　DAY_OF_WEEK_WEEK_OF_YEAR<BR>　　HOUR_OF_DAY<BR><BR>1.4&nbsp;随机数类Random<BR><BR>　　Java实用工具类库中的类java.util.Random提供了产生各种类型随机数的方法。它可以产生int、long、float、double以及Goussian等类型的随机数。这也是它与java.lang.Math中的方法Random()最大的不同之处，后者只产生double型的随机数。<BR>　　类Random中的方法十分简单，它只有两个构造方法和六个普通方法。<BR>　　构造方法：<BR>　　(1)public&nbsp;Random()<BR>　　(2)public&nbsp;Random(long&nbsp;seed)<BR>　　Java产生随机数需要有一个基值seed，在第一种方法中基值缺省，则将系统时间作为seed。<BR>　　普通方法：<BR>　　(1)public&nbsp;synonronized&nbsp;void&nbsp;setSeed(long&nbsp;seed)<BR>　　该方法是设定基值seed。<BR>　　(2)public&nbsp;int&nbsp;nextInt()<BR>　　该方法是产生一个整型随机数。<BR>　　(3)public&nbsp;long&nbsp;nextLong()<BR>　　该方法是产生一个long型随机数。<BR>　　(4)public&nbsp;float&nbsp;nextFloat()<BR>　　该方法是产生一个Float型随机数。<BR>　　(5)public&nbsp;double&nbsp;nextDouble()<BR>　　该方法是产生一个Double型随机数。<BR>　　(6)public&nbsp;synchronized&nbsp;double&nbsp;nextGoussian()<BR>　　该方法是产生一个double型的Goussian随机数。<BR>　　例1.2&nbsp;RandomApp.java。<BR>　　//import&nbsp;java.lang.*;<BR>　　import&nbsp;java.util.Random;<BR><BR>　　public&nbsp;class&nbsp;RandomApp{<BR>　　　public&nbsp;static&nbsp;void&nbsp;main(String&nbsp;args[]){<BR>　　　　Random&nbsp;ran1=new&nbsp;Random();<BR>　　　　Random&nbsp;ran2=new&nbsp;Random(12345);<BR>　　　　//创建了两个类Random的对象。<BR>　　　　System.out.println("The&nbsp;1st&nbsp;set&nbsp;of&nbsp;random&nbsp;numbers:");<BR>　　　　System.out.println("\t&nbsp;Integer:"+ran1.nextInt());<BR>　　　　System.out.println("\t&nbsp;Long:"+ran1.nextLong());<BR>　　　　System.out.println("\t&nbsp;Float:"+ran1.nextFloat());<BR>　　　　System.out.println("\t&nbsp;Double:"+ran1.nextDouble());<BR>　　　　System.out.println("\t&nbsp;Gaussian:"+ran1.nextGaussian());<BR>　　　　//产生各种类型的随机数<BR>　　　　System.out.print("The&nbsp;2nd&nbsp;set&nbsp;of&nbsp;random&nbsp;numbers:");<BR>　　　　for(int&nbsp;i=0;i&lt;5;i++){<BR>　　　　　System.out.println(ran2.nextInt()+"&nbsp;");<BR>　　　　　if(i==2)&nbsp;System.out.println();<BR>　　　　　//产生同种类型的不同的随机数。<BR>　　　　　System.out.println();//原文如此<BR>　　　　}<BR>　　　}<BR>　　}<BR><BR>　　运行结果：<BR>　　E:\java01&gt;java&nbsp;RandomApp<BR>　　The&nbsp;1st&nbsp;set&nbsp;of&nbsp;random&nbsp;numbers:<BR>　　　　Integer:-173899656<BR>　　　　Long:8056223819738127077<BR>　　　　Float:0.6293638<BR>　　　　Double:0.7888394520265607<BR>　　　　Gaussian:0.5015701094568733<BR>　　The&nbsp;2nd&nbsp;set&nbsp;of&nbsp;random&nbsp;numbers:1553932502<BR>　　-2090749135<BR>　　-287790814<BR>　　-355989640<BR>　　-716867186<BR>　　E:\java01&gt;<BR><BR>1.5&nbsp;向量类Vector<BR><BR>　　Java.util.Vector提供了向量(Vector)类以实现类似动态数组的功能。在Java语言中。正如在一开始就提到过，是没有指针概念的，但如果能正确灵活地使用指针又确实可以大大提高程序的质量，比如在C、C++中所谓“动态数组”一般都由指针来实现。为了弥补这点缺陷，Java提供了丰富的类库来方便编程者使用，Vector类便是其中之一。事实上，灵活使用数组也可完成向量类的功能，但向量类中提供的大量方法大大方便了用户的使用。<BR>　　创建了一个向量类的对象后，可以往其中随意地插入不同的类的对象，既不需顾及类型也不需预先选定向量的容量，并可方便地进行查找。对于预先不知或不愿预先定义数组大小，并需频繁进行查找、插入和删除工作的情况，可以考虑使用向量类。<BR>　　向量类提供了三种构造方法：<BR>　　public&nbsp;vector()<BR>　　public&nbsp;vector(int&nbsp;initialcapacity,int&nbsp;capacityIncrement)<BR>　　public&nbsp;vector(int&nbsp;initialcapacity)<BR>　　使用第一种方法，系统会自动对向量对象进行管理。若使用后两种方法，则系统将根据参数initialcapacity设定向量对象的容量(即向量对象可存储数据的大小)，当真正存放的数据个数超过容量时，系统会扩充向量对象的存储容量。参数capacityIncrement给定了每次扩充的扩充值。当capacityIncrement为0时，则每次扩充一倍。利用这个功能可以优化存储。<BR>　　在Vector类中提供了各种方法方便用户使用：<BR>　　■插入功能<BR>　　(1)public&nbsp;final&nbsp;synchronized&nbsp;void&nbsp;addElement(Object&nbsp;obj)<BR>　　将obj插入向量的尾部。obj可以是任何类的对象。对同一个向量对象，可在其中插入不同类的对象。但插入的应是对象而不是数值，所以插入数值时要注意将数值转换成相应的对象。<BR>　　例&nbsp;要插入一个整数1时，不要直接调用v1.addElement(1)，正确的方法为：<BR>　　Vector&nbsp;v1=new&nbsp;Vector();<BR>　　Integer&nbsp;integer1=new&nbsp;Integer(1);<BR>　　v1.addElement(integer1);<BR>　　(2)public&nbsp;final&nbsp;synchronized&nbsp;void&nbsp;setElementAt(object&nbsp;obj,int&nbsp;index)<BR>　　将index处的对象设成obj，原来的对象将被覆盖。<BR>　　(3)public&nbsp;final&nbsp;synchronized&nbsp;void&nbsp;insertElementAt(Object&nbsp;obj,int&nbsp;index)<BR>　　在index指定的位置插入obj，原来对象以及此后的对象依次往后顺延。<BR>　　■删除功能<BR>　　(1)public&nbsp;final&nbsp;synchronized&nbsp;void&nbsp;removeElement(Object&nbsp;obj)<BR>　　从向量中删除obj。若有多个存在，则从向量头开始试，删除找到的第一个与obj相同的向量成员。<BR>　　(2)public&nbsp;final&nbsp;synchronized&nbsp;void&nbsp;removeAllElement()<BR>　　删除向量中所有的对象。<BR>　　(3)public&nbsp;final&nbsp;synchronized&nbsp;void&nbsp;removeElementlAt(int&nbsp;index)<BR>　　删除index所指的地方的对象。<BR>　　■查询搜索功能<BR>　　(1)public&nbsp;final&nbsp;int&nbsp;indexOf(Object&nbsp;obj)<BR>　　从向量头开始搜索obj&nbsp;,返回所遇到的第一个obj对应的下标，若不存在此obj，返回-1。<BR>　　(2)public&nbsp;final&nbsp;synchronized&nbsp;int&nbsp;indexOf(Object&nbsp;obj,int&nbsp;index)<BR>　　从index所表示的下标处开始搜索obj。<BR>　　(3)public&nbsp;final&nbsp;int&nbsp;lastIndexOf(Object&nbsp;obj)<BR>　　从向量尾部开始逆向搜索obj。<BR>　　(4)public&nbsp;final&nbsp;synchronized&nbsp;int&nbsp;lastIndexOf(Object&nbsp;obj,int&nbsp;index)<BR>　　从index所表示的下标处由尾至头逆向搜索obj。<BR>　　(5)public&nbsp;final&nbsp;synchronized&nbsp;Object&nbsp;firstElement()<BR>　　获取向量对象中的首个obj。<BR>　　(6)public&nbsp;final&nbsp;synchronized&nbsp;Object&nbsp;lastelement()<BR>　　获取向量对象中的最后一个obj。<BR>　　了解了向量的最基本的方法后，我们来看一下例8.3VectorApp.java。<BR>　　例1.3&nbsp;VectorApp.java。<BR>　　import&nbsp;java.util.Vector;<BR>　　import&nbsp;java.lang.*;//这一句不应该要，但原文如此<BR>　　import&nbsp;java.util.Enumeration;<BR>　　public&nbsp;class&nbsp;VectorApp{<BR>　　　public&nbsp;static&nbsp;void&nbsp;main(String[]&nbsp;args){<BR>　　　　Vector&nbsp;v1=new&nbsp;Vector();<BR>　　　　Integer&nbsp;integer1=new&nbsp;Integer(1);<BR>　　　　v1.addElement("one");<BR>　　　　//加入的为字符串对象<BR>　　　　v1.addElement(integer1);<BR>　　　　v1.addElement(integer1);<BR>　　　　//加入的为Integer的对象<BR>　　　　v1.addElement("two");<BR>　　　　v1.addElement(new&nbsp;Integer(2));<BR>　　　　v1.addElement(integer1);<BR>　　　　v1.addElement(integer1);<BR>　　　　System.out.println("The&nbsp;vector&nbsp;v1&nbsp;is:\n\t"+v1);<BR>　　　　//将v1转换成字符串并打印<BR>　　　　v1.insertElementAt("three",2);<BR>　　　　v1.insertElementAt(new&nbsp;Float(3.9),3);<BR>　　　　System.out.println("The&nbsp;vector&nbsp;v1(used&nbsp;method&nbsp;insertElementAt())&nbsp;is:\n\t&nbsp;"+v1);<BR>　　　　//往指定位置插入新的对象，指定位置后的对象依次往后顺延<BR>　　　　v1.setElementAt("four",2);<BR>　　　　System.out.println("The&nbsp;vector&nbsp;v1(used&nbsp;method&nbsp;setElementAt())&nbsp;is:\n\t&nbsp;"+v1);<BR>　　　　//将指定位置的对象设置为新的对象<BR>　　　　v1.removeElement(integer1);<BR>　　　　//从向量对象v1中删除对象integer1由于存在多个integer1所以从头开始<BR>　　　　//找，删除找到的第一个integer1<BR>　　　　Enumeration&nbsp;enum=v1.elements();<BR>　　　　System.out.print("The&nbsp;vector&nbsp;v1(used&nbsp;method&nbsp;removeElement())is:");<BR>　　　　while(enum.hasMoreElements())<BR>　　　　System.out.print(enum.nextElement()+"&nbsp;");<BR>　　　　System.out.println();<BR>　　　　//使用枚举类(Enumeration)的方法来获取向量对象的每个元素<BR>　　　　System.out.println("The&nbsp;position&nbsp;of&nbsp;object&nbsp;1(top-to-bottom):"<BR>　　　　　+&nbsp;v1.indexOf(integer1));<BR>　　　　System.out.println("The&nbsp;position&nbsp;of&nbsp;object&nbsp;1(tottom-to-top):"<BR>　　　　　+v1.lastIndexOf(integer1));<BR>　　　　//按不同的方向查找对象integer1所处的位置<BR>　　　　v1.setSize(4);<BR>　　　　System.out.println("The&nbsp;new&nbsp;vector(resized&nbsp;the&nbsp;vector)is:"+v1);<BR>　　　　//重新设置v1的大小，多余的元素被行弃<BR>　　　}<BR>　　}<BR>　　运行结果：<BR>　　E:\java01&gt;java&nbsp;VectorApp<BR>　　The&nbsp;vector&nbsp;v1&nbsp;is:<BR>　　　　　[one,&nbsp;1,&nbsp;1,&nbsp;two,&nbsp;2,&nbsp;1,&nbsp;1]<BR>　　The&nbsp;vector&nbsp;v1(used&nbsp;method&nbsp;insertElementAt())&nbsp;is:<BR>　　　　　[one,&nbsp;1,&nbsp;three,&nbsp;3.9,&nbsp;1,&nbsp;two,&nbsp;2,&nbsp;1,&nbsp;1]<BR>　　The&nbsp;vector&nbsp;v1(used&nbsp;method&nbsp;setElementAt())&nbsp;is:<BR>　　　　　[one,&nbsp;1,&nbsp;four,&nbsp;3.9,&nbsp;1,&nbsp;two,&nbsp;2,&nbsp;1,&nbsp;1]<BR>　　The&nbsp;vector&nbsp;v1(used&nbsp;method&nbsp;removeElement())is:one&nbsp;four&nbsp;3.9&nbsp;1&nbsp;two&nbsp;2&nbsp;1&nbsp;1<BR>　　The&nbsp;position&nbsp;of&nbsp;object&nbsp;1(top-to-bottom):3<BR>　　The&nbsp;position&nbsp;of&nbsp;object&nbsp;1(tottom-to-top):7<BR>　　The&nbsp;new&nbsp;vector(resized&nbsp;the&nbsp;vector)is:[one,&nbsp;four,&nbsp;3.9,&nbsp;1]<BR>　　E:\java01&gt;<BR>　　从例1.3运行的结果中可以清楚地了解上面各种方法的作用，另外还有几点需解释。<BR>　　(1)类Vector定义了方法<BR>　　public&nbsp;final&nbsp;int&nbsp;size()<BR>　　此方法用于获取向量元素的个数。它的返回值是向是中实际存在的元素个数，而非向量容量。可以调用方法capactly()来获取容量值。<BR>　　方法：<BR>　　public&nbsp;final&nbsp;synchronized&nbsp;void&nbsp;setsize(int&nbsp;newsize)<BR>　　此方法用来定义向量大小。若向量对象现有成员个数已超过了newsize的值，则超过部分的多余元素会丢失。<BR>　　(2)程序中定义了Enumeration类的一个对象<BR>　　Enumeration是java.util中的一个接口类，在Enumeration中封装了有关枚举数据集合的方法。<BR>　　在Enumeration中提供了方法hawMoreElement()来判断集合中是束还有其它元素和方法nextElement()来获取下一个元素。利用这两个方法可以依次获得集合中元素。<BR>　　Vector中提供方法：<BR>　　public&nbsp;final&nbsp;synchronized&nbsp;Enumeration&nbsp;elements()<BR>　　此方法将向量对象对应到一个枚举类型。java.util包中的其它类中也大都有这类方法，以便于用户获取对应的枚举类型。<BR><BR>1.6&nbsp;栈类Stack<BR><BR>　　Stack类是Vector类的子类。它向用户提供了堆栈这种高级的数据结构。栈的基本特性就是先进后出。即先放入栈中的元素将后被推出。Stack类中提供了相应方法完成栈的有关操作。<BR>　　基本方法：<BR>　　public&nbsp;Object&nbsp;push(Object&nbsp;Hem)<BR>　　将Hem压入栈中，Hem可以是任何类的对象。<BR>　　public&nbsp;Object&nbsp;pop()<BR>　　弹出一个对象。<BR>　　public&nbsp;Object&nbsp;peek()<BR>　　返回栈顶元素，但不弹出此元素。<BR>　　public&nbsp;int&nbsp;search(Object&nbsp;obj)<BR>　　搜索对象obj,返回它所处的位置。<BR>　　public&nbsp;boolean&nbsp;empty()<BR>　　判别栈是否为空。<BR>　　例1.4&nbsp;StackApp.java使用了上面的各种方法。<BR>　　例1.4&nbsp;StackApp.java。<BR>　　import&nbsp;java.lang.*;<BR>　　import&nbsp;java.util.*;<BR>　　public&nbsp;class&nbsp;StackApp{<BR>　　　public&nbsp;static&nbsp;void&nbsp;main(String&nbsp;args[]){<BR>　　　　Stack&nbsp;sta=new&nbsp;Stack();<BR>　　　　sta.push("Apple");<BR>　　　　sta.push("banana");<BR>　　　　sta.push("Cherry");<BR>　　　　//压入的为字符串对象<BR>　　　　sta.push(new&nbsp;Integer(2));<BR>　　　　//压入的为Integer的对象，值为2<BR>　　　　sta.push(new&nbsp;Float(3.5));<BR>　　　　//压入的为Float的对象，值为3.5<BR>　　　　System.out.println("The&nbsp;stack&nbsp;is,"+sta);<BR>　　　　//对应栈sta<BR>　　　　System.out.println("The&nbsp;top&nbsp;of&nbsp;stack&nbsp;is:"+sta.peek());<BR>　　　　//对应栈顶元素，但不将此元素弹出<BR>　　　　System.out.println("The&nbsp;position&nbsp;of&nbsp;object&nbsp;Cherry&nbsp;is:"<BR>　　　　+sta.search("cherry"));<BR>　　　　//打印对象Cherry所处的位置<BR>　　　　System.out.print("Pop&nbsp;the&nbsp;element&nbsp;of&nbsp;the&nbsp;stack:");<BR>　　　　while(!sta.empty())<BR>　　　　System.out.print(sta.pop()+"&nbsp;");<BR>　　　　System.out.println();<BR>　　　　//将栈中的元素依次弹出并打印。与第一次打印的sta的结果比较，可看出栈<BR>　　　　//先进后出的特点<BR>　　　}<BR>　　}<BR>　　运行结果(略)<BR><BR><BR>1.7&nbsp;哈希表类Hashtable<BR><BR>　　哈希表是一种重要的存储方式，也是一种常见的检索方法。其基本思想是将关系码的值作为自变量，通过一定的函数关系计算出对应的函数值，把这个数值解释为结点的存储地址，将结点存入计算得到存储地址所对应的存储单元。检索时采用检索关键码的方法。现在哈希表有一套完整的算法来进行插入、删除和解决冲突。在Java中哈希表用于存储对象，实现快速检索。<BR>　　Java.util.Hashtable提供了种方法让用户使用哈希表，而不需要考虑其哈希表真正如何工作。<BR>　　哈希表类中提供了三种构造方法，分别是：<BR>　　public&nbsp;Hashtable()<BR>　　public&nbsp;Hashtable(int&nbsp;initialcapacity)<BR>　　public&nbsp;Hashtable(int&nbsp;initialCapacity,float&nbsp;loadFactor)<BR>　　参数initialCapacity是Hashtable的初始容量，它的值应大于0。loadFactor又称装载因子，是一个0.0到0.1之间的float型的浮点数。它是一个百分比，表明了哈希表何时需要扩充，例如，有一哈希表，容量为100，而装载因子为0.9，那么当哈希表90%的容量已被使用时，此哈希表会自动扩充成一个更大的哈希表。如果用户不赋这些参数，系统会自动进行处理，而不需要用户操心。<BR>　　Hashtable提供了基本的插入、检索等方法。<BR>　　■插入<BR>　　public&nbsp;synchronized&nbsp;void&nbsp;put(Object&nbsp;key,Object&nbsp;value)<BR>给对象value设定一关键字key,并将其加到Hashtable中。若此关键字已经存在，则将此关键字对应的旧对象更新为新的对象Value。这表明在哈希表中相同的关键字不可能对应不同的对象(从哈希表的基本思想来看，这也是显而易见的)。<BR>　　■检索<BR>　　public&nbsp;synchronized&nbsp;Object&nbsp;get(Object&nbsp;key)<BR>　　根据给定关键字key获取相对应的对象。<BR>　　public&nbsp;synchronized&nbsp;boolean&nbsp;containsKey(Object&nbsp;key)<BR>　　判断哈希表中是否包含关键字key。<BR>　　public&nbsp;synchronized&nbsp;boolean&nbsp;contains(Object&nbsp;value)<BR>　　判断value是否是哈希表中的一个元素。<BR>　　■删除<BR>　　public&nbsp;synchronized&nbsp;object&nbsp;remove(object&nbsp;key)<BR>　　从哈希表中删除关键字key所对应的对象。<BR>　　public&nbsp;synchronized&nbsp;void&nbsp;clear()<BR>　　清除哈希表<BR>　　另外，Hashtalbe还提供方法获取相对应的枚举集合：<BR>　　public&nbsp;synchronized&nbsp;Enumeration&nbsp;keys()<BR>　　返回关键字对应的枚举对象。<BR>　　public&nbsp;synchronized&nbsp;Enumeration&nbsp;elements()<BR>　　返回元素对应的枚举对象。<BR>　　例1.5&nbsp;Hashtable.java给出了使用Hashtable的例子。<BR>　　例1.5&nbsp;Hashtalbe.java。<BR>　　//import&nbsp;java.lang.*;<BR>　　import&nbsp;java.util.Hashtable;<BR>　　import&nbsp;java.util.Enumeration;<BR>　　public&nbsp;class&nbsp;HashApp{<BR>　　　public&nbsp;static&nbsp;void&nbsp;main(String&nbsp;args[]){<BR>　　　　Hashtable&nbsp;hash=new&nbsp;Hashtable(2,(float)0.8);<BR>　　　　//创建了一个哈希表的对象hash，初始容量为2，装载因子为0.8<BR><BR>　　　　hash.put("Jiangsu","Nanjing");<BR>　　　　//将字符串对象“Jiangsu”给定一关键字“Nanjing”,并将它加入hash<BR>　　　　hash.put("Beijing","Beijing");<BR>　　　　hash.put("Zhejiang","Hangzhou");<BR><BR>　　　　System.out.println("The&nbsp;hashtable&nbsp;hash1&nbsp;is:&nbsp;"+hash);<BR>　　　　System.out.println("The&nbsp;size&nbsp;of&nbsp;this&nbsp;hash&nbsp;table&nbsp;is&nbsp;"+hash.size());<BR>　　　　//打印hash的内容和大小<BR><BR>　　　　Enumeration&nbsp;enum1=hash.elements();<BR>　　　　System.out.print("The&nbsp;element&nbsp;of&nbsp;hash&nbsp;is:&nbsp;");<BR>　　　　while(enum1.hasMoreElements())<BR>　　　　　System.out.print(enum1.nextElement()+"&nbsp;");<BR>　　　　System.out.println();<BR>　　　　//依次打印hash中的内容<BR>　　　　if(hash.containsKey("Jiangsu"))<BR>　　　　　System.out.println("The&nbsp;capatial&nbsp;of&nbsp;Jiangsu&nbsp;is&nbsp;"+hash.get("Jiangsu"));<BR>　　　　hash.remove("Beijing");<BR>　　　　//删除关键字Beijing对应对象<BR>　　　　System.out.println("The&nbsp;hashtable&nbsp;hash2&nbsp;is:&nbsp;"+hash);<BR>　　　　System.out.println("The&nbsp;size&nbsp;of&nbsp;this&nbsp;hash&nbsp;table&nbsp;is&nbsp;"+hash.size());<BR>　　　}<BR>　　}<BR><BR>　　运行结果：<BR>　　The&nbsp;hashtable&nbsp;hash1&nbsp;is:&nbsp;{Beijing=Beijing,&nbsp;Zhejiang=Hangzhou,&nbsp;Jiangsu=Nanjing}<BR>　　The&nbsp;size&nbsp;of&nbsp;this&nbsp;hash&nbsp;table&nbsp;is&nbsp;3<BR>　　The&nbsp;element&nbsp;of&nbsp;hash&nbsp;is:&nbsp;Beijing&nbsp;Hangzhou&nbsp;Nanjing<BR>　　The&nbsp;capatial&nbsp;of&nbsp;Jiangsu&nbsp;is&nbsp;Nanjing<BR>　　The&nbsp;hashtable&nbsp;hash2&nbsp;is:&nbsp;{Zhejiang=Hangzhou,&nbsp;Jiangsu=Nanjing}<BR>　　The&nbsp;size&nbsp;of&nbsp;this&nbsp;hash&nbsp;table&nbsp;is&nbsp;2<BR><BR>　　Hashtable是Dictionary(字典)类的子类。在字典类中就把关键字对应到数据值。字典类是一个抽象类。在java.util中还有一个类Properties，它是Hashtable的子类。用它可以进行与对象属性相关的操作。<BR><BR>1.8&nbsp;位集合类BitSet<BR><BR>　　位集合类中封装了有关一组二进制数据的操作。<BR>　　我们先来看一下例8.6&nbsp;BitSetApp.java。<BR>　　例8.6&nbsp;BitSetApp.java<BR>　　//import&nbsp;java.lang.*;<BR>　　import&nbsp;java.util.BitSet;<BR>　　public&nbsp;class&nbsp;BitSetApp{<BR>　　　private&nbsp;static&nbsp;int&nbsp;n=5;<BR>　　　public&nbsp;static&nbsp;void&nbsp;main(String[]&nbsp;args){<BR>　　　　BitSet&nbsp;set1=new&nbsp;BitSet(n);<BR>　　　　for(int&nbsp;i=0;i&lt;n;i++)&nbsp;set1.set(i);<BR>　　　　//将set1的各位赋1，即各位均为true<BR>　　　　BitSet&nbsp;set2=&nbsp;new&nbsp;BitSet();<BR>　　　　set2=(BitSet)set1.clone();<BR>　　　　//set2为set1的拷贝<BR>　　　　set1.clear(0);<BR>　　　　set2.clear(2);<BR>　　　　//将set1的第0位set2的第2位清零<BR>　　　　System.out.println("The&nbsp;set1&nbsp;is:&nbsp;"+set1);<BR>　　　　//直接将set1转换成字符串输出，输出的内容是set1中值true所处的位置<BR>　　　　//打印结果为The&nbsp;set1&nbsp;is:{1,2,3,4}<BR>　　　　System.out.println("The&nbsp;hash&nbsp;code&nbsp;of&nbsp;set2&nbsp;is:&nbsp;"+set2.hashCode());<BR>　　　　//打印set2的hashCode<BR>　　　　printbit("set1",set1);<BR>　　　　printbit("set2",set2);<BR>　　　　//调用打印程序printbit(),打印对象中的每一个元素<BR>　　　　//打印set1的结果为The&nbsp;bit&nbsp;set1&nbsp;is:&nbsp;false&nbsp;true&nbsp;true&nbsp;true&nbsp;true<BR>　　　　set1.and(set2);<BR>　　　　printbit("set1&nbsp;and&nbsp;set2",set1);<BR>　　　　//完成set1&nbsp;and&nbsp;set2,并打印结果<BR>　　　　set1.or(set2);<BR>　　　　printbit("set1&nbsp;or&nbsp;set2",set1);<BR>　　　　//完成set1&nbsp;or&nbsp;set2,并打印结果<BR>　　　　set1.xor(set2);<BR>　　　　printbit("set1&nbsp;xor&nbsp;set2",set1);<BR>　　　　//完成set1&nbsp;xor&nbsp;set2,并打印结果<BR>　　　}<BR>　　　//打印BitSet对象中的内容<BR>　　　public&nbsp;static&nbsp;void&nbsp;printbit(String&nbsp;name,BitSet&nbsp;set){<BR>　　　　System.out.print("The&nbsp;bit&nbsp;"+name+"&nbsp;is:&nbsp;");<BR>　　　　for(int&nbsp;i=0;i&lt;n;i++)<BR>　　　　　System.out.print(set.get(i)+"&nbsp;");<BR>　　　　System.out.println();<BR>　　　}<BR>　　}<BR><BR>　　运行结果：<BR>　　The&nbsp;set1&nbsp;is:&nbsp;{1,&nbsp;2,&nbsp;3,&nbsp;4}<BR>　　The&nbsp;hash&nbsp;code&nbsp;of&nbsp;set2&nbsp;is:&nbsp;1225<BR>　　The&nbsp;bit&nbsp;set1&nbsp;is:&nbsp;false&nbsp;true&nbsp;true&nbsp;true&nbsp;true<BR>　　The&nbsp;bit&nbsp;set2&nbsp;is:&nbsp;true&nbsp;true&nbsp;false&nbsp;true&nbsp;true<BR>　　The&nbsp;bit&nbsp;set1&nbsp;and&nbsp;set2&nbsp;is:&nbsp;false&nbsp;true&nbsp;false&nbsp;true&nbsp;true<BR>　　The&nbsp;bit&nbsp;set1&nbsp;or&nbsp;set2&nbsp;is:&nbsp;true&nbsp;true&nbsp;false&nbsp;true&nbsp;true<BR>　　The&nbsp;bit&nbsp;set1&nbsp;xor&nbsp;set2&nbsp;is:&nbsp;false&nbsp;false&nbsp;false&nbsp;false&nbsp;false<BR><BR>　　程序中使用了BitSet类提供的两种构造方法：<BR>　　　　public&nbsp;BitSet();<BR>　　　　public&nbsp;BitSet(int&nbsp;n);<BR>　　参数n代表所创建的BitSet类的对象的大小。BitSet类的对象的大小在必要时会由系统自动扩充。<BR>　　其它方法：<BR>　　public&nbsp;void&nbsp;set(int&nbsp;n)<BR>　　将BitSet对象的第n位设置成1。<BR>　　public&nbsp;void&nbsp;clear(int&nbsp;n)<BR>　　将BitSet对象的第n位清零。<BR>　　public&nbsp;boolean&nbsp;get(int&nbsp;n)<BR>　　读取位集合对象的第n位的值，它获取的是一个布尔值。当第n位为1时，返回true；第n位为0时，返回false。<BR>　　另外，如在程序中所示，当把一BitSet类的对象转换成字符串输出时，输出的内容是此对象中true所处的位置。<BR>　　在BitSet中提供了一组位操作，分别是：<BR>　　public&nbsp;void&nbsp;and(BitSet&nbsp;set)<BR>　　public&nbsp;void&nbsp;or(BitSet&nbsp;set)<BR>　　public&nbsp;void&nbsp;xor(BitSet&nbsp;set)<BR>利用它们可以完成两个位集合之间的与、或、异或操作。<BR>　　BitSet类中有一方法public&nbsp;int&nbsp;size()来取得位集合的大小，它的返回值与初始化时设定的位集合大小n不一样，一般为64。<BR><BR>小结<BR><BR>　　本章我们介绍了Java的实用工具类库java.util中一些常用的类。java.util包中还有其它一些类。它们的具体用法用户可以自行查阅API。&nbsp;<BR><img src ="http://www.blogjava.net/hmily1211/aggbug/21318.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/hmily1211/" target="_blank">活在JAVA岛的日子</a> 2005-11-24 17:55 <a href="http://www.blogjava.net/hmily1211/archive/2005/11/24/21318.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>ARRAYS</title><link>http://www.blogjava.net/hmily1211/archive/2005/11/23/21153.html</link><dc:creator>活在JAVA岛的日子</dc:creator><author>活在JAVA岛的日子</author><pubDate>Wed, 23 Nov 2005 09:22:00 GMT</pubDate><guid>http://www.blogjava.net/hmily1211/archive/2005/11/23/21153.html</guid><wfw:comment>http://www.blogjava.net/hmily1211/comments/21153.html</wfw:comment><comments>http://www.blogjava.net/hmily1211/archive/2005/11/23/21153.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/hmily1211/comments/commentRss/21153.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/hmily1211/services/trackbacks/21153.html</trackback:ping><description><![CDATA[<P>&nbsp;&nbsp;&nbsp;&nbsp; package untitled4;<BR>import java.util.*;<BR>class compare implements Comparator<BR>{<BR>&nbsp;&nbsp;&nbsp; public int compare(Object o1,Object o2)<BR>&nbsp;&nbsp;&nbsp; {&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int i=((Person)o1).getFirstName().compareTo(((Person)o2).getFirstName());<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return (i!=0?i:((Person)o1).getLastName().compareTo(((Person)o2).getLastName()));<BR>&nbsp;&nbsp;&nbsp; }<BR>}<BR>class Person implements Comparable<BR>{&nbsp;&nbsp; private String firstName;<BR>&nbsp;&nbsp;&nbsp; private String lastName;<BR>&nbsp;&nbsp;&nbsp; public String getFirstName()<BR>&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return firstName;<BR>&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp; public String getLastName()<BR>&nbsp;&nbsp;&nbsp; {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return lastName;<BR>&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp; public Person(String f,String l)<BR>&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; firstName=f;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lastName=l;<BR>&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp; public int compareTo(Object o)<BR>&nbsp;&nbsp;&nbsp; {&nbsp; Person p=(Person)o;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int l=firstName.compareTo(p.firstName);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return (l!=0?l:(lastName.compareTo(p.lastName)));<BR>&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp; public String toString()<BR>&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return firstName+" "+lastName;<BR>&nbsp;&nbsp;&nbsp; }</P>
<P><BR>}<BR>public class ArrayTest {<BR>&nbsp;&nbsp;&nbsp; public ArrayTest() {<BR>&nbsp;&nbsp;&nbsp; }<BR>public static void main(String args[])<BR>&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int a[];<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; a = new int[5];<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Arrays.fill(a, 5);&nbsp; //用5填充数组<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int i = 0; i &lt; a.length; i++) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(a[i]);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int b[] = {10, 3, 5, 6, 8, 9};<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Arrays.sort(b);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int i = 0; i &lt; b.length; i++) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(b[i]);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Person p[] = {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; new Person("John", "Lennon"),<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; new Person("Karl", "Marx"),<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; new Person("Groucho", "Marx"),<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; new Person("Oscar", "Grouch")<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; };<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int i = 0; i &lt; p.length; i++) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(p[i]);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //Arrays.sort(p);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Arrays.sort(p, new compare());<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int i = 0; i &lt; p.length; i++) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(p[i]);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int s = Arrays.binarySearch(p, new Person("Groucho", "Marx"), new compare());<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(s);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Person person[];<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; person = new Person[4];<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.arraycopy(p, 0, person, 0, p.length);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int c= 0; c &lt; person.length; c++) <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(person[c]);<BR>&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp; }<BR>}<BR>当用Arrays的sort方法时,需要排序的数组必须实现Comparable借口.或者实现Comparator接口<BR>Arrays的fill方法是用一个基本类型或者一个对象填充数组.<BR>当调用binarySearch()时如果是调用sort(Objiec a[],Comparator a)时,,应调用相应的binarySearch(Object a[],Object value,Comparator)方法.<BR>equals()方法用于比较非基本类型数组时,调用他们的equals方法..比如<BR>&nbsp;int a[]={5,4,3,2,1};<BR>&nbsp;int c[]={5,4,3,2,1};&nbsp; 基本类型比较他们的值,<BR>&nbsp;System.out.println(Arrays.equals(a,c));&nbsp; TRUE<BR>Integer a[]={new Integer(1),new Integer(2),new Integer(3)};<BR>Integer b[]={new Integer(1),new Integer(2),new Integer(3)};<BR>System.out.println(Arrays.equals(a,b));&nbsp;&nbsp; 调用Integer.equals()方法<BR>对于没有覆盖Object equals方法的对象数组,他们之间比较的是对象的引用.<BR>System.arraycopy()方法对与基本类型来说只是复制他们的值.而对于非基本类型时他们复制引用<BR>&nbsp; person person[]={new person("guo",20),new person("cher",21)};<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; person per[]={new person("guo",20),new person("cher",21)};<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; person []p=new person[2];<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.arraycopy(person,0,p,0,person.length);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(person==p);&nbsp;&nbsp;&nbsp; //输出&nbsp; false<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; person[1].setName("hao");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(person[1].equals(p[1]));&nbsp; 输出 true<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(Arrays.equals(person,p));&nbsp;&nbsp; 输出&nbsp;true<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(Arrays.equals(person,per));&nbsp; 输出 false<BR></P><img src ="http://www.blogjava.net/hmily1211/aggbug/21153.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/hmily1211/" target="_blank">活在JAVA岛的日子</a> 2005-11-23 17:22 <a href="http://www.blogjava.net/hmily1211/archive/2005/11/23/21153.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>对多态的理解</title><link>http://www.blogjava.net/hmily1211/archive/2005/11/16/19985.html</link><dc:creator>活在JAVA岛的日子</dc:creator><author>活在JAVA岛的日子</author><pubDate>Tue, 15 Nov 2005 18:59:00 GMT</pubDate><guid>http://www.blogjava.net/hmily1211/archive/2005/11/16/19985.html</guid><wfw:comment>http://www.blogjava.net/hmily1211/comments/19985.html</wfw:comment><comments>http://www.blogjava.net/hmily1211/archive/2005/11/16/19985.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/hmily1211/comments/commentRss/19985.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/hmily1211/services/trackbacks/19985.html</trackback:ping><description><![CDATA[如果子类继承了父类的方法（未重写），则运行时系统调用父类的方法。</SPAN><BR><SPANCLASS=PT9-BLACK1>在例3-6中，</SPAN></FONT></SPAN><FONTFACE=宋体><SPANCLASS=PT9-BLACK1><SPANSTYLE="FONT-SIZE:9PT;COLOR:RED">父类对象<SPANLANG=EN-US>a引用的是子类的实例</SPAN></SPAN></SPAN><SPANCLASS=PT9-BLACK1><SPANSTYLE="FONT-SIZE:9PT">，所以，<SPANLANG=EN-US>java运行时调用子类B的callme方法。<BR>importjava.io.*;</SPAN><BR><SPANCLASS=PT9-BLACK1>　　classA{</SPAN><BR><SPANCLASS=PT9-BLACK1>　　　　　voidcallme(){</SPAN><BR><SPANCLASS=PT9-BLACK1>　　　　　　System.out.println("InsideA'scallme()method");</SPAN><BR><SPANCLASS=PT9-BLACK1>　　　　　}</SPAN><BR><SPANCLASS=PT9-BLACK1>　　}</SPAN><BR><SPANCLASS=PT9-BLACK1>　　classBextendsA{</SPAN><BR><SPANCLASS=PT9-BLACK1>　　　　　voidcallme(){</SPAN><BR><SPANCLASS=PT9-BLACK1>　　　　　　System.out.println("InsideB'scallme()Method");</SPAN><BR><SPANCLASS=PT9-BLACK1>　　　　　}</SPAN><BR><SPANCLASS=PT9-BLACK1>　　}</SPAN><BR><SPANCLASS=PT9-BLACK1>　　publicclassDispatch{</SPAN><BR><SPANCLASS=PT9-BLACK1>　　　　　publicstaticvoidmain(Stringargs[]){</SPAN><BR><SPANCLASS=PT9-BLACK1>　　　　　　Aa=newB();</SPAN><BR><SPANCLASS=PT9-BLACK1>　　　　　　a.callme();</SPAN><BR><SPANCLASS=PT9-BLACK1>　　　　　}</SPAN><BR><SPANCLASS=PT9-BLACK1>　　}<BR></SPAN></SPAN></SPAN></FONT><SPANLANG=EN-USSTYLE="FONT-SIZE:9PT"><BR><STRONG>◇方法重写时应遵循的原则</STRONG>：</SPAN><BR><SPANCLASS=PT9-BLACK1>　　</SPAN></FONT></SPAN><SPANCLASS=PT9-BLACK1><SPANLANG=EN-USSTYLE="FONT-SIZE:9PT;COLOR:RED"><FONTFACE=宋体>1）改写后的方法不能比被重写的方法有更严格的访问权限（可以相同）。</FONT></SPAN></SPAN><SPANLANG=EN-USSTYLE="FONT-SIZE:9PT;COLOR:RED"><BR><SPANCLASS=PT9-BLACK1><SPANSTYLE="COLOR:RED"><FONTFACE=宋体>　　2）改写后的方法不能比重写的方法产生更多的例外。</SPANSTYLE="COLOR:RED"></SPANLANG=EN-USSTYLE="FONT-SIZE:9PT;COLOR:RED"></SPANLANG=EN-USSTYLE="FONT-SIZE:9PT;COLOR:RED"><BR><?XML:NAMESPACEPREFIX=VNS="URN:SCHEMAS-MICROSOFT-COM:VML" /><?XML:NAMESPACE PREFIX = V /><V:SHAPETYPEID=_X0000_T75STROKED="F"FILLED="F"PATH="M@4@5L@4@11@9@11@9@5XE"O:PREFERRELATIVE="T"O:SPT="75"COORDSIZE="21600,21600"><V:STROKEJOINSTYLE="MITER"></V:STROKEJOINSTYLE="MITER"><V:FORMULAS><V:FEQN="IFLINEDRAWNPIXELLINEWIDTH0"></V:FEQN="IFLINEDRAWNPIXELLINEWIDTH0"><V:FEQN="SUM@010"></V:FEQN="SUM@010"><V:FEQN="SUM00@1"></V:FEQN="SUM00@1"><V:FEQN="PROD@212"></V:FEQN="PROD@212"><V:FEQN="PROD@321600PIXELWIDTH"></V:FEQN="PROD@321600PIXELWIDTH"><V:FEQN="PROD@321600PIXELHEIGHT"></V:FEQN="PROD@321600PIXELHEIGHT"><V:FEQN="SUM@001"></V:FEQN="SUM@001"><V:FEQN="PROD@612"></V:FEQN="PROD@612"><V:FEQN="PROD@721600PIXELWIDTH"></V:FEQN="PROD@721600PIXELWIDTH"><V:FEQN="SUM@8216000"></V:FEQN="SUM@8216000"><V:FEQN="PROD@721600PIXELHEIGHT"></V:FEQN="PROD@721600PIXELHEIGHT"><V:FEQN="SUM@10216000"></V:FEQN="SUM@10216000"></V:FORMULAS><V:PATHO:CONNECTTYPE="RECT"GRADIENTSHAPEOK="T"O:EXTRUSIONOK="F"></V:PATHO:CONNECTTYPE="RECT"GRADIENTSHAPEOK="T"O:EXTRUSIONOK="F"><?XML:NAMESPACE PREFIX = O /><O:LOCKASPECTRATIO="T"V:EXT="EDIT"></O:LOCKASPECTRATIO="T"V:EXT="EDIT"></V:SHAPETYPEID=_X0000_T75STROKED="F"FILLED="F"PATH="M@4@5L@4@11@9@11@9@5XE"O:PREFERRELATIVE="T"O:SPT="75"COORDSIZE="21600,21600"><V:SHAPEID=_X0000_I1025STYLE="WIDTH:28.5PT;HEIGHT:28.5PT"TYPE="#_X0000_T75"ALT=""><V:IMAGEDATASRC="FILE: C: DOCUME~1 ADMINI~1 LOCALS~1 Temp msoclip1 06 clip_image001.gif?o:href="file:///D:/java%20教程/JAVA-qinghua/images/html/liti.gif"><FONTFACE=宋体></FONT></V:IMAGEDATASRC="FILE:></V:SHAPEID=_X0000_I1025STYLE="WIDTH:28.5PT;HEIGHT:28.5PT"TYPE="#_X0000_T75"ALT=""><FONTFACE=宋体><SPANCLASS=PT9-BLACK1></SPANLANG=EN-USSTYLE="FONT-SIZE:9PT"></SPANSTYLE="FONT-SIZE:9PT"></SPANSTYLE="FONT-SIZE:9PT;COLOR:RED"><img src ="http://www.blogjava.net/hmily1211/aggbug/19985.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/hmily1211/" target="_blank">活在JAVA岛的日子</a> 2005-11-16 02:59 <a href="http://www.blogjava.net/hmily1211/archive/2005/11/16/19985.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>多态</title><link>http://www.blogjava.net/hmily1211/archive/2005/11/16/19984.html</link><dc:creator>活在JAVA岛的日子</dc:creator><author>活在JAVA岛的日子</author><pubDate>Tue, 15 Nov 2005 18:44:00 GMT</pubDate><guid>http://www.blogjava.net/hmily1211/archive/2005/11/16/19984.html</guid><wfw:comment>http://www.blogjava.net/hmily1211/comments/19984.html</wfw:comment><comments>http://www.blogjava.net/hmily1211/archive/2005/11/16/19984.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/hmily1211/comments/commentRss/19984.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/hmily1211/services/trackbacks/19984.html</trackback:ping><description><![CDATA[<TABLE cellSpacing=2 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD style="FONT-SIZE: 12pt; COLOR: #ff6600" align=middle height=32>
<DIV align=center>品味Java子类型多态的魅力</DIV></TD></TR>
<TR>
<TD align=middle bgColor=#f7f7f7 height=25>
<DIV align=center>&nbsp;</DIV></TD></TR>
<SCRIPT language=javascript>
var newasp_fontsize=9;
var newasp_lineheight=12;
function do_color(vobject,vvar) {
 document.getElementById(vobject).style.color=vvar; 
}
</SCRIPT>

<TR>
<TD>
<P align=right><A title=减小字体 style="CURSOR: hand; POSITION: relative" onclick='if(newasp_fontsize>8){NewaspContentLabel.style.fontSize=(--newasp_fontsize)+"pt";NewaspContentLabel.style.lineHeight=(--newasp_lineheight)+"pt";}'><IMG height=15 src="http://www.gz235.com/images/1.gif" width=15 border=0><FONT color=#ff6600>减小字体</FONT></A> <A title=增大字体 style="CURSOR: hand; POSITION: relative" onclick='if(newasp_fontsize<64){NewaspContentLabel.style.fontSize=(++newasp_fontsize)+"pt";NewaspContentLabel.style.lineHeight=(++newasp_lineheight)+"pt";}'><IMG height=15 src="http://www.gz235.com/images/2.gif" width=15 border=0><FONT color=#ff6600>增大字体</FONT></A> <SELECT onchange="if(this.options[this.selectedindex].value!=''){do_color('font_word',this.options[this.selectedindex].value);}" size=1 name=do_color_frm> <OPTION value="" selected>颜色</OPTION> <OPTION style="COLOR: #000000" value=#000000>默认</OPTION> <OPTION style="COLOR: #808080" value=#808080>灰度</OPTION> <OPTION style="COLOR: #808000" value=#808000>橄榄色</OPTION> <OPTION style="COLOR: #008000" value=#008000>绿色</OPTION> <OPTION style="COLOR: #0000ff" value=#0000ff>蓝色</OPTION> <OPTION style="COLOR: #800000" value=#800000>褐色</OPTION> <OPTION style="COLOR: #ff0000" value=#ff0000>红色</OPTION></SELECT> </P><BR>
<DIV class=ContentFont id=NewaspContentLabel style="PADDING-RIGHT: 10px; DISPLAY: block; PADDING-LEFT: 10px; PADDING-BOTTOM: 0px; PADDING-TOP: 0px"><FONT id=font_word style="FONT-SIZE: 14px; FONT-FAMILY: 宋体, Verdana, Arial, Helvetica, sans-serif"><FONT id=font_word style="FONT-SIZE: 14px; FONT-FAMILY: 宋体, Verdana, Arial, Helvetica, sans-serif"><FONT id=font_word style="FONT-SIZE: 14px; FONT-FAMILY: 宋体, Verdana, Arial, Helvetica, sans-serif"></FONT></FONT></FONT></DIV></TD>
<TR>
<TD vAlign=top width="100%">
<P><FONT color=#a52a2a>　　“polymorphism(多态）”一词来自希腊语，意为“多种形式”。多数Java程序员把多态看作对象的一种能力，使其能调用正确的方法版本。尽管如此，这种面向实现的观点导致了多态的神奇功能，胜于仅仅把多态看成纯粹的概念。<BR><BR>　　Java中的多态总是子类型的多态。几乎是机械式产生了一些多态的行为，使我们不去考虑其中涉及的类型问题。本文研究了一种面向类型的对象观点，分析了如何将对象能够表现的行为和对象即将表现的行为分离开来。抛开Java中的多态都是来自继承的概念，我们仍然可以感到，Java中的接口是一组没有公共代码的对象共享实现。<BR><BR>　　<B>多态的分类</B> <BR><BR>　　多态在面向对象语言中是个很普遍的概念.虽然我们经常把多态混为一谈，但实际上有四种不同类型的多态。在开始正式的子类型多态的细节讨论前，然我们先来看看普通面向对象中的多态。<BR><BR>　　Luca Cardelli和Peter Wegner（"On Understanding Types, Data Abstraction, and Polymorphism"一文的作者， 文章参考资源链接）把多态分为两大类----特定的和通用的----四小类：强制的，重载的，参数的和包含的。他们的结构如下：<BR><BR><IMG onmousewheel="return bbimg(this)" style="CURSOR: pointer" src="http://www.gz235.com/Article/UploadPic/2005-10/2005102710031619.gif" onload="javascript:if(this.width onclick=" align=center vspace=1 border=1 javascript:window.open(this.src);?>screen.width-600)this.style.width=screen.width-600;"&gt;<BR><BR>　　在这样一个体系中，多态表现出多种形式的能力。通用多态引用有相同结构类型的大量对象，他们有着共同的特征。特定的多态涉及的是小部分没有相同特征的对象。四种多态可做以下描述：<BR><BR>　　强制的：一种隐式做类型转换的方法。<BR><BR>　　重载的：将一个标志符用作多个意义。<BR><BR>　　参数的：为不同类型的参数提供相同的操作。<BR><BR>　　包含的：类包含关系的抽象操作。<BR><BR>　　我将在讲述子类型多态前简单介绍一下这几种多态。<BR><BR>　　<B>强制的多态</B><BR><BR>　　强制多态隐式的将参数按某种方法，转换成编译器认为正确的类型以避免错误。在以下的表达式中，编译器必须决定二元运算符‘+’所应做的工作：<BR><BR>　　2.0 + 2.0<BR><BR>　　2.0 + 2<BR><BR>　　2.0 + "2"<BR><BR>　　第一个表达式将两个double的操作数相加；Java中特别声明了这种用法。<BR><BR>　　第二个表达式将double型和int相加。Java中没有明确定义这种运算。不过，编译器隐式的将第二个操作数转换为double型，并作double型的加法。做对程序员来说十分方便，否则将会抛出一个编译错误，或者强制程序员显式的将int转换为double。<BR><BR>　　第三个表达式将double与一个String相加。Java中同样没有定义这样的操作。所以，编译器将double转换成String类型，并将他们做串联。<BR><BR>　　强制多态也会发生在方法调用中。假设类Derived继承了类Base，类C有一个方法，原型为m(Base)，在下面的代码中，编译器隐式的将Derived类的对象derived转化为Base类的对象。这种隐式的转换使m(Base)方法使用所有能转换成Base类的所有参数。<BR><BR>C c = new C();<BR><BR>Derived derived = new Derived();<BR><BR>c.m( derived );<BR><BR>　　并且，隐式的强制转换，可以避免类型转换的麻烦，减少编译错误。当然，编译器仍然会优先验证符合定义的对象类型。<BR><BR>　　<B>重载的多态</B><BR><BR>　　重载允许用相同的运算符或方法，去表示截然不同的意义。‘+’在上面的程序中有两个意思：两个double型的数相加；两个串相连。另外还有整型相加，长整型，等等。这些运算符的重载，依赖于编译器根据上下文做出的选择。以往的编译器会把操作数隐式转换为完全符合操作符的类型。虽然Java明确支持重载，但不支持用户定义的操作符重载。<BR><BR>　　Java支持用户定义的函数重载。一个类中可以有相同名字的方法，这些方法可以有不同的意义。这些重载的方法中，必须满足参数数目不同，相同位置上的参数类型不同。这些不同可以帮助编译器区分不同版本的方法。<BR><BR>　　编译器以这种唯一表示的特征来表示不同的方法，比用名字表示更为有效。据此，所有的多态行为都能编译通过。<BR><BR>　　强制和重载的多态都被分类为特定的多态，因为这些多态都是在特定的意义上的。这些被划入多态的特性给程序员带来了很大的方便。强制多态排除了麻烦的类型和编译错误。重载多态像一块糖，允许程序员用相同的名字表示不同的方法，很方便。<BR><BR>　　<B>参数的多态</B><BR><BR>　　参数多态允许把许多类型抽象成单一的表示。例如，List抽象类中，描述了一组具有同样特征的对象，提供了一个通用的模板。你可以通过指定一种类型以重用这个抽象类。这些参数可以是任何用户定义的类型，大量的用户可以使用这个抽象类，因此参数多态毫无疑问的成为最强大的多态。<BR><BR>　　乍一看，上面抽象类好像是java.util.List的功能。然而，Java实际上并不支持真正的安全类型风格的参数多态，这也是java.util.List和java.util的其他集合类是用原始的java.lang.Object写的原因（参考我的文章"A Primordial Interface?" 以获得更多细节）。Java的单根继承方式解决了部分问题，但没有发挥出参数多态的全部功能。Eric Allen有一篇精彩的文章“Behold the Power of Parametric Polymorphism”，描述了Java通用类型的需求，并建议给Sun的Java规格需求#000014号文档"Add Generic Types to the Java Programming Language."（参考资源链接）<BR><BR>　　<B>包含的多态</B><BR><BR>　　包含多态通过值的类型和集合的包含关系实现了多态的行为.在包括Java在内的众多面向对象语言中，包含关系是子类型的。所以，Java的包含多态是子类型的多态。<BR><BR>　　在早期，Java开发者们所提及的多态就特指子类型的多态。通过一种面向类型的观点，我们可以看到子类型多态的强大功能。以下的文章中我们将仔细探讨这个问题。为简明起见，下文中的多态均指包含多态。<BR><BR></FONT><SPAN class=f14><FONT color=#a52a2a>　　<B>面向类型观点</B> <BR><BR>　　图1的UML类图给出了类和类型的简单继承关系，以便于解释多态机制。模型中包含5种类型，4个类和一个接口。虽然UML中称为类图，我把它看成类型图。如"Thanks Type and Gentle Class," 一文中所述，每个类和接口都是一种用户定义的类型。按独立实现的观点（如面向类型的观点），下图中的每个矩形代表一种类型。从实现方法看，四种类型运用了类的结构，一种运用了接口的结构。<BR><BR><IMG onmousewheel="return bbimg(this)" style="CURSOR: pointer" src="http://www.gz235.com/Article/UploadPic/2005-10/2005102710031818.gif" onload="javascript:if(this.width onclick=" align=center vspace=1 border=1 javascript:window.open(this.src);?>screen.width-600)this.style.width=screen.width-600;"&gt;<BR>图1：示范代码的UML类图<BR><BR>　　以下的代码实现了每个用户定义的数据类型，我把实现写得很简单。<BR><BR></FONT></P>
<P>
<TABLE width="100%" bgColor=#ffffff>
<TBODY>
<TR>
<TD><FONT color=#a52a2a>/* Base.java */<BR><BR>public class Base<BR>{<BR>　public String m1()<BR>　{<BR>　　return "Base.m1()";<BR>　}<BR><BR>　public String m2( String s )<BR>　{<BR>　　return "Base.m2( " + s + " )";<BR>　}<BR>}<BR><BR>/* IType.java */<BR><BR>interface IType<BR>{<BR>　String m2( String s );<BR>　String m3();<BR>}<BR><BR>/* Derived.java */<BR><BR>public class Derived<BR>extends Base<BR>implements IType<BR>{<BR>　public String m1()<BR>　{<BR>　　return "Derived.m1()";<BR>　}<BR><BR>　public String m3()<BR>　{<BR>　　return "Derived.m3()";<BR>　}<BR>}<BR><BR>/* Derived2.java */<BR><BR>public class Derived2<BR>extends Derived<BR>{<BR>　public String m2( String s )<BR>　{<BR>　　return "Derived2.m2( " + s + " )";<BR>　}<BR>　public String m4()<BR>　{<BR>　　return "Derived2.m4()";<BR>　}<BR>}<BR><BR>/* Separate.java */<BR><BR>public class Separate<BR>implements IType<BR>{<BR>　public String m1()<BR>　{<BR>　　return "Separate.m1()";<BR>　}<BR>　public String m2( String s )<BR>　{<BR>　　return "Separate.m2( " + s + " )";<BR>　}<BR><BR>　public String m3()<BR>　{<BR>　　return "Separate.m3()";<BR>　}<BR>}<BR></FONT></TD></TR></TBODY></TABLE></P>
<P><BR><FONT color=#a52a2a>　　用这样的类型声明和类的定义，图2从概念的观点描述了Java指令。<BR><BR>Derived2 derived2 = new Derived2();<BR><BR><IMG onmousewheel="return bbimg(this)" style="CURSOR: pointer" src="http://www.gz235.com/Article/UploadPic/2005-10/2005102710031149.gif" onload="javascript:if(this.width onclick=" align=center vspace=1 border=1 javascript:window.open(this.src);?>screen.width-600)this.style.width=screen.width-600;"&gt;<BR>图2 ：Derived2 对象上的引用<BR><BR>　　上文中声明了derived2这个对象，它是Derived2类的。图2种的最顶层把Derived2引用描述成一个集合的窗口，虽然其下的Derived2对象是可见的。这里为每个Derived2类型的操作留了一个孔。Derived2对象的每个操作都去映射适当的代码，按照上面的代码所描述的那样。例如，Derived2对象映射了在Derived中定义的m1()方法。而且还重载了Base类的m1()方法。一个Derived2的引用变量无权访问Base类中被重载的m1()方法。但这并不意味着不可以用super.m1()的方法调用去使用这个方法。关系到derived2这个引用的变量，这个代码是不合适的。Derived2的其他的操作映射同样表明了每种类型操作的代码执行。<BR><BR>　　既然你有一个Derived2对象，可以用任何一个Derived2类型的变量去引用它。如图1所示，Derived, Base和IType都是Derived2的基类。所以，Base类的引用是很有用的。图3描述了以下语句的概念观点。<BR><BR>Base base = derived2;<BR><BR><IMG onmousewheel="return bbimg(this)" style="CURSOR: pointer" src="http://www.gz235.com/Article/UploadPic/2005-10/2005102710032768.gif" onload="javascript:if(this.width onclick=" align=center vspace=1 border=1 javascript:window.open(this.src);?>screen.width-600)this.style.width=screen.width-600;"&gt;<BR>图3：Base类引用附于Derived2对象之上<BR>　 <BR>　　虽然Base类的引用不用再访问m3()和m4()，但是却不会改变它Derived2对象的任何特征及操作映射。无论是变量derived2还是base，其调用m1()或m2(String)所执行的代码都是一样的。<BR><BR></FONT></P>
<P>
<TABLE width="100%" bgColor=#ffffff>
<TBODY>
<TR>
<TD><FONT color=#a52a2a>String tmp;<BR>// Derived2 reference (Figure 2)<BR>tmp = derived2.m1(); // tmp is "Derived.m1()"<BR>tmp = derived2.m2( "Hello" ); // tmp is "Derived2.m2( Hello )"<BR><BR>// Base reference (Figure 3)<BR><BR>tmp = base.m1(); // tmp is "Derived.m1()"<BR>tmp = base.m2( "Hello" ); // tmp is "Derived2.m2( Hello )"</FONT></TD></TR></TBODY></TABLE></P>
<P><BR><FONT color=#a52a2a>　　两个引用之所以调用同一个行为，是因为Derived2对象并不知道去调用哪个方法。对象只知道什么时候调用，它随着继承实现的顺序去执行。这样的顺序决定了Derived2对象调用Derived里的m1()方法，并调用Derived2里的m2(String)方法。这种结果取决于对象本身的类型，而不是引用的类型。<BR><BR>　　尽管如此，但不意味着你用derived2和base引用的效果是完全一样的。如图3所示，Base的引用只能看到Base类型拥有的操作。所以，虽然Derived2有对方法m3()和m4()的映射，但是变量base不能访问这些方法。<BR><BR></FONT></P>
<P>
<TABLE width="100%" bgColor=#ffffff>
<TBODY>
<TR>
<TD><FONT color=#a52a2a>String tmp;<BR>// Derived2 reference (Figure 2)<BR>tmp = derived2.m3(); // tmp is "Derived.m3()"<BR>tmp = derived2.m4(); // tmp is "Derived2.m4()"<BR><BR>// Base reference (Figure 3)<BR><BR>tmp = base.m3(); // Compile-time error<BR>tmp = base.m4(); // Compile-time error</FONT></TD></TR></TBODY></TABLE></P>
<P><BR><FONT color=#a52a2a>　　运行期的Derived2对象保持了接受m3()和m4()方法的能力。类型的限制使Base的引用不能在编译期调用这些方法。编译期的类型检查像一套铠甲，保证了运行期对象只能和正确的操作进行相互作用。换句话说，类型定义了对象间相互作用的边界。<BR><BR></FONT><SPAN class=f14><FONT color=#a52a2a>　　<B>多态的依附性</B><BR><BR>　　类型的一致性是多态的核心。对象上的每一个引用，静态的类型检查器都要确认这样的依附和其对象的层次是一致的。当一个引用成功的依附于另一个不同的对象时，有趣的多态现象就产生了。（严格的说，对象类型是指类的定义。）你也可以把几个不同的引用依附于同一个对象。在开始更有趣的场景前，我们先来看一下下面的情况为什么不会产生多态。<BR><BR>　　<B>多个引用依附于一个对象</B><BR><BR>　　图2和图3描述的例子是把两个及两个以上的引用依附于一个对象。虽然Derived2对象在被依附之后仍保持了变量的类型，但是，图3中的Base类型的引用依附之后，其功能减少了。结论很明显：把一个基类的引用依附于派生类的对象之上会减少其能力。<BR><BR>　　一个开发这怎么会选择减少对象能力的方案呢？这种选择是间接的。假设有一个名为ref的引用依附于一个包含如下方法的类的对象：<BR><BR></FONT></P>
<P>
<TABLE width="100%" bgColor=#ffffff>
<TBODY>
<TR>
<TD><FONT color=#a52a2a>public String poly1( Base base )<BR>{<BR>　return base.m1();<BR>} </FONT></TD></TR></TBODY></TABLE></P>
<P><BR><FONT color=#a52a2a>　　用一个Derived2的参数调用poly(Base)是符合参数类型检查的：<BR><BR></FONT></P>
<P>
<TABLE width="100%" bgColor=#ffffff>
<TBODY>
<TR>
<TD><FONT color=#a52a2a>ref.poly1( derived2 );</FONT></TD></TR></TBODY></TABLE></P>
<P><BR><FONT color=#a52a2a>　　方法调用把一个本地Base类型的变量依附在一个引入的对象上。所以，虽然这个方法只接受Base类型的参数，但Derived2对象仍是允许的。开发这就不必选择丢失功能的方案。从人眼在通过Derived2对象时所看到的情况，Base类型引用的依附导致了功能的丧失。但从执行的观点看，每一个传入poly1(Base)的参数都认为是Base的对象。执行机并不在乎有多个引用指向同一个对象，它只注重把指向另一个对象的引用传给方法。这些对象的类型不一致并不是主要问题。执行器只关心给运行时的对象找到适当的实现。面向类型的观点展示了多态的巨大能力。<BR><BR>　　<B>附于多个对象的引用</B><BR><BR>　　让我们来看一下发生在poly1(Base)中的多态行为。下面的代码创建了三个对象，并通过引用传给poly1(Base):<BR><BR></FONT></P>
<P>
<TABLE width="100%" bgColor=#ffffff>
<TBODY>
<TR>
<TD><FONT color=#a52a2a>Derived2 derived2 = new Derived2();<BR>Derived derived = new Derived();<BR>Base base = new Base();<BR><BR>String tmp;<BR><BR>tmp = ref.poly1( derived2 ); // tmp is "Derived.m1()"<BR>tmp = ref.poly1( derived ); // tmp is "Derived.m1()"<BR>tmp = ref.poly1( base ); // tmp is "Base.m1()"</FONT></TD></TR></TBODY></TABLE></P>
<P><BR><FONT color=#a52a2a>　　poly1(Base)的实现代码是调用传进来的参数的m1()方法。图3和图4展示了把三个类的对象传给方法时，面向类型的所使用的体系结构。<BR><BR><IMG onmousewheel="return bbimg(this)" style="CURSOR: pointer" src="http://www.gz235.com/Article/UploadPic/2005-10/2005102710032671.gif" onload="javascript:if(this.width onclick=" align=center vspace=1 border=1 javascript:window.open(this.src);?>screen.width-600)this.style.width=screen.width-600;"&gt;<BR>图4：将Base引用指向Derived类，以及Base对象<BR><BR>　　请注意每个图中方法m1()的映射。图3中，m1()调用了Derived类的代码；上面代码中的注释标明了ploy1(Base)调用Derived.m1()。图4中Derived对象调用的仍然是Derived类的m1()方法。最后，图4中，Base对象调用的m1()是Base类中定义的代码。<BR><BR>　　多态的魅力何在？再来看一下poly1(Base)的代码，它可以接受任何属于Base类范畴的参数。然而，当他收到一个Derived2的对象时，它实际上却调用了Derived版本的方法。当你根据Base类派生出其他类时，如Derived，Derived2，poly1(Base)都可以接受这些参数，并作出选择调用合适的方法。多态允许你在完成poly1(Base)后扩展它的用途。<BR><BR>　　这看起来当然很神奇。基本的理解展示了多态的内部工作原理。在面向类型的观点中，底层的对象所实现的代码是非实质性的。重要的是，类型检查器会在编译期间为每个引用选择合适的代码以实现其方法。多态使开发者运用面向类型的观点，不考虑实现的细节。这样有助于把类型和实现分离（实际用处是把接口和实现分离）。<BR><BR></FONT><SPAN class=f14><FONT color=#a52a2a>　　<B>对象接口</B><BR><BR>　　多态依赖于类型和实现的分离，多用来把接口和实现分离。但下面的观点好像把Java的关键字interface搞得很糊涂。<BR><BR>　　更为重要的使开发者们怎样理解短语“the interface to an object"，典型地，根据上下文，这个短语的意思是指一切对象类中所定义的方法，至一切对象公开的方法。这种倾向于以实现为中心的观点较之于面向类型的观点来说，使我们更加注重于对象在运行期的能力。图3中，引用面板的对象表面被标志成"Derived2 Object"。这个面板上列出了Derived2对象的所有可用的方法。但是要理解多态，我们必须从实现这一层次上解放出来，并注意面向类型的透视图中被标为"Base Reference"的面板。在这一层意思上，引用变量的类型指明了一个对象的表面。这只是一个表面，不是接口。在类型一致的原则下，我们可以用面向类型的观点，为一个对象依附多个引用。对interface to an object这个短语的理解没有确定的理解。<BR><BR>　　在类型概念中，the interface to an object refers 引用了面向类型观点的最大可能----如图2的情形。把一个基类的引用指向相同的对象缩小了这样的观点----如图3所示。类型概念能使人获得把对象间的相互作用同实现细节分离的要领。相对于一个对象的接口，面向类型的观点更鼓励人们去使用一个对象的引用。引用类型规定了对象间的相互作用。当你考虑一个对象能做什么的时候，只需搞明白他的类型，而不需要去考虑他的实现细节。<BR><BR>　　<B>Java接口</B><BR><BR>　　以上所谈到的多态行为用到了类的继承关系所建立起来的子类型关系。Java接口同样支持用户定义的类型，相对地，Java的接口机制启动了建立在类型层次结构上的多态行为。假设一个名为ref的引用变量，并使其指向一个包含一下方法的类对象：<BR><BR></FONT>
<TABLE width="100%" bgColor=#ffffff>
<TBODY>
<TR>
<TD><FONT color=#a52a2a>public String poly2( IType iType )<BR>{<BR>　return iType.m3();<BR>}</FONT></TD></TR></TBODY></TABLE><BR><FONT color=#a52a2a>　　为了弄明白poly2(IType)中的多态，以下的代码从不同的类创建两个对象，并分别把他们传给poly2(IType)：<BR><BR></FONT>
<TABLE width="100%" bgColor=#ffffff>
<TBODY>
<TR>
<TD><FONT color=#a52a2a>Derived2 derived2 = new Derived2();<BR>Separate separate = new Separate();<BR><BR>String tmp;<BR><BR>tmp = ref.poly2( derived2 ); // tmp is "Derived.m3()"<BR>tmp = ref.poly2( separate ); // tmp is "Separate.m3()"</FONT></TD></TR></TBODY></TABLE><BR><FONT color=#a52a2a>　　上面的代码类似于关于poly1(Base)中的多态的讨论。poly2(IType)的实现代码是调用每个对象的本地版本的m3()方法。如同以前，代码的注释表明了每次调用所返回的CString类型的结果。图5表明了两次调用poly2(IType)的概念结构：<BR><BR><IMG onmousewheel="return bbimg(this)" style="CURSOR: pointer" src="http://www.gz235.com/Article/UploadPic/2005-10/2005102710032443.gif" onload="javascript:if(this.width onclick=" align=center vspace=1 border=1 javascript:window.open(this.src);?>screen.width-600)this.style.width=screen.width-600;"&gt;<BR>图5：指向Derived2和Separate对象的IType引用 <BR><BR>　　方法poly1(Base)和poly2(IType)中所表现的多态行为的相似之处可以从透视图中直接看出来。把我们在实现在一层上的理解再提高一层，就可以看到这两段代码的技巧。基类的引用指向了作为参数传进的类，并且按照类型的限制调用对象的方法。引用既不知道也不关心执行哪一段代码。编译期间的子类型关系检查保证了通过的对象有能力在被调用的时候选择合适的实现代码。<BR><BR>　　然而，他们在实现层上有一个重要的差别。在poly1(Base)的例子中（图3和图4），Base-Derived-Derived2的类继承结构为子类型关系的建立提供了条件，并决定了方法去调用哪段代码。在poly2(IType)的例子中（如图5），则是完全不同的动态发生的。Derived2和Separate不共享任何实现的层次，但是他们还是通过IType的引用展示了多态的行为。<BR><BR>　　这样的多态行为使Java的接口的功能的重大意义显得很明显。图1中的UML类图说明了Derived是Base和IType的子类型。通过完全脱离实现细节的类型的定义方法，Java实现了多类型继承，并且不存在Java所禁止的多继承所带来的烦人的问题。完全脱离实现层次的类可以按照Java接口实现分组。在图1中，接口IType和Derived,Separate以及这类型的其他子类型应该划为一组。<BR><BR>　　按照这种完全不同于实现层次的分类方法，Java的接口机制是多态变得很方便，哪怕不存在任何共享的实现或者复写的方法。如图5所示，一个IType的引用，用多态的方法访问到了Derived2和Separate对象的m3()方法。<BR><BR>　　<B>再次探讨对象的接口</B> <BR><BR>　　注意图5中的Derived2和Separate对象的对m1()的映射方法。如前所述，每一个对象的接口都包含方法m1()。但却没有办法用这两个对象使方法m1()表现出多态的行为。每一个对象占有一个m1()方法是不够的。必须存在一个可以操作m1()方法的类型，通过这个类型可以看到对象。这些对象似乎是共享了m1()方法，但在没有共同基类的条件下，多态是不可能的。通过对象的接口来看多态，会把这个概念搞混。<BR><BR>　　<B>结论</B><BR><BR>　　从全文所述的面向对象多态所建立起来的子类型多态，你可以清楚地认识到这种面向类型的观点。如果你想理解子类型多态的思想，就应该把注意力从实现的细节转移到类型的上。类型把对象分成组，并且管理着这些对象的接口。类型的继承层次结构决定了实现多态所需的类型关系。<BR><BR>　　有趣的是，实现的细节并不影响子类型多态的层次结构。类型决定了对象调用什么方法，而实现则决定了对象怎么执行这个方法。也就是说，类型表明了责任，而负责实施的则是具体的实现。将实现和类型分离后，我们好像看到了这两个部分在一起跳舞，类型决定了他的舞伴和舞蹈的名字，而实现则是舞蹈动作的设计师。<BR></FONT></SPAN></P></SPAN></SPAN></TD></TR></TBODY></TABLE><img src ="http://www.blogjava.net/hmily1211/aggbug/19984.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/hmily1211/" target="_blank">活在JAVA岛的日子</a> 2005-11-16 02:44 <a href="http://www.blogjava.net/hmily1211/archive/2005/11/16/19984.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>初学者如何开发出高质量的J2EE系统 (转载)</title><link>http://www.blogjava.net/hmily1211/archive/2005/11/15/19914.html</link><dc:creator>活在JAVA岛的日子</dc:creator><author>活在JAVA岛的日子</author><pubDate>Tue, 15 Nov 2005 08:13:00 GMT</pubDate><guid>http://www.blogjava.net/hmily1211/archive/2005/11/15/19914.html</guid><wfw:comment>http://www.blogjava.net/hmily1211/comments/19914.html</wfw:comment><comments>http://www.blogjava.net/hmily1211/archive/2005/11/15/19914.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/hmily1211/comments/commentRss/19914.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/hmily1211/services/trackbacks/19914.html</trackback:ping><description><![CDATA[J2EE学习者越来越多，J2EE本身技术不断在发展，涌现出各种概念，本文章试图从一种容易理解的角度对这些概念向初学者进行解释，以便掌握学习J2EE学习方向。首先我们需要知道Java和J2EE是两个不同概念，Java不只是指一种语言，已经代表与微软不同的另外一个巨大阵营，所以Java有时是指一种软件系统的流派，当然目前主要是.NET和Java两大主流体系。<BR><BR>　　J2EE可以说指Java在数据库信息系统上实现，数据库信息系统从早期的dBase、到Delphi/VB等C/S结构，发展到B/S（Browser浏览器/Server服务器）结构，而J2EE主要是指B/S结构的实现。<BR><BR>　　J2EE又是一种框架和标准，框架类似API、库的概念，但是要超出它们。如果需要详细了解框架，可先从设计模式开始学习。<BR><BR>　　J2EE是一个虚的大的概念，J2EE标准主要有三种子技术标准：WEB技术、EJB技术和JMS，谈到J2EE应该说最终要落实到这三个子概念上。<BR><BR>　　这三种技术的每个技术在应用时都涉及两个部分：容器部分和应用部分，Web容器也是指Jsp/Servlet容器，你如果要开发一个Web应用，无论是编译或运行，都必须要有Jsp/Servlet库或API支持（除了JDK/J2SE以外）。<BR><BR>　　Web技术中除了Jsp/Servlet技术外，还需要JavaBeans或Java Class实现一些功能或者包装携带数据，所以Web技术最初裸体简称为Jsp/Servlet+JavaBeans系统。<BR>谈到JavaBeans技术，就涉及到组件构件技术（component），这是Java的核心基础部分，很多软件设计概念（设计模式）都是通过JavaBeans实现的。<BR><BR>　　JavaBeans不属于J2EE概念范畴中，如果一个JavaBeans对象被Web技术（也就是Jsp/Servlet）调用，那么JavaBeans就运行在J2EE的Web容器中；如果它被EJB调用，它就运行在EJB容器中。<BR><BR>　　EJB（企业JavaBeans）是普通JavaBeans的一种提升和规范，因为企业信息系统开发中需要一个可伸缩的性能和事务、安全机制，这样能保证企业系统平滑发展，而不是发展到一种规模重新更换一套软件系统。<BR><BR>　　至此，JavaBeans组件发展到EJB后，并不是说以前的那种JavaBeans形式就消失了，这就自然形成了两种JavaBeans技术：EJB和POJO，POJO完全不同于EJB概念，指的是普通JavaBeans，而且这个JavaBeans不依附某种框架，或者干脆可以说：这个JavaBeans是你为这个应用程序单独开发创建的。<BR><BR>　　J2EE应用系统开发工具有很多：如JBuilder、Eclipse等，这些IDE首先是Java开发工具，也就是说，它们首要基本功能是可以开发出JavaBeans或Java class，但是如果要开发出J2EE系统，就要落实到要么是Web技术或EJB技术，那么就有可能要一些专门模块功能(如eclipse需要lomboz插件)，最重要的是，因为J2EE系统区分为容器和应用两个部分，所以，在任何开发工具中开发J2EE都需要指定J2EE容器。<BR><BR>　　J2EE容器分为WEB容器和EJB容器，Tomcat/Resin是Web容器；JBoss是EJB容器+Web容器等，其中Web容器直接使用Tomcat实现的。所以你开发的Web应用程序可以在上面两种容器运行，而你开发的Web+EJB应用则只可以在JBoss服务器上运行，商业产品Websphere/Weblogic等和JBoss属于同一种性质。<BR><BR>　　J2EE容器也称为J2EE服务器，大部分时它们概念是一致的。<BR>如果你的J2EE应用系统的数据库连接是通过JNDI获得，也就是说是从容器中获得，那么你的J2EE应用系统基本与数据库无关，如果你在你的J2EE应用系统耦合了数据库JDBC驱动的配置，那么你的J2EE应用系统就有数据库概念色彩，作为一个成熟需要推广的J2EE应用系统，不推荐和具体数据库耦合，当然这其中如何保证J2EE应用系统运行性能又是体现你的设计水平了。<BR><BR>　　衡量J2EE应用系统设计开发水平高低的标准就是：解耦性；你的应用系统各个功能是否能够彻底脱离？是否不相互依赖，也只有这样，才能体现可维护性、可拓展性的软件设计目标。<BR><BR>　　为了达到这个目的，诞生各种框架概念，J2EE框架标准将一个系统划分为WEB和EJB主要部分，当然我们有时不是以这个具体技术区分，而是从设计上抽象为表现层、服务层和持久层，这三个层次从一个高度将J2EE分离开来，实现解耦目的。<BR><BR>　　因此，我们实际编程中，也要将自己的功能向这三个层次上靠，做到大方向清楚，泾渭分明，但是没有技术上约束限制要做到这点是很不容易的，因此我们还是必须借助J2EE具体技术来实现，这时，你可以使用EJB规范实现服务层和持久层，Web技术实现表现层；<BR><BR>　　EJB为什么能将服务层从Jsp/Servlet手中分离出来，因为它对JavaBeans编码有强制的约束，现在有一种对JavaBeans弱约束，使用Ioc模式实现的（当然EJB 3.0也采取这种方式），在Ioc模式诞生前，一般都是通过工厂模式来对JavaBeans约束，形成一个服务层，这也是是Jive这样开源论坛设计原理之一。<BR><BR>　　由此，将服务层从表现层中分离出来目前有两种可选架构选择：管理普通JavaBeans（POJO）框架(如Spring、JdonFramework)以及管理EJB的EJB框架，因为EJB不只是框架，还是标准，而标准可以扩展发展，所以，这两种区别将来是可能模糊，被纳入同一个标准了。　但是，个人认为：标准制定是为某个目的服务的，总要牺牲一些换取另外一些，所以，这两种架构会长时间并存。<BR><BR>　　这两种架构分歧也曾经诞生一个新名词：完全POJO的系统也称为轻量级系统(lightweight)，其实这个名词本身就没有一个严格定义，更多是一个吸引人的招牌，轻量是指容易学习容易使用吗？按照这个定义，其实轻量Spring等系统并不容易学习；而且EJB 3.0（依然叫EJB）以后的系统是否可称为轻量级了呢？<BR>前面谈了服务层框架，使用服务层框架可以将JavaBeans从Jsp/Servlet中分离出来，而使用表现层框架则可以将Jsp中剩余的JavaBeans完全分离，这部分JavaBeans主要负责显示相关，一般是通过标签库（taglib）实现，不同框架有不同自己的标签库，Struts是应用比较广泛的一种表现层框架。<BR><BR>　　这样，表现层和服务层的分离是通过两种框架达到目的，剩余的就是持久层框架了，通过持久层的框架将数据库存储从服务层中分离出来是其目的，持久层框架有两种方向：直接自己编写JDBC等SQL语句（如iBatis）；使用O/R Mapping技术实现的Hibernate和JDO技术；当然还有EJB中的实体Bean技术。<BR><BR>　　持久层框架目前呈现百花齐放，各有优缺点的现状，所以正如表现层框架一样，目前没有一个框架被指定为标准框架，当然，表现层框架现在又出来了一个JSF，它代表的页面组件概念是一个新的发展方向，但是复杂的实现让人有些忘而却步。<BR><BR>　　在所有这些J2EE技术中，虽然SUN公司发挥了很大的作用，不过总体来说：网络上有这样一个评价：SUN的理论天下无敌；SUN的产品用起来撞墙；对于初学者，特别是那些试图通过或已经通过SUN认证的初学者，赶快摆脱SUN的阴影，立即开溜，使用开源领域的产品来实现自己的应用系统。<BR><BR>　　最后，你的J2EE应用系统如果采取上面提到的表现层、服务层和持久层的框架实现，基本你也可以在无需深刻掌握设计模式的情况下开发出一个高质量的应用系统了。<BR><BR>　　还要注意的是: 开发出一个高质量的J2EE系统还需要正确的业务需求理解，那么域建模提供了一种比较切实可行的正确理解业务需求的方法，相关详细知识可从UML角度结合理解。<BR><BR>　　当然，如果你想设计自己的行业框架，那么第一步从设计模式开始吧，因为设计模式提供你一个实现JavaBeans或类之间解耦参考实现方法，当你学会了系统基本单元JavaBean或类之间解耦时，那么系统模块之间的解耦你就可能掌握，进而你就可以实现行业框架的提炼了，这又是另外一个发展方向了。<BR><img src ="http://www.blogjava.net/hmily1211/aggbug/19914.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/hmily1211/" target="_blank">活在JAVA岛的日子</a> 2005-11-15 16:13 <a href="http://www.blogjava.net/hmily1211/archive/2005/11/15/19914.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>好象搞懂了</title><link>http://www.blogjava.net/hmily1211/archive/2005/11/12/19529.html</link><dc:creator>活在JAVA岛的日子</dc:creator><author>活在JAVA岛的日子</author><pubDate>Sat, 12 Nov 2005 13:53:00 GMT</pubDate><guid>http://www.blogjava.net/hmily1211/archive/2005/11/12/19529.html</guid><wfw:comment>http://www.blogjava.net/hmily1211/comments/19529.html</wfw:comment><comments>http://www.blogjava.net/hmily1211/archive/2005/11/12/19529.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.blogjava.net/hmily1211/comments/commentRss/19529.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/hmily1211/services/trackbacks/19529.html</trackback:ping><description><![CDATA[<P>public class statictest {<BR>&nbsp;&nbsp;&nbsp; public statictest() <BR>&nbsp;&nbsp;&nbsp; {&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp; public static void prin(String s)<BR>&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp; System.out.println(s);<BR>&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp; public static int i=printy("hehe");<BR>&nbsp;&nbsp;&nbsp; public static int printy(String s)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(s);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return 4;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp; public static void main(String[] args) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; statictest.prin("fdsafa");<BR>&nbsp;&nbsp;&nbsp; }<BR>}<BR>输出结果 hehe fdsafa<BR>当生成一个类的对象时,或者首次访问属于哪个类的静态数据成员时,,进行初始化.<BR>package untitled4;<BR>class teststatic<BR>{<BR>&nbsp;&nbsp; static int i=prin("test");<BR>&nbsp;&nbsp; static int prin(String s)<BR>&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(s);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return 2;<BR>&nbsp;&nbsp; }<BR>&nbsp;&nbsp; static void play()<BR>&nbsp;&nbsp; {<BR>&nbsp;&nbsp; System.out.println("play");<BR>&nbsp;&nbsp; }</P>
<P>}<BR>public class statictest2 {<BR>&nbsp;&nbsp;&nbsp; public statictest2() {<BR>&nbsp;&nbsp;&nbsp; }</P>
<P>&nbsp;&nbsp;&nbsp; public static void main(String[] args) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; teststatic.play();<BR>&nbsp;&nbsp;&nbsp; }<BR>}<BR>输出结果&nbsp;&nbsp; TEST PLAY <BR>对于类CLASS A的执行相当于调用A.main(),,他首先对A的元素初始化(遵循,从A的基类开始,STATIC)<BR>非STATIC在对象生成时候才初始化</P><img src ="http://www.blogjava.net/hmily1211/aggbug/19529.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/hmily1211/" target="_blank">活在JAVA岛的日子</a> 2005-11-12 21:53 <a href="http://www.blogjava.net/hmily1211/archive/2005/11/12/19529.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>