﻿<?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-bill</title><link>http://www.blogjava.net/Bill-Xie/</link><description /><language>zh-cn</language><lastBuildDate>Sun, 12 Apr 2026 19:04:04 GMT</lastBuildDate><pubDate>Sun, 12 Apr 2026 19:04:04 GMT</pubDate><ttl>60</ttl><item><title>string和StringBuffer的区别</title><link>http://www.blogjava.net/Bill-Xie/archive/2010/08/05/328058.html</link><dc:creator>Bill_Xie</dc:creator><author>Bill_Xie</author><pubDate>Thu, 05 Aug 2010 08:32:00 GMT</pubDate><guid>http://www.blogjava.net/Bill-Xie/archive/2010/08/05/328058.html</guid><wfw:comment>http://www.blogjava.net/Bill-Xie/comments/328058.html</wfw:comment><comments>http://www.blogjava.net/Bill-Xie/archive/2010/08/05/328058.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/Bill-Xie/comments/commentRss/328058.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/Bill-Xie/services/trackbacks/328058.html</trackback:ping><description><![CDATA[<span style="font-family: 宋体; font-size: 13px; color: #555555; line-height: 20px; ">
<p style="line-height: normal; ">如果你在网上一搜，会有非常多的答案，其中最多的就是String是不可变而StringBuffer是可变的，但是这可变与不可变究竟是什么意思呢？如果你能用IDE进行debug的话，你就会发现，String实例化以后所有的属性都是final的，而StringBuffer确不是，这就是可变与不可变。下面引用SCJP的试题来解释一下这个例子：</p>
<div style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; ">java 代码</div>
<div style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; ">
<ol style="line-height: normal; ">
    <li style="line-height: normal; "><span style="line-height: normal; "><span style="line-height: normal; "><strong style="line-height: normal; "><font color="#7f0055" style="line-height: normal; ">public</font></strong></span>&nbsp;<span style="line-height: normal; "><strong style="line-height: normal; "><font color="#7f0055" style="line-height: normal; ">class</font></strong></span><span style="line-height: normal; ">&nbsp;Test { &nbsp;&nbsp;</span></span></li>
    <li style="line-height: normal; "><span style="line-height: normal; ">&nbsp;&nbsp;&nbsp;</span><span style="line-height: normal; "><strong style="line-height: normal; "><font color="#7f0055" style="line-height: normal; ">public</font></strong></span>&nbsp;<span style="line-height: normal; "><strong style="line-height: normal; "><font color="#7f0055" style="line-height: normal; ">static</font></strong></span>&nbsp;<span style="line-height: normal; "><strong style="line-height: normal; "><font color="#7f0055" style="line-height: normal; ">void</font></strong></span><span style="line-height: normal; ">&nbsp;stringReplace (String text) { &nbsp;&nbsp;</span></li>
    <li style="line-height: normal; "><span style="line-height: normal; ">&nbsp;&nbsp;&nbsp; text = text.replace('j' , 'i'); &nbsp;&nbsp;</span></li>
    <li style="line-height: normal; "><span style="line-height: normal; ">&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;</span></li>
    <li style="line-height: normal; "><span style="line-height: normal; ">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;</span></li>
    <li style="line-height: normal; "><span style="line-height: normal; ">&nbsp;&nbsp;&nbsp;</span><span style="line-height: normal; "><strong style="line-height: normal; "><font color="#7f0055" style="line-height: normal; ">public</font></strong></span>&nbsp;<span style="line-height: normal; "><strong style="line-height: normal; "><font color="#7f0055" style="line-height: normal; ">static</font></strong></span>&nbsp;<span style="line-height: normal; "><strong style="line-height: normal; "><font color="#7f0055" style="line-height: normal; ">void</font></strong></span><span style="line-height: normal; ">&nbsp;bufferReplace (StringBuffer text) { &nbsp;&nbsp;</span></li>
    <li style="line-height: normal; "><span style="line-height: normal; ">&nbsp;&nbsp;&nbsp; text = text.append(</span><span style="line-height: normal; "><font color="#0000ff" style="line-height: normal; ">"C"</font></span><span style="line-height: normal; ">); &nbsp;&nbsp;</span></li>
    <li style="line-height: normal; "><span style="line-height: normal; ">&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;</span></li>
    <li style="line-height: normal; "><span style="line-height: normal; ">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;</span></li>
    <li style="line-height: normal; "><span style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="line-height: normal; "><strong style="line-height: normal; "><font color="#7f0055" style="line-height: normal; ">public</font></strong></span>&nbsp;<span style="line-height: normal; "><strong style="line-height: normal; "><font color="#7f0055" style="line-height: normal; ">static</font></strong></span>&nbsp;<span style="line-height: normal; "><strong style="line-height: normal; "><font color="#7f0055" style="line-height: normal; ">void</font></strong></span><span style="line-height: normal; ">&nbsp;main (String args[]) { &nbsp;&nbsp;</span></li>
    <li style="line-height: normal; "><span style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp; String textString =&nbsp;</span><span style="line-height: normal; "><strong style="line-height: normal; "><font color="#7f0055" style="line-height: normal; ">new</font></strong></span><span style="line-height: normal; ">&nbsp;String (</span><span style="line-height: normal; "><font color="#0000ff" style="line-height: normal; ">"java"</font></span><span style="line-height: normal; ">); &nbsp;&nbsp;</span></li>
    <li style="line-height: normal; "><span style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp; StringBuffer textBuffer =&nbsp;</span><span style="line-height: normal; "><strong style="line-height: normal; "><font color="#7f0055" style="line-height: normal; ">new</font></strong></span><span style="line-height: normal; ">&nbsp;StringBuffer (</span><span style="line-height: normal; "><font color="#0000ff" style="line-height: normal; ">"java"</font></span><span style="line-height: normal; ">); &nbsp;&nbsp;</span></li>
    <li style="line-height: normal; "><span style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;</span></li>
    <li style="line-height: normal; "><span style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp; stringReplace (textString); &nbsp;&nbsp;</span></li>
    <li style="line-height: normal; "><span style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp; bufferReplace (textBuffer); &nbsp;&nbsp;</span></li>
    <li style="line-height: normal; "><span style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;</span></li>
    <li style="line-height: normal; "><span style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp; System.out.println (textString + textBuffer); &nbsp;&nbsp;</span></li>
    <li style="line-height: normal; "><span style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;</span></li>
    <li style="line-height: normal; "><span style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;</span></li>
</ol>
</div>
<p style="line-height: normal; ">答案是 javajavaC</p>
<p style="line-height: normal; ">这是因为第七行text = text.append ("C")，append方法会改变text中的值,而text与textBuffer指向的地址是相同的。因此会打印javaC</p>
<p style="line-height: normal; ">再举个例子：</p>
<p style="line-height: normal; ">String a = "a"; //假设a指向地址0x0001</p>
<p style="line-height: normal; ">a = "b";//重新赋值后a指向地址0x0002，但0x0001地址中保存的"a"依旧存在，但已经不再是a所指向的。</p>
<p style="line-height: normal; ">因此String的操作都是改变赋值地址而不是改变值操作</p>
</span>
<img src ="http://www.blogjava.net/Bill-Xie/aggbug/328058.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/Bill-Xie/" target="_blank">Bill_Xie</a> 2010-08-05 16:32 <a href="http://www.blogjava.net/Bill-Xie/archive/2010/08/05/328058.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>tomcat的用户配置</title><link>http://www.blogjava.net/Bill-Xie/archive/2010/07/09/325673.html</link><dc:creator>Bill_Xie</dc:creator><author>Bill_Xie</author><pubDate>Fri, 09 Jul 2010 08:50:00 GMT</pubDate><guid>http://www.blogjava.net/Bill-Xie/archive/2010/07/09/325673.html</guid><wfw:comment>http://www.blogjava.net/Bill-Xie/comments/325673.html</wfw:comment><comments>http://www.blogjava.net/Bill-Xie/archive/2010/07/09/325673.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/Bill-Xie/comments/commentRss/325673.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/Bill-Xie/services/trackbacks/325673.html</trackback:ping><description><![CDATA[<div>&lt;tomcat-users&gt;</div>
<div>&lt;role rolename="manager"/&gt;</div>
<div>&nbsp;&nbsp;&lt;role rolename="admin"/&gt;</div>
<div>&nbsp;&nbsp;&lt;user username="admin" password="admin" roles="admin,manager"/&gt;</div>
<div>&lt;/tomcat-users&gt;</div>
<img src ="http://www.blogjava.net/Bill-Xie/aggbug/325673.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/Bill-Xie/" target="_blank">Bill_Xie</a> 2010-07-09 16:50 <a href="http://www.blogjava.net/Bill-Xie/archive/2010/07/09/325673.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>