??xml version="1.0" encoding="utf-8" standalone="yes"?>亚洲色自偷自拍另类小说,亚洲爆乳精品无码一区二区,亚洲1区1区3区4区产品乱码芒果http://www.tkk7.com/tianmaoye/category/31980.html...从明天vQ做一个幸的人?..喂马Q劈_周游世界?..从明天vQ开始关心粮食和蔬菜?..我有一所房子Q面朝大h暖花开......zh-cnFri, 25 Jul 2008 11:39:53 GMTFri, 25 Jul 2008 11:39:53 GMT60JAVA字符串处理函数列表一?/title><link>http://www.tkk7.com/tianmaoye/articles/217534.html</link><dc:creator>飞行?/dc:creator><author>飞行?/author><pubDate>Fri, 25 Jul 2008 10:07:00 GMT</pubDate><guid>http://www.tkk7.com/tianmaoye/articles/217534.html</guid><wfw:comment>http://www.tkk7.com/tianmaoye/comments/217534.html</wfw:comment><comments>http://www.tkk7.com/tianmaoye/articles/217534.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.tkk7.com/tianmaoye/comments/commentRss/217534.html</wfw:commentRss><trackback:ping>http://www.tkk7.com/tianmaoye/services/trackbacks/217534.html</trackback:ping><description><![CDATA[<div id="tuqjmll" class="date"> Java中的字符串也是一q串的字W。但是与许多其他的计机语言字W串作ؓ字符数组处理不同QJava字W串作ؓStringcd对象来处理。将字符串作为内|的对象处理允许Java提供十分丰富的功能特性以方便处理字符丌Ӏ下面是一些用频率比较高的函数及其相兌明?</div> <div id="ypskpen" class="entry"> <div id="zezoene" class="adsDiv"> </div> <p><img src="http://ntt.cc/wp-content/themes/i3theme-1-5-right/images/bullet-grey.gif" alt="" />substring()<br /> 它有两种形式Q第一U是QString substring(int startIndex)<br /> W二U是QString substring(int startIndex,int endIndex)</p> <p><img src="http://ntt.cc/wp-content/themes/i3theme-1-5-right/images/bullet-grey.gif" alt="" />concat() q接两个字符?/p> <p><img src="http://ntt.cc/wp-content/themes/i3theme-1-5-right/images/bullet-grey.gif" alt="" />replace() 替换<br /> 它有两种形式Q第一UŞ式用一个字W在调用字符串中所有出现某个字W的地方q行替换QŞ式如下:<br /> String replace(char original,char replacement)<br /> 例如QString s=”Hello”.replace(’l',’w');<br /> W二UŞ式是用一个字W序列替换另一个字W序列,形式如下Q?br /> String replace(CharSequence original,CharSequence replacement)</p> <p><img src="http://ntt.cc/wp-content/themes/i3theme-1-5-right/images/bullet-grey.gif" alt="" />trim() L起始和结I格</p> <p><img src="http://ntt.cc/wp-content/themes/i3theme-1-5-right/images/bullet-grey.gif" alt="" />valueOf() 转换为字W串</p> <p><img src="http://ntt.cc/wp-content/themes/i3theme-1-5-right/images/bullet-grey.gif" alt="" />toLowerCase() 转换为小?/p> <p><img src="http://ntt.cc/wp-content/themes/i3theme-1-5-right/images/bullet-grey.gif" alt="" />toUpperCase() 转换为大?/p> <p><img src="http://ntt.cc/wp-content/themes/i3theme-1-5-right/images/bullet-grey.gif" alt="" />length() 取得字符串的长度<br /> 例:<br /> char chars[]={’a',’b’.’c'};<br /> String s=new String(chars);<br /> int len=s.length();</p> <p><img src="http://ntt.cc/wp-content/themes/i3theme-1-5-right/images/bullet-grey.gif" alt="" />charAt() 截取一个字W?br /> 例:<br /> char ch;<br /> ch=”abc”.charAt(1); <br /> q回gؓ’b’</p> <p><img src="http://ntt.cc/wp-content/themes/i3theme-1-5-right/images/bullet-grey.gif" alt="" />getChars() 截取多个字符<br /> void getChars(int sourceStart,int sourceEnd,char target[],int targetStart)<br /> sourceStart 指定了子串开始字W的下标<br /> sourceEnd 指定了子串结束后的下一个字W的下标。因此,子串包含从sourceStart到sourceEnd-1的字W?br /> target 指定接收字符的数l?br /> targetStart target中开始复制子串的下标?br /> 例:<br /> String s=”this is a demo of the getChars method.”;<br /> char buf[]=new char[20];<br /> s.getChars(10,14,buf,0);</p> <p><img src="http://ntt.cc/wp-content/themes/i3theme-1-5-right/images/bullet-grey.gif" alt="" />getBytes()<br /> 替代getChars()的一U方法是字W存储在字节数组中,该方法即getBytes()<br /> 例:<br /> String s = “Hello!你好Q?#8221;; <br /> byte[] bytes = s.getBytes(); </p> <p><img src="http://ntt.cc/wp-content/themes/i3theme-1-5-right/images/bullet-grey.gif" alt="" />toCharArray()<br /> 例:<br /> String s = “Hello!你好Q?#8221;; <br /> char[] ss = s.toCharArray();</p> <p><img src="http://ntt.cc/wp-content/themes/i3theme-1-5-right/images/bullet-grey.gif" alt="" />equals()和equalsIgnoreCase() 比较两个字符?/p> <p><img src="http://ntt.cc/wp-content/themes/i3theme-1-5-right/images/bullet-grey.gif" alt="" />regionMatches() 用于比较一个字W串中特定区域与另一特定区域Q它有一个重载的形式允许在比较中忽略大小写?br /> boolean regionMatches(int startIndex,String str2,int<br /> str2StartIndex,int numChars)<br /> boolean regionMatches(boolean ignoreCase,int startIndex,String<br /> str2,int str2StartIndex,int numChars)</p> <p><img src="http://ntt.cc/wp-content/themes/i3theme-1-5-right/images/bullet-grey.gif" alt="" />startsWith()和endsWith()<br /> startsWith()Ҏ军_是否以特定字W串开始,endWith()Ҏ军_是否以特定字W串l束</p> <p><img src="http://ntt.cc/wp-content/themes/i3theme-1-5-right/images/bullet-grey.gif" alt="" />equals()?=<br /> equals()Ҏ比较字符串对象中的字W,==q算W比较两个对象是否引用同一实例?br /> 例:String s1=”Hello”;<br /> String s2=new String(s1);<br /> s1.eauals(s2); //true<br /> s1==s2;//false</p> <p><img src="http://ntt.cc/wp-content/themes/i3theme-1-5-right/images/bullet-grey.gif" alt="" />compareTo()和compareToIgnoreCase() 比较字符?/p> <p><img src="http://ntt.cc/wp-content/themes/i3theme-1-5-right/images/bullet-grey.gif" alt="" />indexOf()和lastIndexOf()<br /> indexOf() 查找字符或者子串第一ơ出现的地方?br /> lastIndexOf() 查找字符或者子串是后一ơ出现的地方?/p> <p><img src="http://ntt.cc/wp-content/themes/i3theme-1-5-right/images/bullet-grey.gif" alt="" />StringBuffer构造函?br /> StringBuffer定义了三个构造函敎ͼ<br /> StringBuffer()<br /> StringBuffer(int size)<br /> StringBuffer(String str)<br /> StringBuffer(CharSequence chars)</p> <p>下面是StringBuffer相关的函敎ͼ<br /> <img src="http://ntt.cc/wp-content/themes/i3theme-1-5-right/images/bullet-grey.gif" alt="" />length()和capacity()<br /> 一个StringBuffer当前长度可通过length()Ҏ得到,而整个可分配I间通过capacity()Ҏ得到?/p> <p><img src="http://ntt.cc/wp-content/themes/i3theme-1-5-right/images/bullet-grey.gif" alt="" />ensureCapacity() 讄~冲区的大小<br /> void ensureCapacity(int capacity)</p> <p><img src="http://ntt.cc/wp-content/themes/i3theme-1-5-right/images/bullet-grey.gif" alt="" />setLength() 讄~冲区的长度<br /> void setLength(int len)</p> <p><img src="http://ntt.cc/wp-content/themes/i3theme-1-5-right/images/bullet-grey.gif" alt="" />charAt()和setCharAt()<br /> char charAt(int where)<br /> void setCharAt(int where,char ch)</p> <p><img src="http://ntt.cc/wp-content/themes/i3theme-1-5-right/images/bullet-grey.gif" alt="" />getChars()<br /> void getChars(int sourceStart,int sourceEnd,char target[],int targetStart)</p> <p><img src="http://ntt.cc/wp-content/themes/i3theme-1-5-right/images/bullet-grey.gif" alt="" />append() 可把Mcd数据的字W串表示q接到调用的StringBuffer对象的末?br /> 例:int a=42;<br /> StringBuffer sb=new StringBuffer(40);<br /> String s=sb.append(”a=”).append(a).append(”!”).toString();</p> <p><img src="http://ntt.cc/wp-content/themes/i3theme-1-5-right/images/bullet-grey.gif" alt="" />insert() 插入字符?br /> StringBuffer insert(int index,String str)<br /> StringBuffer insert(int index,char ch)<br /> StringBuffer insert(int index,Object obj)<br /> index指定字W串插入到StringBuffer对象中的位置的下标?/p> <p><img src="http://ntt.cc/wp-content/themes/i3theme-1-5-right/images/bullet-grey.gif" alt="" />reverse() 颠倒StringBuffer对象中的字符<br /> StringBuffer reverse()</p> <p><img src="http://ntt.cc/wp-content/themes/i3theme-1-5-right/images/bullet-grey.gif" alt="" />delete()和deleteCharAt() 删除字符<br /> StringBuffer delete(int startIndex,int endIndex)<br /> StringBuffer deleteCharAt(int loc)</p> <p><img src="http://ntt.cc/wp-content/themes/i3theme-1-5-right/images/bullet-grey.gif" alt="" />replace() 替换<br /> StringBuffer replace(int startIndex,int endIndex,String str)</p> <p><img src="http://ntt.cc/wp-content/themes/i3theme-1-5-right/images/bullet-grey.gif" alt="" />substring() 截取子串<br /> String substring(int startIndex)<br /> String substring(int startIndex,int endIndex)</p> <!-- 2008/01/22 TEI add Google Adsense Begin --><!-- <p class="submeta"> --></div> <img src ="http://www.tkk7.com/tianmaoye/aggbug/217534.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/tianmaoye/" target="_blank">飞行?/a> 2008-07-25 18:07 <a href="http://www.tkk7.com/tianmaoye/articles/217534.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>String与StringBuffer【{?/title><link>http://www.tkk7.com/tianmaoye/articles/208385.html</link><dc:creator>飞行?/dc:creator><author>飞行?/author><pubDate>Mon, 16 Jun 2008 09:27:00 GMT</pubDate><guid>http://www.tkk7.com/tianmaoye/articles/208385.html</guid><wfw:comment>http://www.tkk7.com/tianmaoye/comments/208385.html</wfw:comment><comments>http://www.tkk7.com/tianmaoye/articles/208385.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.tkk7.com/tianmaoye/comments/commentRss/208385.html</wfw:commentRss><trackback:ping>http://www.tkk7.com/tianmaoye/services/trackbacks/208385.html</trackback:ping><description><![CDATA[<div id="iykgtwj" class="content" style="overflow: auto" minmax_bound="true"> <p minmax_bound="true"><font size="2" minmax_bound="true">String的创?/font></p> <p minmax_bound="true"><font size="2" minmax_bound="true">  String s = "hello";<br minmax_bound="true" />   JVM先根据内?hello"查找对象Q如果没有找刎ͼ则在heap上创建新对象Qƈ其赋予s1Q否则用已l存在的对象</font></p> <p minmax_bound="true"><font size="2" minmax_bound="true">  String s = new String("hello");<br minmax_bound="true" />   JVM直接在heap上创建新的对象,所以在heap中会出现内容相同Q地址不同的String对象</font></p> <p minmax_bound="true"><br minmax_bound="true" /> <font size="2" minmax_bound="true">String的比?/font></p> <p minmax_bound="true"><font size="2" minmax_bound="true">  "=="        比较地址<br minmax_bound="true" />   "equals"    比较内容</font></p> <p minmax_bound="true"><font size="2" minmax_bound="true">  举例:<br minmax_bound="true" />   String s1 = "hello";<br minmax_bound="true" />   String s2 = "hello";<br minmax_bound="true" />   String s3 = new String("hello");</font></p> <p minmax_bound="true"><font size="2" minmax_bound="true">  s1 == s2;               // true        地址相同<br minmax_bound="true" />   s1 == s3;               // false       地址不同<br minmax_bound="true" />   s1.equals(s2);          // true        内容相同<br minmax_bound="true" />   s1.equals(s3);          // true        内容相同</font></p> <p minmax_bound="true"><br minmax_bound="true" /> <font size="2" minmax_bound="true">intern() Ҏ</font></p> <p minmax_bound="true"><font size="2" minmax_bound="true">   查找内容相同(equals())的字W串</font></p> <p minmax_bound="true"><font size="2" minmax_bound="true">   String s1 = "hello";                // hello不存在,jvm创徏新对?(1)<br minmax_bound="true" />    String s2 = new String("hello");    // 创D新对?(2)Q这时heap中存在两个内容ؓhello的对?br minmax_bound="true" />    s1 == s2;           // false        // 地址不同<br minmax_bound="true" />    s1.equals(s2);      // true         // 内容相同<br minmax_bound="true" />    s2 = s2.intern();   // true         // 扑ֈ对象(1) q赋予s2<br minmax_bound="true" />    s1 == s2;           // true !!      // 注意Q此时s1,s2同指?1)</font></p> <p minmax_bound="true"><br minmax_bound="true" /> <font size="2" minmax_bound="true">效率QString ?StringBuffer</font></p> <p minmax_bound="true"><font size="2" minmax_bound="true">    情景1Q?br minmax_bound="true" />     (1) String result = "hello" + " world";<br minmax_bound="true" />     (2) StringBuffer result = new String().append("hello").append(" world");</font></p> <p minmax_bound="true"><font size="2" minmax_bound="true">        (1) 的效率好?(2)Q不要奇怪,q是因ؓJVM会做如下处理<br minmax_bound="true" />         ~译?nbsp;  String result = "hello" + " world";<br minmax_bound="true" />         ~译?nbsp;  String result = "hello world";</font></p> <p minmax_bound="true"><br minmax_bound="true" /> <font size="2" minmax_bound="true">    情景2Q?br minmax_bound="true" />     (1) public String getString(String s1, String s2) {<br minmax_bound="true" />             return s1 + s2;<br minmax_bound="true" />         }<br minmax_bound="true" />     (2) public String getString(String s1, String s2) {<br minmax_bound="true" />             return new StringBuffer().append(s1).append(s2);<br minmax_bound="true" />         }</font></p> <p minmax_bound="true"><font size="2" minmax_bound="true">        (1) 的效率与 (2) 一Pq是因ؓJVM会做如下处理<br minmax_bound="true" />         ~译?nbsp;  return s1 + s2;<br minmax_bound="true" />         ~译?nbsp;  return new StringBuffer().append(s1).append(s2);</font></p> <p minmax_bound="true"><br minmax_bound="true" /> <font size="2" minmax_bound="true">    情景3Q?br minmax_bound="true" />     (1) String s = "s1";<br minmax_bound="true" />               s += "s2";<br minmax_bound="true" />               s += "s3";<br minmax_bound="true" />     (2) StringBuffer s = new StringBuffer().append("s1").append("s2").append("s3");</font></p> <p minmax_bound="true"><font size="2" minmax_bound="true">        (2) 的效率好?1)Q因为String是不可变对象Q每?+="操作都会造成构造新的String对象</font></p> <p minmax_bound="true"><br minmax_bound="true" /> <font size="2" minmax_bound="true">    情景4Q?br minmax_bound="true" />     (1) StringBuffer s = new StringBuffer();<br minmax_bound="true" />         for (int i = 0; i < 50000; i ++) {<br minmax_bound="true" />             s.append("hello");<br minmax_bound="true" />         }<br minmax_bound="true" />     (2) StringBuffer s = new StringBuffer(250000);<br minmax_bound="true" />         for (int i = 0; i < 50000; i ++) {<br minmax_bound="true" />             s.append("hello");<br minmax_bound="true" />         }<br minmax_bound="true" />        <br minmax_bound="true" />         (2) 的效率好?(1)Q因为StringBuffer内部实现是char数组Q默认初始化长度?6Q每当字W串长度大于char<br minmax_bound="true" />         数组长度的时候,JVM会构造更大的新数l,q将原先的数l内容复制到新数l,(2)避免了复制数l的开销<br minmax_bound="true" />         <br minmax_bound="true" />     </font></p> <p minmax_bound="true"><br minmax_bound="true" /> <font size="2" minmax_bound="true">关键?br minmax_bound="true" />     1). 单的认ؓ .append() 效率好于 "+" 是错误的Q?br minmax_bound="true" />     2). 不要使用 new 创徏 String<br minmax_bound="true" />     3). 注意 .intern() 的?br minmax_bound="true" />     4). 在编译期能够定字符串值的情况下,使用"+"效率最?br minmax_bound="true" />     5). 避免使用 "+=" 来构造字W串<br minmax_bound="true" />     6). 在声明StringBuffer对象的时候,指定合适的capacityQ不要用默认?18)<br minmax_bound="true" />     7). 注意以下二者的区别不一?br minmax_bound="true" />         - String s = "a" + "b";<br minmax_bound="true" />         - String s = "a";<br minmax_bound="true" />           s += "b";</font></p> <p minmax_bound="true"><font size="2" minmax_bound="true"> </font></p> <p minmax_bound="true"><font size="2" minmax_bound="true">String和StringBuffer之概?br minmax_bound="true" />   创徏字符串的较佳途径<br minmax_bound="true" />   滞留字符串带来的优化<br minmax_bound="true" />   q接字符串时的优化技?br minmax_bound="true" />   借助StringBuffer的初始化q程的优化技?br minmax_bound="true" />   关键?/font></p> <div minmax_bound="true"><font size="2" minmax_bound="true"> </font></div> <div minmax_bound="true"><font size="2" minmax_bound="true"><strong minmax_bound="true">String和StringBuffer之概?/strong><br minmax_bound="true" />   非可变对象一旦创Z后就不能再被改变Q可变对象则可以在创Z后被改变。String对象是非可变对象QStringBuffer对象则是可变对象。ؓ获得更佳的性能你需要根据实际情况小心}慎地选择到底使用q两者中的某一个。下面的话题会作详细的阐q。(注意Q这个章节假设读者已l具备Java的String和StringBuffer的相兛_知识。)</font></div> <div minmax_bound="true"><font size="2" minmax_bound="true"> </font></div> <div minmax_bound="true"><strong minmax_bound="true"><font size="2" minmax_bound="true">创徏字符串的较佳途径</font></strong></div> <div minmax_bound="true"><font size="2" minmax_bound="true">你可以按照以下方式创建字W串对象Q?br minmax_bound="true" /> 1. String s1 = "hello"; <br minmax_bound="true" />     String s2 = "hello"; <br minmax_bound="true" /> 2. String s3 = new String("hello");<br minmax_bound="true" />     String s4 = new String("hello");</font></div> <div minmax_bound="true"><font size="2" minmax_bound="true"> </font></div> <div minmax_bound="true"><font size="2" minmax_bound="true">上面哪种方式会带来更好的性能呢?下面的代码片断用来测量二者之间的区别?/font></div> <div minmax_bound="true"><br minmax_bound="true" /> <font size="2" minmax_bound="true">StringTest1.java</font></div> <div minmax_bound="true"><font style="background-color: #c0c0c0" size="2" minmax_bound="true">package com.performance.string;</font></div> <div minmax_bound="true"><font style="background-color: #c0c0c0" size="2" minmax_bound="true">/** This class shows the time taken for creation of <br minmax_bound="true" />  *  String literals and String objects.<br minmax_bound="true" />  */</font></div> <div minmax_bound="true"><font style="background-color: #c0c0c0" size="2" minmax_bound="true">public class StringTest1 {</font></div> <div minmax_bound="true"><font style="background-color: #c0c0c0" size="2" minmax_bound="true">public static void main(String[] args){</font></div> <div minmax_bound="true"><font style="background-color: #c0c0c0" size="2" minmax_bound="true">    // create String literals<br minmax_bound="true" />     long startTime = System.currentTimeMillis();<br minmax_bound="true" />     for(int i=0;i<50000;i++){</font></div> <div minmax_bound="true"><font style="background-color: #c0c0c0" size="2" minmax_bound="true">    String s1 = "hello";<br minmax_bound="true" />     String s2 = "hello"; <br minmax_bound="true" />     }</font></div> <div minmax_bound="true"><font style="background-color: #c0c0c0" size="2" minmax_bound="true">    long endTime = System.currentTimeMillis();<br minmax_bound="true" />     System.out.println("Time taken for creation of String literals : "<br minmax_bound="true" />                   + (endTime - startTime) + " milli seconds" );</font></div> <div minmax_bound="true"><font style="background-color: #c0c0c0" size="2" minmax_bound="true">    // create String objects using 'new' keyword        <br minmax_bound="true" />     long startTime1 = System.currentTimeMillis();<br minmax_bound="true" />     for(int i=0;i<50000;i++){</font></div> <div minmax_bound="true"><font style="background-color: #c0c0c0" size="2" minmax_bound="true">    String s3 = new String("hello");<br minmax_bound="true" />     String s4 = new String("hello");<br minmax_bound="true" />     }</font></div> <div minmax_bound="true"><font size="2" minmax_bound="true"><font style="background-color: #c0c0c0" minmax_bound="true">    long endTime1 = System.currentTimeMillis();<br minmax_bound="true" />     System.out.println("Time taken for creation of String objects : " <br minmax_bound="true" />                   + (endTime1 - startTime1)+" milli seconds");<br minmax_bound="true" />     }<br minmax_bound="true" /> }</font><br minmax_bound="true" /> q段代码的输出:<br minmax_bound="true" /> </font><font style="background-color: #c0c0c0" size="2" minmax_bound="true">Time taken for creation of String literals : 0 milli seconds<br minmax_bound="true" /> Time taken for creation of String objects : 170 milli seconds</font></div> <div minmax_bound="true"><font style="background-color: #c0c0c0" minmax_bound="true"></font><font size="2" minmax_bound="true"> </font></div> <div minmax_bound="true"><font size="2" minmax_bound="true"><strong minmax_bound="true">JVM是怎样处理字符串的呢?</strong><br minmax_bound="true" />   Java虚拟Zl护一个内部的滞留字符串对象的列表Q唯一字符串的池)来避免在堆内存中产生重复的String对象。当JVM从class文g里加载字W串字面量ƈ执行的时候,它会先检查一下当前的字符串是否已l存在于滞留字符串列表,如果已经存在Q那׃会再创徏一个新的String对象而是引用指向已l存在的String对象QJVM会在内部为字W串字面量作q种查,但ƈ不会为通过new关键字创建的String对象作这U检查。当然你可以明确C用String.intern()Ҏ强制JVM为通过new关键字创建的String对象作这L查。这样可以强制JVM查内部列表而用已有的String对象?/font></div> <div minmax_bound="true"><font size="2" minmax_bound="true">  所以结论是QJVM会内在地为字W串字面量维护一些唯一的String对象Q程序员不需要ؓ字符串字面量而发愁,但是可能会被一些通过new关键字创建的String对象而困扎ͼ不过他们可以使用intern()Ҏ来避免在堆内存上创徏重复的String对象来改善Java的运行性能。下一节会向大家展示更多的信息?/font></div> <div minmax_bound="true"><font size="2" minmax_bound="true"> </font></div> <div minmax_bound="true"><font size="2" minmax_bound="true">下图展示了未使用intern()Ҏ来创建字W串的情c?br minmax_bound="true" />  </font></div> <p minmax_bound="true"><font size="2" minmax_bound="true"><img style="width: auto; height: auto; minmaxwidth: auto; minmaxheight: auto; maxwidth: 450px" alt="" src="http://www.webdn.com/web_file/program/jsp/060208097/images/20060117201239_1.gif" minmax_bound="true" /> </font></p> <div minmax_bound="true"><font size="2" minmax_bound="true">  你可以自׃?=操作W和String.equals()Ҏ来编码测试上面提到的区别?=操作W会q回true如果一些引用指向一个相同的对象但不会判断String对象的内Ҏ否相同;String.equals()Ҏ会返回true如果被操作的String对象的内容相同。对于上面的代码会有s1==s2Q因为s1和s2两个引用指向同一个对象,对于上面的代码,s3.equals(s4)会返回true因ؓ两个对象的内定w一样ؓ”hello”。你可以从上囄U机制。在q里有三个独立的包含了相同的内容Q?#8221;hello”Q的对象Q实际上我们不需要这么三个独立的对象――因q行它们的话既浪Ҏ间又费内存?/font></div> <div minmax_bound="true"><font size="2" minmax_bound="true"> </font></div> <div minmax_bound="true"><font size="2" minmax_bound="true">  那么怎样才能保String对象不会重复呢?下一个话题会늛对于内徏String机制的兴?/font></div> <div minmax_bound="true"><font size="2" minmax_bound="true"> </font></div> <div minmax_bound="true"><font size="2" minmax_bound="true"><strong minmax_bound="true">滞留字符串的优化作用<br minmax_bound="true" /> </strong>  同一个字W串对象被重复地创徏是不必要的,String.intern()Ҏ可以避免q种情况。下图说明了String.intern()Ҏ是如何工作的QString.intern()Ҏ查字W串对象的存在性,如果需要的字符串对象已l存在,那么它会引用指向已l存在的字符串对象而不是重新创Z个。下图描l了使用了intern()Ҏ的字W串字面量和字符串对象的创徏情况?br minmax_bound="true" />  <br minmax_bound="true" /> <img style="width: auto; height: auto; minmaxwidth: auto; minmaxheight: auto; maxwidth: 450px" alt="" src="http://www.webdn.com/web_file/program/jsp/060208097/images/20060117201219_2.gif" minmax_bound="true" /></font></div> <div minmax_bound="true"><font size="2" minmax_bound="true">下面的例E帮助大家了解String.intern()Ҏ的重要性?br minmax_bound="true" /> StringTest2.java</font></div> <div minmax_bound="true"><font size="2" minmax_bound="true"> </font></div> <div minmax_bound="true"><font style="background-color: #c0c0c0" size="2" minmax_bound="true">package com.performance.string;</font></div> <div minmax_bound="true"><font style="background-color: #c0c0c0" size="2" minmax_bound="true">// This class shows the use of intern() method to improve performance<br minmax_bound="true" /> public class StringTest2 {</font></div> <div minmax_bound="true"><font style="background-color: #c0c0c0" size="2" minmax_bound="true">public static void main(String[] args){</font></div> <div minmax_bound="true"><font style="background-color: #c0c0c0" size="2" minmax_bound="true">    // create String references like s1,s2,s3...so on..<br minmax_bound="true" />     String variables[] = new String[50000];<br minmax_bound="true" />     for( int i=0;i         variables[i] = "s"+i;<br minmax_bound="true" />     }</font></div> <div minmax_bound="true"><font style="background-color: #c0c0c0" size="2" minmax_bound="true">    // create String literals<br minmax_bound="true" />     long startTime0 = System.currentTimeMillis();<br minmax_bound="true" />     for(int i=0;i         variables[i] = "hello";<br minmax_bound="true" />     }</font></div> <div minmax_bound="true"><font style="background-color: #c0c0c0" size="2" minmax_bound="true">    long endTime0 = System.currentTimeMillis();<br minmax_bound="true" />     System.out.println("Time taken for creation of String literals : "<br minmax_bound="true" />                          + (endTime0 - startTime0) + " milli seconds" );</font></div> <div minmax_bound="true"><font style="background-color: #c0c0c0" size="2" minmax_bound="true">    // create String objects using 'new' keyword        <br minmax_bound="true" />     long startTime1 = System.currentTimeMillis();<br minmax_bound="true" />     for(int i=0;i         variables[i] = new String("hello");<br minmax_bound="true" />     }</font></div> <div minmax_bound="true"><font style="background-color: #c0c0c0" size="2" minmax_bound="true">    long endTime1 = System.currentTimeMillis();<br minmax_bound="true" />     System.out.println("Time taken for creation of String objects with 'new' key word : " <br minmax_bound="true" />                         + (endTime1 - startTime1)+" milli seconds");</font></div> <div minmax_bound="true"><font style="background-color: #c0c0c0" size="2" minmax_bound="true">    // intern String objects with intern() method    <br minmax_bound="true" />     long startTime2 = System.currentTimeMillis();<br minmax_bound="true" />     for(int i=0;i         variables[i] = new String("hello");<br minmax_bound="true" />         variables[i] = variables[i].intern();<br minmax_bound="true" />     }</font></div> <div minmax_bound="true"><font style="background-color: #c0c0c0" size="2" minmax_bound="true">    long endTime2 = System.currentTimeMillis();<br minmax_bound="true" />     System.out.println("Time taken for creation of String objects with intern(): " <br minmax_bound="true" />                         + (endTime2 - startTime2)+" milli seconds");<br minmax_bound="true" />     }<br minmax_bound="true" /> }</font></div> <div minmax_bound="true"><font size="2" minmax_bound="true">q是上面那段代码的输出结果:<br minmax_bound="true" /> </font><font style="background-color: #c0c0c0" size="2" minmax_bound="true">Time taken for creation of String literals : 0 milli seconds<br minmax_bound="true" /> Time taken for creation of String objects with 'new' key word : 160 milli seconds<br minmax_bound="true" /> Time taken for creation of String objects with intern(): 60 milli seconds</font></div> <div minmax_bound="true"><font style="background-color: #c0c0c0" minmax_bound="true"></font><font size="2" minmax_bound="true"> </font></div> <div minmax_bound="true"><font size="2" minmax_bound="true"><strong minmax_bound="true">q接字符串时候的优化技?br minmax_bound="true" /> </strong>  你可以?操作W或者String.concat()或者StringBuffer.append(){办法来q接多个字符Ԍ那一U办法具有最佳的性能呢?</font></div> <div minmax_bound="true"><font size="2" minmax_bound="true">  如何作出选择取决于两U情景,W一U情景是需要连接的字符串是在编译期军_的还是在q行期决定的Q第二种情景是你使用的是StringBufferq是String。通常E序员会认ؓStringBuffer.append()Ҏ会优?操作W或String.concat()ҎQ但是在一些特定的情况下这个假x不成立的?/font></div> <div minmax_bound="true"><font size="2" minmax_bound="true"> </font></div> <div minmax_bound="true"><font size="2" minmax_bound="true">1) W一U情景:~译期决定相对于q行期决?br minmax_bound="true" /> L下面的StringTest3.java代码和输出结果?/font></div> <div minmax_bound="true"><br minmax_bound="true" /> <font style="background-color: #c0c0c0" size="2" minmax_bound="true">package com.performance.string;</font></div> <div minmax_bound="true"><font style="background-color: #c0c0c0" size="2" minmax_bound="true">/** This class shows the time taken by string concatenation at compile time and run time.*/<br minmax_bound="true" /> public class StringTest3 {</font></div> <div minmax_bound="true"><font style="background-color: #c0c0c0" size="2" minmax_bound="true">  public static void main(String[] args){<br minmax_bound="true" />     //Test the String Concatination<br minmax_bound="true" />     long startTime = System.currentTimeMillis();<br minmax_bound="true" />     for(int i=0;i<5000;i++){<br minmax_bound="true" />     String result = "This is"+ "testing the"+ "difference"+ "between"+ <br minmax_bound="true" />             "String"+ "and"+ "StringBuffer";<br minmax_bound="true" />     }</font></div> <div minmax_bound="true"><font style="background-color: #c0c0c0" size="2" minmax_bound="true">    long endTime = System.currentTimeMillis();<br minmax_bound="true" />     System.out.println("Time taken for string concatenation using + operator : " <br minmax_bound="true" />          + (endTime - startTime)+ " milli seconds");</font></div> <div minmax_bound="true"><font style="background-color: #c0c0c0" size="2" minmax_bound="true">    //Test the StringBuffer Concatination<br minmax_bound="true" />     long startTime1 = System.currentTimeMillis();<br minmax_bound="true" />     for(int i=0;i<5000;i++){<br minmax_bound="true" />     StringBuffer result = new StringBuffer();<br minmax_bound="true" />          result.append("This is");<br minmax_bound="true" />         result.append("testing the");<br minmax_bound="true" />         result.append("difference");<br minmax_bound="true" />         result.append("between");<br minmax_bound="true" />        result.append("String");<br minmax_bound="true" />        result.append("and");<br minmax_bound="true" />        result.append("StringBuffer");<br minmax_bound="true" />      }</font></div> <div minmax_bound="true"><font size="2" minmax_bound="true"><font style="background-color: #c0c0c0" minmax_bound="true">    long endTime1 = System.currentTimeMillis();<br minmax_bound="true" />     System.out.println("Time taken for String concatenation using StringBuffer : "<br minmax_bound="true" />            + (endTime1 - startTime1)+ " milli seconds");<br minmax_bound="true" />   }<br minmax_bound="true" /> }<br minmax_bound="true" /> </font>q是上面的代码的输出l果Q?br minmax_bound="true" /> </font><font size="2" minmax_bound="true"><font style="background-color: #c0c0c0" minmax_bound="true">Time taken for String concatenation using + operator : 0 milli seconds<br minmax_bound="true" /> Time taken for String concatenation using StringBuffer : 50 milli seconds</font><br minmax_bound="true" /> 很有地Q?操作W居然比StringBuffer.append()Ҏ要快Qؓ什么呢Q?/font></div> <div minmax_bound="true"><font size="2" minmax_bound="true"> </font></div> <div minmax_bound="true"><font size="2" minmax_bound="true">  q里~译器的优化起了关键作用Q编译器像下面D例的那样单地在编译期q接多个字符丌Ӏ它使用~译期决定取代运行期军_Q在你用new关键字来创徏String对象的时候也是如此?/font></div> <div minmax_bound="true"><font size="2" minmax_bound="true"> </font></div> <div minmax_bound="true"><font size="2" minmax_bound="true">~译前:<br minmax_bound="true" /> String result = "This is"+"testing the"+"difference"+"between"+"String"+"and"+"StringBuffer";<br minmax_bound="true" /> ~译后:<br minmax_bound="true" /> String result = "This is testing the difference between String and StringBuffer";</font></div> <div minmax_bound="true"><br minmax_bound="true" /> <font size="2" minmax_bound="true">q里String对象在编译期决定了而StringBuffer对象是在q行期决定的。运行期军_需要额外的开销当字W串的值无法预先知道的时候,~译期决定作用于字符串的值可以预先知道的时候,下面是一个例子?/font></div> <div minmax_bound="true"><font size="2" minmax_bound="true"> </font></div> <div minmax_bound="true"><font size="2" minmax_bound="true">~译前:<br minmax_bound="true" /> public String getString(String str1,String str2) {<br minmax_bound="true" />     return str1+str2;<br minmax_bound="true" /> }<br minmax_bound="true" /> ~译后:<br minmax_bound="true" /> return new StringBuffer().append(str1).append(str2).toString();<br minmax_bound="true" /> q行期决定需要更多的旉来运行?/font></div> <div minmax_bound="true"><font size="2" minmax_bound="true"> </font></div> <div minmax_bound="true"><font size="2" minmax_bound="true">2) W二U情景:使用StringBuffer取代String<br minmax_bound="true" /> 看看下面的代码你会发C情景一相反的结果――连接多个字W串的时候StringBuffer要比String快?br minmax_bound="true" /> StringTest4.java</font></div> <div minmax_bound="true"><font size="2" minmax_bound="true"> </font></div> <div minmax_bound="true"><font style="background-color: #c0c0c0" size="2" minmax_bound="true">package com.performance.string;</font></div> <div minmax_bound="true"><font style="background-color: #c0c0c0" size="2" minmax_bound="true">/** This class shows the time taken by string concatenation <br minmax_bound="true" /> using + operator and StringBuffer  */<br minmax_bound="true" /> public class StringTest4 {</font></div> <div minmax_bound="true"><font style="background-color: #c0c0c0" size="2" minmax_bound="true"> public static void main(String[] args){<br minmax_bound="true" />     //Test the String Concatenation using + operator<br minmax_bound="true" />     long startTime = System.currentTimeMillis();<br minmax_bound="true" />     String result = "hello";<br minmax_bound="true" />     for(int i=0;i<1500;i++){<br minmax_bound="true" />         result += "hello";<br minmax_bound="true" />     }</font></div> <div minmax_bound="true"><font style="background-color: #c0c0c0" size="2" minmax_bound="true">    long endTime = System.currentTimeMillis();<br minmax_bound="true" />     System.out.println("Time taken for string concatenation using + operator : "<br minmax_bound="true" />                   + (endTime - startTime)+ " milli seconds");</font></div> <div minmax_bound="true"><font style="background-color: #c0c0c0" size="2" minmax_bound="true">    //Test the String Concatenation using StringBuffer<br minmax_bound="true" />     long startTime1 = System.currentTimeMillis();<br minmax_bound="true" />     StringBuffer result1 = new StringBuffer("hello");<br minmax_bound="true" />     for(int i=0;i<1500;i++){<br minmax_bound="true" />         result1.append("hello");<br minmax_bound="true" />     }</font></div> <div minmax_bound="true"><font size="2" minmax_bound="true"><font style="background-color: #c0c0c0" minmax_bound="true">    long endTime1 = System.currentTimeMillis();<br minmax_bound="true" />     System.out.println("Time taken for string concatenation using StringBuffer :  " <br minmax_bound="true" />                   + (endTime1 - startTime1)+ " milli seconds");<br minmax_bound="true" />     }<br minmax_bound="true" /> }<br minmax_bound="true" /> </font>q是上面的代码的输出l果Q?br minmax_bound="true" /> </font><font style="background-color: #c0c0c0" size="2" minmax_bound="true">Time taken for string concatenation using + operator : 280 milli seconds<br minmax_bound="true" /> Time taken for String concatenation using StringBuffer : 0 milli seconds</font></div> <div minmax_bound="true"><font size="2" minmax_bound="true">看得出StringBuffer.append()Ҏ要比+操作W要快得多,Z么呢Q?/font></div> <div minmax_bound="true"><br minmax_bound="true" /> <font size="2" minmax_bound="true">  原因是两者都是在q行期决定字W串对象Q但?操作W用不同于StringBuffer.append()的规则通过String和StringBuffer来完成字W串q接操作。(译注Q什么样的规则呢Q)</font></div> <div minmax_bound="true"><font size="2" minmax_bound="true"> </font></div> <div minmax_bound="true"><font size="2" minmax_bound="true"><strong minmax_bound="true">借助StringBuffer的初始化q程的优化技?/strong><br minmax_bound="true" />   你可以通过StringBuffer的构造函数来讑֮它的初始化容量,q样可以明显地提升性能。这里提到的构造函数是StringBuffer(int length)Qlength参数表示当前的StringBuffer能保持的字符数量。你也可以用ensureCapacity(int minimumcapacity)Ҏ在StringBuffer对象创徏之后讄它的定w。首先我们看看StringBuffer的缺省行为,然后再找Z条更好的提升性能的途径?/font></div> <div minmax_bound="true"><font size="2" minmax_bound="true"> </font></div> <div minmax_bound="true"><font size="2" minmax_bound="true"><strong minmax_bound="true">StringBuffer的缺省行为:</strong><br minmax_bound="true" />   StringBuffer在内部维护一个字W数l,当你使用~省的构造函数来创徏StringBuffer对象的时候,因ؓ没有讄初始化字W长度,StringBuffer的容量被初始化ؓ16个字W,也就是说~省定w是16个字W。当StringBuffer辑ֈ最大容量的时候,它会自w容量增加到当前?倍再?Q也是Q?*旧?2Q?/font></div> <div minmax_bound="true"><font size="2" minmax_bound="true">  如果你用缺省|初始化之后接着往里面q加字符Q在你追加到W?6个字W的时候它会将定w增加?4Q?*16+2Q,当追加到34个字W的时候就会将定w增加?0Q?*34+2Q。无Z事只要StringBuffer到达它的最大容量它׃得不创徏一个新的字W数l然后重新将旧字W和新字W都拯一遍――这也太昂贵了点。所以LlStringBuffer讄一个合理的初始化容量值是错不了的Q这样会带来立竿见媄的性能增益?/font></div> <div minmax_bound="true"><font size="2" minmax_bound="true">  我利用两个StringBuffer重新试了上面的StringTest4.java代码Q一个未使用初始化容量D另一个用了。这ơ我q加?0000?#8217;hello’对象没有使用+操作W。区别是我用StringBuffer(250000)的构造函数来初始化第二个StringBuffer了?/font></div> <div minmax_bound="true"><font size="2" minmax_bound="true"> </font></div> <div minmax_bound="true"><font size="2" minmax_bound="true">输出l果如下Q?br minmax_bound="true" /> </font><font style="background-color: #c0c0c0" size="2" minmax_bound="true">Time taken for String concatenation using StringBuffer with out setting size: 280 milli seconds<br minmax_bound="true" /> Time taken for String concatenation using StringBuffer with setting size: 0 milli seconds</font></div> <div minmax_bound="true"><font size="2" minmax_bound="true">StringBuffer初始化过E的调整的作用由此可见一斑。所以,使用一个合适的定w值来初始化StringBuffer永远都是一个最佳的?/font></div> <div minmax_bound="true"><font size="2" minmax_bound="true"> </font></div> <div minmax_bound="true"><font size="2" minmax_bound="true"><strong minmax_bound="true">关键?/strong><br minmax_bound="true" /> 1. 无论何时只要可能的话使用字符串字面量来常见字W串而不是用new关键字来创徏字符丌Ӏ?br minmax_bound="true" /> 2. 无论何时当你要用new关键字来创徏很多内容重复的字W串的话Q请使用String.intern()Ҏ?br minmax_bound="true" /> 3. +操作W会为字W串q接提供最佳的性能――当字符串是在编译期军_的时候?br minmax_bound="true" /> 4. 如果字符串在q行期决定,使用一个合适的初期定w值初始化的StringBuffer会ؓ字符串连接提供最佳的性能?/font></div> <div minmax_bound="true"><font size="2" minmax_bound="true"></font></div> <div minmax_bound="true"><font size="2" minmax_bound="true"></font></div> <div minmax_bound="true"><font size="2" minmax_bound="true"><strong minmax_bound="true">String与StringBuffer的比?/strong><br minmax_bound="true" /> StringcL供了一些方法,用来q行字符串的比较。这个类实现了Object父类的equals()ҎQ用来比较两U字W串的值是否相{。同时还增加了equalsIgnoreCase()Ҏ可以忽略两个字符串大写的区别。下面是q两U方法的例子?br minmax_bound="true" /> 【例6-6?br minmax_bound="true" /> public class E6_6{<br minmax_bound="true" />  public static void main(String args[]) { <br minmax_bound="true" />   String s1="a";<br minmax_bound="true" />   String s2=new String("a");<br minmax_bound="true" />   String s3="A";<br minmax_bound="true" />   System.out.println(s1.equals(s2));<br minmax_bound="true" />   System.out.println(s1.equals(s3));<br minmax_bound="true" />   System.out.println(s1.equalsIgnoreCase(s3));<br minmax_bound="true" />  }<br minmax_bound="true" /> }<br minmax_bound="true" /> 上例的输出是<br minmax_bound="true" /> true<br minmax_bound="true" /> flase<br minmax_bound="true" /> true </font> <p minmax_bound="true"><font size="2" minmax_bound="true">但是StringBuffercdƈ没有实现ObjcetcȝEqualsҎQ所以不能用q个Ҏ来比较两个StringBuffercȝ字符串是否相{,如下例所C?br minmax_bound="true" /> 【例6-7?br minmax_bound="true" /> public class E6_7{<br minmax_bound="true" />  public static void main(String args[]) { <br minmax_bound="true" />   StringBuffer s1=new StringBuffer("a");<br minmax_bound="true" />   StringBuffer s2=new StringBuffer("a");<br minmax_bound="true" />   System.out.println(s1.equals(s2));<br minmax_bound="true" />   <br minmax_bound="true" />  }<br minmax_bound="true" /> }<br minmax_bound="true" /> E序输出Qfalse</font></p> <p minmax_bound="true"><font size="2" minmax_bound="true">除了用equalseҎ来比较两个字W串外,q可以用==来比较字W串。与equalseҎ不同的是Q?=不是比较两个字符串的值是否相{,而是比较几个字符串的引用是否指向同一个实例。如?-8所C?br minmax_bound="true" /> 【例6-8?br minmax_bound="true" /> public class E6_8{<br minmax_bound="true" />  public static void main(String args[]) { <br minmax_bound="true" />   String s1="a";<br minmax_bound="true" />   String s2="a";<br minmax_bound="true" />   String s3=new String("a");<br minmax_bound="true" />   String s4=new String("a");<br minmax_bound="true" />   System.out.println(s1==s2);<br minmax_bound="true" />   System.out.println(s3==s4);<br minmax_bound="true" />   System.out.println(s1==s3);<br minmax_bound="true" />   <br minmax_bound="true" />  }<br minmax_bound="true" />  <br minmax_bound="true" /> }<br minmax_bound="true" /> 上面的程序段输出Q?br minmax_bound="true" /> true<br minmax_bound="true" /> false<br minmax_bound="true" /> false<br minmax_bound="true" /> 与上例进行比较,不仅可以看出?=与equals的区别,q可以看到字面量的String的特D之外?br minmax_bound="true" /> 对于字面量的StringQ只要字W串的值是相等的,不论有多个引用都是指向同一块内存,不再另外分配I间。而用new关键字生成的实例则不同,每当用new实例化一ơ,分配该实例自q内存I间。上例的存储方式如图所C:</font></p> <p minmax_bound="true"><font size="2" minmax_bound="true"> <br minmax_bound="true" /> ?-1  s1、s2、s3、s4的区?br minmax_bound="true" /> 下面再通过另一个例子来看String和StringBuffer的区别?br minmax_bound="true" /> 【例6-9?br minmax_bound="true" /> public class E6_9{<br minmax_bound="true" />  public static void main(String args[]) { <br minmax_bound="true" />   String s1="a";<br minmax_bound="true" />   StringBuffer sb1=new StringBuffer("a");<br minmax_bound="true" />   <br minmax_bound="true" />   StringBuffer sb2=sb1;<br minmax_bound="true" />   String s2="a"+"b";<br minmax_bound="true" />   sb1.append("b");<br minmax_bound="true" />     <br minmax_bound="true" />   System.out.println(s1==s2);<br minmax_bound="true" />   System.out.println(sb1==sb2);<br minmax_bound="true" />   <br minmax_bound="true" />  } <br minmax_bound="true" /> }<br minmax_bound="true" /> 上例输出的是Q?br minmax_bound="true" /> flase<br minmax_bound="true" /> true<br minmax_bound="true" /> 上例可以证明q样的结论:String是不可变长的字符Ԍ对String做Q何的修改生成新的字W串Q而StringBuffer是可变长的字W串Q不论怎么更动q是同一个字W串?br minmax_bound="true" /> </font></p> </div> </div> <img src ="http://www.tkk7.com/tianmaoye/aggbug/208385.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/tianmaoye/" target="_blank">飞行?/a> 2008-06-16 17:27 <a href="http://www.tkk7.com/tianmaoye/articles/208385.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>jdk与jre的区?/title><link>http://www.tkk7.com/tianmaoye/articles/205617.html</link><dc:creator>飞行?/dc:creator><author>飞行?/author><pubDate>Tue, 03 Jun 2008 09:56:00 GMT</pubDate><guid>http://www.tkk7.com/tianmaoye/articles/205617.html</guid><wfw:comment>http://www.tkk7.com/tianmaoye/comments/205617.html</wfw:comment><comments>http://www.tkk7.com/tianmaoye/articles/205617.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.tkk7.com/tianmaoye/comments/commentRss/205617.html</wfw:commentRss><trackback:ping>http://www.tkk7.com/tianmaoye/services/trackbacks/205617.html</trackback:ping><description><![CDATA[<p>对于java初学者来_往往不懂区分jdk和jre的区别,实际上这两个东西差别很大的,有必要了解一下:</p> <p>单的说JDK是面向开发h员用的SDKQ它提供了Java的开发环境和q行环境。SDK是Software Development Kit 一般指软g开发包Q可以包括函数库、编译程序等?br />    JDK是Java Development Kit<br /> JRE是Java Runtime Enviroment是指Java的运行环境,是面向JavaE序的用者,而不是开发者?/p> <p>如果安装了JDKQ会发同你的电脑有两套JREQ一套位?\jre 另外一套位?C:\Program Files\Java\j2re1.4.1_01 目录下,后面q套比前面那套少了Server端的Java虚拟机,不过直接前面那套的Server端Java虚拟机复制过来就行了。而且在安装JDK可以选择是否安装q个位于 C:\Program Files\Jav a 目录下的JRE。如果你只安装JREQ而不是JDKQ那么只会在 C:\Program Files\Java 目录下安装唯一的一套JRE?<br /> <br />      JRE的地位就象一台PCZP我们写好的Win32应用E序需要操作系l帮我们q行Q同LQ我们编写的JavaE序也必要JRE才能q行。所以当你装完JDK后,如果分别在硬盘上的两个不同地方安装了两套JREQ那么你可以惌你的电脑有两台虚拟的Java PC机,都具有运行JavaE序的功能。所以我们可以说Q只要你的电脑安装了JREQ就可以正确q行Jav a应用E序?br /> <br />        1、ؓ什么Sun要让JDK安装两套相同的JREQ这是因为JDK里面有很多用Java所~写的开发工P如javac.exe、jar.exe{)Q而且都放|在 \lib\tools.jar 里。从下面例子可以看出Q先tools.jar改名为tools1.jarQ然后运行javac.exeQ显C如下结果: Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/tools/javac /Main q个意思是_你输入javac.exe与输?java -cp c:\jdk\lib\tools.jar com.sun.tools.javac.Main 是一LQ会得到相同的结果。从q里我们可以证明javac.exe只是一个包装器QWrapperQ,而制作的目的是ؓ了让开发者免于输入太长的指命。而且可以发现\lib目录下的E序都很,不大? 9KQ从q里我们可以得出一个结论。就是JDK里的工具几乎是用Java所~写Q所以也是Java应用E序Q因此要使用JDK所附的工具来开发JavaE序Q也必须要自行附一套JRE才行Q所以位于C:\Program Files\Java目录下的那套JRE是用来q行一般JavaE序用的?<br /> <br />       2、如果一台电脑安装两套以上的JREQ谁来决定呢Q这个重大Q务就落在java.exew上。Java.exe的工作就是找到合适的JRE来运行JavaE序?Java.exe依照底下的顺序来查找JREQ自q目录下有没有JREQ父目录有没有JREQ查询注册表Q?[HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment] 所以java.exe的运行结果与你的电脑里面哪个JRE被执行有很大的关pR?<br /> <br />      3、介lJVM JRE目录下的Bin目录有两个目录:server与client。这是真正的jvm.dll所在?jvm.dll无法单独工作Q当jvm.dll启动后,会用explicit的方法(是使用Win32 API之中的LoadLibrary()与GetProcAddress()来蝲入辅助用的动态链接库Q,而这些辅助用的动态链接库Q?dllQ都必须位于jvm.dll所在目录的父目录之中。因此想使用哪个JVMQ只需要设|PATHQ指向JRE所在目录底下的jvm.dll</p> <img src ="http://www.tkk7.com/tianmaoye/aggbug/205617.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/tianmaoye/" target="_blank">飞行?/a> 2008-06-03 17:56 <a href="http://www.tkk7.com/tianmaoye/articles/205617.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss> <footer> <div class="friendship-link"> <p>лǵվܻԴȤ</p> <a href="http://www.tkk7.com/" title="亚洲av成人片在线观看">亚洲av成人片在线观看</a> <div class="friend-links"> </div> </div> </footer> վ֩ģ壺 <a href="http://79909d.com" target="_blank">Ҹ24p</a>| <a href="http://ulihix.com" target="_blank">avһ㽶</a>| <a href="http://glhrsydc.com" target="_blank">ŮˬƵѲ</a>| <a href="http://dgyinhezy.com" target="_blank">椸ѹۿ</a>| <a href="http://448tk.com" target="_blank">޸Ů侫</a>| <a href="http://600c28.com" target="_blank">ҹϼӰԺ</a>| <a href="http://69ct.com" target="_blank">ձƷþþþþþþ</a>| <a href="http://diswooo.com" target="_blank">av붫˵</a>| <a href="http://dqzlxgg.com" target="_blank">һƵ </a>| <a href="http://yqstickers.com" target="_blank">91Ѹվ</a>| <a href="http://yixinbanks.com" target="_blank">AV</a>| <a href="http://5222133.com" target="_blank">һɫaƬþëƬ</a>| <a href="http://hzczj.com" target="_blank">Ƶ</a>| <a href="http://xuanboart.com" target="_blank">йavƬ</a>| <a href="http://qimiaodh.com" target="_blank">˳ɵӰ߲ </a>| <a href="http://j8j8x.com" target="_blank">Ʒר߹ۿ</a>| <a href="http://txtmp3.com" target="_blank">žžžƷƵ</a>| <a href="http://91ttvv.com" target="_blank">AV뾫Ʒվ߹ۿ</a>| <a href="http://155lh.com" target="_blank">߾Ʒ </a>| <a href="http://sdzhly.com" target="_blank">Ʒ޾Ʒպͼ</a>| <a href="http://tmg-beelen.com" target="_blank">ѳҹƵ</a>| <a href="http://jj5c.com" target="_blank">aëƬëƬѹۿó</a>| <a href="http://www-64000.com" target="_blank">˶վ</a>| <a href="http://yaboxxx125.com" target="_blank">һAV</a>| <a href="http://tiantiantegou.com" target="_blank">ɫ͵͵</a>| <a href="http://mcsser.com" target="_blank">Ļר</a>| <a href="http://jpsp8.com" target="_blank">ƵƷѹۿ99</a>| <a href="http://cqyouyongpx.com" target="_blank">ŷһ </a>| <a href="http://by2988.com" target="_blank">߹ۿƬ˳Ƶ </a>| <a href="http://4001120002.com" target="_blank">޲Ƶ߹ۿ</a>| <a href="http://dddd20.com" target="_blank">լ666ѹۿ</a>| <a href="http://xbooktxt.com" target="_blank">ƷƵۿ</a>| <a href="http://wwyw99977.com" target="_blank">޸һ</a>| <a href="http://srvz83.com" target="_blank">žžƵ</a>| <a href="http://www1688mz.com" target="_blank">㻨Ƶۿ</a>| <a href="http://8mav1007.com" target="_blank">ۺϾþþƷɫ</a>| <a href="http://https357171.com" target="_blank">AV߲պŷ</a>| <a href="http://91packing.com" target="_blank">91ѹ߹ۿ</a>| <a href="http://ss8kk.com" target="_blank">ѾþþƷ99þ</a>| <a href="http://bjycxy88.com" target="_blank">xxxx18</a>| <a href="http://yw8885.com" target="_blank">VAĻëƬ</a>| <script> (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })(); </script> </body>