??xml version="1.0" encoding="utf-8" standalone="yes"?>亚洲精品99久久久久,久久蜜桃香蕉精品一区二区三区,涩涩视频在线观看免费http://www.aygfsteel.com/xiexin80/archive/2009/02/13/254494.html天长天长Fri, 13 Feb 2009 00:40:00 GMThttp://www.aygfsteel.com/xiexin80/archive/2009/02/13/254494.htmlhttp://www.aygfsteel.com/xiexin80/comments/254494.htmlhttp://www.aygfsteel.com/xiexin80/archive/2009/02/13/254494.html#Feedback0http://www.aygfsteel.com/xiexin80/comments/commentRss/254494.htmlhttp://www.aygfsteel.com/xiexin80/services/trackbacks/254494.htmlIoC   :   Inversion of Control

spring是通过依赖注入(Dependency Injection )实现的IoC

IoC容器

   *   必须被理的对象定义到spring配置文g?br />    *   必须定义constructor或者setterҎ(gu)Q让spring对象注入进?br />
AOP  :  Aspect Oriented Programming

spring带来?jin)一U编E方式,面向切面的编E?br />
AOP是一个概?br />
在一个程序中分离一个功能,q种功能的实现是与程序不相关的类?br /> 同时能够使很多类׃nq个功能?br />
x(chng)是他的主要点Q要x(chng)某个功能Q要x(chng)切入炏V?br />
实现AOP?点:(x)
1、PointcutQ切入点Q是一个范?--表达?br /> 2、AdviceQ具体实玎ͼ(j)功能攑ֈ那个Ҏ(gu)



spring对hibernate的支?br />
注入SessionFactory到spring配置文g?br />
<Bean id="SessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactory">
        <property nema="configLocation">
                <value>classpath:hibernate.cfg.xml</value>
        </property>
</Bean>



天长 2009-02-13 08:40 发表评论
]]>
mysql中文Q?/title><link>http://www.aygfsteel.com/xiexin80/archive/2009/01/20/251987.html</link><dc:creator>天长</dc:creator><author>天长</author><pubDate>Mon, 19 Jan 2009 23:19:00 GMT</pubDate><guid>http://www.aygfsteel.com/xiexin80/archive/2009/01/20/251987.html</guid><wfw:comment>http://www.aygfsteel.com/xiexin80/comments/251987.html</wfw:comment><comments>http://www.aygfsteel.com/xiexin80/archive/2009/01/20/251987.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/xiexin80/comments/commentRss/251987.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/xiexin80/services/trackbacks/251987.html</trackback:ping><description><![CDATA[在库中徏表时语句? create table tbname(..........)engine=MyISAM character set gbk collate gbk_chinese_ci;<br /> <br /> <br /> E序中连接数据库的Connection对象需要写成Connection con = DriverManaager.getConnection("jdbc:mysql://...user=..&password=...&useUnicode=true&characterEncoding=gbk"); <br /> 若在l端下用mysql命o(h)向数据库插入数据,则在q入mysql时的命o(h)写成:#mysql --default-character-set=gbk -u ... -p <br /> <br /> <p>在网上看q很多解决mysqlq的帖子,可是写的不是|里|嗦p于复杂。其实ؕ码没那么复杂Q是|上的大侠们惛_?jin)。我研究q一D|子,ȝZ套自q解决Ҏ(gu)Q如果你q没解决qq我的Ҏ(gu)试一下。我的方案不是最好的Q却是最单易懂的。不信你试试?/p> <p>在用MYSQLӞ插入中文字符Q经怼(x)出现qQ中文全被用?代替。出现这U情늚原因Q多是字W集不匹配造成的?br /> 在MYSQL中,如果使用~省的字W集Q在建库、徏表时Q默认用的是latin1字符集,为ISO 8859-1西欧字符集。插入中文字W时Q与之不匚wQ就?x)出Cؕ码?/p> <p>要解x(chng)问题Q就必须手动数据库Server和Client的字W编码改为gb2312。配|方法如下:(x)<br /> 打开MYSQL安装目录下的my.ini文gQ找到如下段落:(x)</p> <p># CLIENT SECTION<br /> # ----------------------------------------------------------------------<br /> #<br /> # The following options will be read by MySQL client applications.<br /> # Note that only client applications shipped by MySQL are guaranteed<br /> # to read this section. If you want your own MySQL client program to<br /> # honor these values, you need to specify it as an option during the<br /> # MySQL client library initialization.<br /> #<br /> [client]</p> <p>port=<font color="#ff0000">3306</font></p> <p>[mysql]</p> <p><font color="#ff0000">default-character-set=latin1</font></p> <p><br /> # SERVER SECTION<br /> # ----------------------------------------------------------------------<br /> #<br /> # The following options will be read by the MySQL Server. Make sure that<br /> # you have installed the server correctly (see above) so it reads this <br /> # file.<br /> #<br /> [mysqld]</p> <p># The TCP/IP Port the MySQL Server will listen on<br /> port=3306</p> <p>#Path to installation directory. All paths are usually resolved relative to this.<br /> basedir="D:/MySQL/MySQL Server 5.0/"</p> <p>#Path to the database root<br /> datadir="D:/MySQL/MySQL Server 5.0/Data/"</p> <p># The default character set that will be used when a new schema or table is<br /> # created and no character set is defined<br /> <font color="#ff0000">default-character-set=latin1</font></p> <p>其中的<font color="#ff0000">default-character-set=latin1</font>改ؓ(f)<font color="#ff0000">default-character-set=gb2312( 两个都改)</font>,然后重启MYSQL(特别注意Q以前徏立的数据库要重徏Q因Z前存储的数据~码方式为ISO-8859-1),q行MySQL Command Line ClientQ?br /> 输入show variables like 'character_set_%';可以查看数据库的字符~码如下Q?/p> <p>mysql> show variables like 'character_set_%';<br /> +--------------------------+-----------------------------------------+<br /> | Variable_name             | Value                                     |<br /> +--------------------------+-----------------------------------------+<br /> | character_set_client      | gb2312                                   |<br /> | character_set_connection | gb2312                                |<br /> | character_set_database    | gb2312                                |<br /> | character_set_filesystem | binary                                   |<br /> | character_set_results     | gb2312                                   |<br /> | character_set_server      | gb2312                                  |<br /> | character_set_system      | utf8                                     |<br /> | character_sets_dir        | D:\MySQL\MySQL Server 5.0\share\charsets|<br /> +--------------------------+-----------------------------------------+</p> <p>然后Q在E序中将q接数据库的URL改ؓ(f)jdbc:mysql://localhost:3306/databasename?useUnicode=true<font color="#ff0000">&amp;</font>characterEncoding=gb2312   可以了(jin)Q?strong><font color="#ff6600">Q?amp;ampQ是代表xml中的&Q?/font></strong></p> <p>q需要注意到是:(x)1.你的JSP面一定别忘(sh)(jin)加上~码方式<font color="#ff0000"><%@ page pageEncoding="GB2312"%>?/font></p> <p>2.把以前写qo(h)Ҏ(gu)?getbytesQiso-8859-1Q全L</p> <p><font color="#ff0000" size="5">最后记住,一定要重新导入或徏立数据库Q!Q?/font></p> <img src ="http://www.aygfsteel.com/xiexin80/aggbug/251987.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/xiexin80/" target="_blank">天长</a> 2009-01-20 07:19 <a href="http://www.aygfsteel.com/xiexin80/archive/2009/01/20/251987.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Ҏ(gu)的重写引发的问题http://www.aygfsteel.com/xiexin80/archive/2009/01/01/249472.html天长天长Thu, 01 Jan 2009 12:10:00 GMThttp://www.aygfsteel.com/xiexin80/archive/2009/01/01/249472.htmlhttp://www.aygfsteel.com/xiexin80/comments/249472.htmlhttp://www.aygfsteel.com/xiexin80/archive/2009/01/01/249472.html#Feedback0http://www.aygfsteel.com/xiexin80/comments/commentRss/249472.htmlhttp://www.aygfsteel.com/xiexin80/services/trackbacks/249472.html 今天深深的明白了(jin)q个道理?br /> 今天写了(jin)个strutsl习(fn)项目?br /> 写个控制器的c要l承Actioncd写executeQ)(j)
public ActionForward execute(
       ActionMapping mapping,
       ActionForm form,
       HttpServletRequest request
       HttpServletResponse response) throws Exception {
  
return null;
}
׃我是手写的,l果形参的顺序没写对?br /> 恶心(j)?jin)我一下午?br /> q有异常的方法也要一栗?

天长 2009-01-01 20:10 发表评论
]]>
Java中的模式 --单态-多线E下的处理方?/title><link>http://www.aygfsteel.com/xiexin80/archive/2008/12/31/249338.html</link><dc:creator>天长</dc:creator><author>天长</author><pubDate>Wed, 31 Dec 2008 07:13:00 GMT</pubDate><guid>http://www.aygfsteel.com/xiexin80/archive/2008/12/31/249338.html</guid><wfw:comment>http://www.aygfsteel.com/xiexin80/comments/249338.html</wfw:comment><comments>http://www.aygfsteel.com/xiexin80/archive/2008/12/31/249338.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/xiexin80/comments/commentRss/249338.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/xiexin80/services/trackbacks/249338.html</trackback:ping><description><![CDATA[<p>单态定?<br /> Singleton模式主要作用是保证在Java应用E序中,一个类Class只有一个实例存在?/p> <p>Singleton模式׃ؓ(f)我们提供?jin)这样实现的可能。用Singleton的好处还在于可以节省内存Q因为它限制?br /> 实例的个敎ͼ有利于Java垃圾回收Qgarbage collectionQ?/p> <p>使用Singleton注意事项Q?br /> 有时在某些情况下Q用Singletonq不能达到Singleton的目的,如有多个Singleton对象同时被不同的c?br /> 装入器装载;在EJBq样的分布式pȝ中用也要注意这U情况,因ؓ(f)EJB是跨服务器,跨JVM?/p> <p>单态模式的演化Q?br /> 单态模式是个简单的模式Q但是这个简单的模式也有很多复杂的东ѝ?/p> <p>一Q首先最单的单态模式,单态模?<br /> import java.util.*;<br /> class Singleton<br /> {<br />   private static Singleton instance;<br />   private Vector v;<br />   private boolean inUse;</p> <p>  private Singleton()<br />   {<br />     v = new Vector();<br />     v.addElement(new Object());<br />     inUse = true;<br />   }</p> <p>  public static Singleton getInstance()<br />   {<br />     if (instance == null)          //1<br />       instance = new Singleton();  //2<br />     return instance;               //3<br />   }<br /> }<br /> q个单态模式是不安全的Qؓ(f)什么说?Q因为没考虑多线E,如下情况<br /> Thread 1 调用getInstance() Ҏ(gu)Qƈ且判断instance是nullQ然後进入if模块Q?br /> 在实例化instance之前Q?br /> Thread 2抢占?jin)Thread 1的cpu<br /> Thread 2 调用getInstance() Ҏ(gu)Qƈ且判断instance是nullQ然後进入if模块Q?br /> Thread 2 实例化instance 完成Q返?br /> Thread 1 再次实例化instance<br /> q个单态已l不在是单?/p> <p>二,Z(jin)解决刚才的问题:(x)单态模?<br /> public static synchronized Singleton getInstance()<br /> {<br />   if (instance == null)          //1<br />     instance = new Singleton();  //2<br />   return instance;               //3<br /> }<br /> 采用同步来解冻Iq种方式解决?jin)问题,但是仔细分?br /> 正常的情况下只有W一ơ时候,q入对象的实例化Q须要同步,<br /> 其它时候都是直接返回已l实例化好的instance不须要同步,<br /> 大家都知到在一个多U程的程序中Q如果同步的消耗是很大的,很容易造成瓉</p> <p>三,Z(jin)解决上边的问题:(x)单态模?Q加入同?br /> public static Singleton getInstance()<br /> {<br />   if (instance == null)<br />   {<br />     synchronized(Singleton.class) {<br />       instance = new Singleton();<br />     }<br />   }<br />   return instance;<br /> }<br /> 同步Ҏ(gu)块同步,而不使用函数同步Q但是仔l分析,<br /> 又回C(jin)模式一的状态,再多U程的时候根本没有解决问?/p> <p>四,Z(jin)对应上边的问题:(x)单态模?Q也是很多人采用的Double-checked locking<br /> public static Singleton getInstance()<br /> {<br />   if (instance == null)<br />   {<br />     synchronized(Singleton.class) {  //1<br />       if (instance == null)          //2<br />         instance = new Singleton();  //3<br />     }<br />   }<br />   return instance;<br /> }<br /> q样Q模式一中提到的问题解决?jin)。不?x)出现多ơ实例化的现?br /> 当第一ơ进入的时候,保正实例化时候的单?在实例化后,多线E访问的时候直接返回,不须要进入同步模块,<br /> 既实C(jin)单态,又没有损失性能。表面上看我们的问题解决?jin),但是再仔l分析:(x)<br /> 我们来假象这中情况:(x)<br /> Thread 1 :q入?/3位置Q执行new Singleton()Q但是在构造函数刚刚开始的时候被Thread2抢占cpu<br /> Thread 2 :q入getInstance()Q判断instance不等于null,q回instanceQ?br /> Qinstance已经被newQ已l分配了(jin)内存I间,但是没有初始化数?<br /> Thread 2 :利用q回的instance做某些操做,p|或者异?br /> Thread 1 :取得cpu初始化完?br /> q程中可能有多个U程取到?jin)没有完成的实例Qƈ用这个实例作出某些操做?br /> Q-Q-Q-Q-Q-Q-Q-Q-Q-Q-Q-Q-Q-Q-Q-Q-Q-Q-Q-Q-Q?br /> 出现以上的问题是因ؓ(f)<br /> mem = allocate();             //分配内存<br /> instance = mem;               //标记instance非空<br />                               //未执行构造函?thread 2从这里进?br /> ctorSingleton(instance);      //执行构造函?br />                               //q回instance<br /> Q-Q-Q-Q-Q-Q-Q-Q-Q-Q-Q-Q-Q-Q-Q-Q-Q-Q-Q-Q-Q-                              </p> <p>五,证明上边的假x(chng)可能发生的,字节码是用来分析问题的最好的工具Q可以利用它来分?br /> 下边一D늨序:(x)Qؓ(f)?jin)分析方便,所以渐了(jin)内容Q?br /> 字节码的使用Ҏ(gu)见这里,<a href="http://www.aygfsteel.com/dreamstone/archive/2006/11/04/79058.html"><font color="#000080">利用字节码分析问?/font></a><br /> class Singleton<br /> {<br />   private static Singleton instance;<br />   private boolean inUse;<br />   private int val;  </p> <p>  private Singleton()<br />   {<br />     inUse = true;<br />     val = 5;<br />   }<br />   public static Singleton getInstance()<br />   {<br />     if (instance == null)<br />       instance = new Singleton();<br />     return instance;<br />   }<br /> }<br /> 得到的字节码                            <br /> ;asm code generated for getInstance<br /> 054D20B0   mov         eax,[049388C8]      ;load instance ref<br /> 054D20B5   test        eax,eax             ;test for null<br /> 054D20B7   jne         054D20D7<br /> 054D20B9   mov         eax,14C0988h<br /> 054D20BE   call        503EF8F0            ;allocate memory<br /> 054D20C3   mov         [049388C8],eax      ;store pointer in <br />                                            ;instance ref. instance  <br />                                            ;non-null and ctor<br />                                            ;has not run<br /> 054D20C8   mov         ecx,dword ptr [eax] <br /> 054D20CA   mov         dword ptr [ecx],1   ;inline ctor - inUse=true;<br /> 054D20D0   mov         dword ptr [ecx+4],5 ;inline ctor - val=5;<br /> 054D20D7   mov         ebx,dword ptr ds:[49388C8h]<br /> 054D20DD   jmp         054D20B0</p> <p>上边的字节码证明Q猜x(chng)有可能实现的</p> <p>六:(x)好了(jin)Q上边证明Double-checked locking可能出现取出错误数据的情况,那么我们q是可以解决?br /> public static Singleton getInstance()<br /> {<br />   if (instance == null)<br />   {<br />     synchronized(Singleton.class) {      //1<br />       Singleton inst = instance;         //2<br />       if (inst == null)<br />       {<br />         synchronized(Singleton.class) {  //3<br />           inst = new Singleton();        //4<br />         }<br />         instance = inst;                 //5<br />       }<br />     }<br />   }<br />   return instance;<br /> }<br /> 利用Double-checked locking 两次同步Q中间变?解决上边的问题?br /> Q下边这D话我只能简单的理解Q翻译过来不好,所以保留原文,list 7是上边的代码Qlist 8是下边的<br /> The code in Listing 7 doesn't work because of the current definition of the memory model.<br />  The Java Language Specification (JLS) demands that code within a synchronized block <br />  not be moved out of a synchronized block. However, it does not say that <br />  code not in a synchronized block cannot be moved into a synchronized block.<br /> A JIT compiler would see an optimization opportunity here. <br /> This optimization would remove the code at <br /> //4 and the code at //5, combine it and generate the code shown in Listing 8:Q?br /> ------------------------------------------------- <br /> list 8<br /> public static Singleton getInstance()<br /> {<br />   if (instance == null)<br />   {<br />     synchronized(Singleton.class) {      //1<br />       Singleton inst = instance;         //2<br />       if (inst == null)<br />       {<br />         synchronized(Singleton.class) {  //3<br />           //inst = new Singleton();      //4<br />           instance = new Singleton();               <br />         }<br />         //instance = inst;               //5<br />       }<br />     }<br />   }<br />   return instance;<br /> }<br /> If this optimization takes place, you have the same out-of-order write problem we discussed earlier.<br /> 如果q个优化发生Q将再次发生上边提到的问题,取得没有实例化完成的数据?br /> -------------------------------------------------</p> <p>以下部分Z(jin)避免我翻译错误误导打Ӟ保留原文</p> <p>Another idea is to use the keyword volatile for the variables inst and instance. <br /> According to the JLS (see Resources), variables declared volatile are supposed to <br /> be sequentially consistent, and therefore, not reordered. <br /> But two problems occur with trying to use volatile to fix the problem with <br /> double-checked locking:</p> <p>The problem here is not with sequential consistency. <br /> Code is being moved, not reordered.</p> <p>Many JVMs do not implement volatile correctly regarding sequential consistency anyway. <br /> The second point is worth expanding upon. Consider the code in Listing 9:</p> <p>Listing 9. Sequential consistency with volatile</p> <p>class test<br /> {<br />   private volatile boolean stop = false;<br />   private volatile int num = 0;</p> <p>  public void foo()<br />   {<br />     num = 100;    //This can happen second<br />     stop = true;  //This can happen first<br />     //...<br />   }</p> <p>  public void bar()<br />   {<br />     if (stop)<br />       num += num;  //num can == 0!<br />   }<br />   //...<br /> }<br />  <br /> According to the JLS, because stop and num are declared volatile, <br /> they should be sequentially consistent. This means that if stop is ever true, <br /> num must have been set to 100. <br /> However, because many JVMs do not implement the sequential consistency feature of volatile,<br /> you cannot count on this behavior. <br /> Therefore, if thread 1 called foo and thread 2 called bar concurrently, <br /> thread 1 might set stop to true before num is set to 100. <br /> This could lead thread 2 to see stop as true, but num still set to 0. <br /> There are additional problems with volatile and the atomicity of 64-bit variables,<br /> but this is beyond the scope of this article. <br /> See Resources for more information on this topic. </p> <p>单的理解上边q段话,使用volatile有可能能解决问题Qvolatile被定义用来保正一致性,但是很多虚拟?br /> q没有很好的实现volatileQ所以用它也会(x)存在问题?/p> <p>最l的解决Ҏ(gu)Q?br />  Q?Q,单态模?Q用同步方?br />  Q?Q,攑ּ同步Q用一个静(rn)态变量,如下<br /> class Singleton<br /> {<br />   private Vector v;<br />   private boolean inUse;<br />   private static Singleton instance = new Singleton();</p> <p>  private Singleton()<br />   {<br />     v = new Vector();<br />     inUse = true;<br />     //...<br />   }</p> <p>  public static Singleton getInstance()<br />   {<br />     return instance;<br />   }<br /> }<br /> 但用静(rn)态变量也?x)存在问题,问题?<a ><font color="#000080">q篇文章</font></a></p> <p>而且如在文章开头提到的Q用EJB跨服务器Q跨JVM的情况下Q单态更是问?/p> <p>好了(jin)是不是感觉单态模式根本没法用了(jin)Q其实上辚w是特D情况,q中Ҏ(gu)情况的出现是有条件的Q只?br /> Ҏ(gu)你的具体应用Q回避一些,p解决问题Q所以单态还是可以用的。但是在使用前慎重,自己考虑好自?br /> 的情况适合哪种情况?nbsp;</p> <img src ="http://www.aygfsteel.com/xiexin80/aggbug/249338.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/xiexin80/" target="_blank">天长</a> 2008-12-31 15:13 <a href="http://www.aygfsteel.com/xiexin80/archive/2008/12/31/249338.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>U程&&同步http://www.aygfsteel.com/xiexin80/archive/2008/12/30/249165.html天长天长Tue, 30 Dec 2008 05:51:00 GMThttp://www.aygfsteel.com/xiexin80/archive/2008/12/30/249165.htmlhttp://www.aygfsteel.com/xiexin80/comments/249165.htmlhttp://www.aygfsteel.com/xiexin80/archive/2008/12/30/249165.html#Feedback0http://www.aygfsteel.com/xiexin80/comments/commentRss/249165.htmlhttp://www.aygfsteel.com/xiexin80/services/trackbacks/249165.html 多个U程操作同一资源的问题(不同步)(j)Q第一个线E正操作资源Q还没完成,W二个线E也来操作?br /> 出现?jin)资源错位?br /> 解决的方法是资源的同步(synchronizedQ。即Q一个线E访问资源的时候,别的U程不能讉K该资源?br /> 实现同步?U方法:(x) 1.把方法同步?nbsp; 2.同步代码块?br /> 实现?jin)同步特征?x)1.U程安全?jin)?2.性能差了(jin)。(异步正好相反Q?br /> Object中的wait()QɾU程q入{待状态,需要别的线Enotify()l箋(hu)工作?br /> wait()必须要在同步的情况下才能使用?br /> 随之带来?jin)死锁的问题?br /> 面试题:(x)
一、waitQ)(j)和sleepQ)(j)区别Q?br /> waitҎ(gu)是ObjectcM的方法?br /> sleepҎ(gu)是ThreadcM的方法?br />
当线E进入wait时别的线E可以访问锁定的对象?br /> 当线E进入sleep时别的线E不能访问锁定的对象?br />
二、写一个死锁?br />
class Demo implements Runnable {
    
int flag ;
    
static Object o1 = new Object(), o2 = new Object();
    
public void run(){
        System.out.println(
"U程" + flag + "在运行。?/span>");
        
if(flag == 1){
            
synchronized(o1){
                
try {
                    Thread.sleep(
100);
                } 
catch(InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(
"U程1 --> o1");
                
synchronized(o2){
                    System.out.println(
"U程1 --> o2");
                }
            }
        }
        
if(flag == 2){
            
synchronized(o2){
                
try {
                    Thread.sleep(
100);
                } 
catch(InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(
"U程2 --> o2");
                
synchronized(o1){
                    System.out.println(
"U程2 --> o1");
                }
            }
        }
    }
}

l果D死锁?br />
U程1在运行。?br /> U程2在运行。?br /> U程1 --> o1
U程2 
--> o2
不能l箋(hu)q行?br />
2个ObjectcMؓ(f)staticQ说明他们锁定的是同2个对象;

三、Producer和Consumer的问?br /> 涉及(qing)?jin)waitҎ(gu)和nofityҎ(gu)
public class ProducerConsumer {
    
public static void main(String[] args) {
        SyncStack ss 
= new SyncStack();
        Producer p 
= new Producer(ss);
        Consumer c 
= new Consumer(ss);
        
new Thread(p).start();
        
new Thread(c).start();
    }
}

class WoTou {
    
int id; 
    WoTou(
int id) {
        
this.id = id;
    }
    
public String toString() {
        
return "WoTou : " + id;
    }
}

class SyncStack {
    
int index = 0;
    WoTou[] arrWT 
= new WoTou[6];
    
    
public synchronized void push(WoTou wt) {
        
while(index == arrWT.length) {
            
try {
                
this.wait();
            } 
catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        
this.notifyAll();        
        arrWT[index] 
= wt;
        index 
++;
    }
    
    
public synchronized WoTou pop() {
        
while(index == 0) {
            
try {
                
this.wait();
            } 
catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        
this.notifyAll();
        index
--;
        
return arrWT[index];
    }
}

class Producer implements Runnable {
    SyncStack ss 
= null;
    Producer(SyncStack ss) {
        
this.ss = ss;
    }
    
    
public void run() {
        
for(int i=0; i<20; i++) {
            WoTou wt 
= new WoTou(i);
            ss.push(wt);
System.out.println(
"生?jin)?x)" + wt);
            
try {
                Thread.sleep((
int)(Math.random() * 200));
            } 
catch (InterruptedException e) {
                e.printStackTrace();
            }            
        }
    }
}

class Consumer implements Runnable {
    SyncStack ss 
= null;
    Consumer(SyncStack ss) {
        
this.ss = ss;
    }
    
    
public void run() {
        
for(int i=0; i<20; i++) {
            WoTou wt 
= ss.pop();
System.out.println(
"消费? " + wt);
            
try {
                Thread.sleep((
int)(Math.random() * 1000));
            } 
catch (InterruptedException e) {
                e.printStackTrace();
            }            
        }
    }
}



天长 2008-12-30 13:51 发表评论
]]>
对String q个Ҏ(gu)的对象de感?zhn)Q{Q?/title><link>http://www.aygfsteel.com/xiexin80/archive/2008/12/07/244830.html</link><dc:creator>天长</dc:creator><author>天长</author><pubDate>Sun, 07 Dec 2008 00:21:00 GMT</pubDate><guid>http://www.aygfsteel.com/xiexin80/archive/2008/12/07/244830.html</guid><wfw:comment>http://www.aygfsteel.com/xiexin80/comments/244830.html</wfw:comment><comments>http://www.aygfsteel.com/xiexin80/archive/2008/12/07/244830.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/xiexin80/comments/commentRss/244830.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/xiexin80/services/trackbacks/244830.html</trackback:ping><description><![CDATA[<span style="font-size: 14px"><span id="Content1419085">初学 Java 有段旉?jin),感觉g开始入?jin)门Q有?jin)点儿感?<br /> 但是发现很多困惑和疑问而且均来自于最基础的知?<br /> 折腾?jin)一阵子又查?jin)查书,l于?String q个Ҏ(gu)的对象有?jin)点感(zhn)?<br /> </span></span><br /> 大家先来看看一D奇怪的E序Q?<br /> <font color="blue">public class TestString { <br /> public static void main(String[] args) { <br /> String s1 = "Monday"; <br /> String s2 = "Monday"; <br /> } <br /> }<br />  </font>q个E序真是单啊Q可是有什么问题呢Q?<br /> <br /> <font color="red">1. 来自 String 的忧?/font> <br /> 上面q段E序中,到底有几个对象呢Q?<br /> 可能很多口而出Q两个,s1 ?s2 <br /> Z么? <br /> String ?final c,它的g可变?<br /> 看v来似乎很有道理,那么来检一下吧Q稍微改动一下程?<br /> 可以看到结果了(jin)Q?<br /> <br /> <font color="blue">public class TestString { <br /> public static void main(String[] args) { <br /> String s1 = "Monday"; <br /> String s2 = "Monday"; <br /> if (s1 == s2) <br /> System.out.println("s1 == s2"); <br /> else <br /> System.out.println("s1 != s2"); <br /> } <br /> } </font>呵呵Q很多h都会(x)说已l不止两个对象了(jin) <br /> ~译q运行程序,输出Qs1 == s2 <br /> 啊! <br /> Z?s1 == s2 ? <br /> == 分明是在_(d)(x)s1 ?s2 引用同一?String 对象 -- "Monday"! <br /> <br /> <font color="red">2. 千变?sh)化?String </font>再稍微改动一下程序,?x)有更奇怪的发现Q?<br /> <font color="blue">public class TestString { <br /> public static void main(String[] args) { <br /> String s1 = "Monday"; <br /> String s2 = new String("Monday"); <br /> if (s1 == s2) <br /> System.out.println("s1 == s2"); <br /> else <br /> System.out.println("s1 != s2"); <br /> if (s1.equals(s2)) <br /> System.out.println("s1 equals s2"); <br /> else <br /> System.out.println("s1 not equals s2"); <br /> } <br /> } </font>我们?s2 ?new 操作W创?<br /> E序输出Q?<br /> s1 != s2 <br /> s1 equals s2 <br /> 嗯,很明昑֘ <br /> s1 s2分别引用?jin)两?Monday"String对象 <br /> 可是Z么两D늨序不一样呢Q?<br /> <br /> <font color="red">3. ?String 的游x(chng)中游?</font><br /> 哈哈Q翻?jin)翻书终于找C(jin){案Q?<br /> 原来Q程序在q行的时候会(x)创徏一个字W串~冲?<br /> 当?s2 = "Monday" q样的表达是创徏字符串的时候,E序首先?<br /> 在这个String~冲池中L相同值的对象Q在W一个程序中Qs1先被 <br /> 攑ֈ?jin)池中,所以在s2被创建的时候,E序扑ֈ?jin)具有相同值的 s1 <br /> ?s2 引用 s1 所引用的对?Monday" <br /> <br /> W二D늨序中Q用了(jin) new 操作W,他明白的告诉E序Q?<br /> “我要一个新的!不要旧的Q?#8221;与是一个新?Monday"Sting对象被创 <br /> 建在内存?sh)。他们的值相同,但是位置不同Q一个在池中游泳 <br /> 一个在岸边休息。哎呀Q真是资源浪费,明明是一L(fng)非要分开做什么呢Q?<br /> <br /> <font color="red">4. l箋(hu)潜水 </font><br /> 再次更改E序Q?<br /> <font color="blue">public class TestString { <br /> public static void main(String[] args) { <br /> String s1 = "Monday"; <br /> String s2 = new String("Monday"); <br /> s2 = s2.intern(); <br /> if (s1 == s2) <br /> System.out.println("s1 == s2"); <br /> else <br /> System.out.println("s1 != s2"); <br /> if (s1.equals(s2)) <br /> System.out.println("s1 equals s2"); <br /> else <br /> System.out.println("s1 not equals s2"); <br /> } <br /> } </font>q次加入Qs2 = s2.intern(); <br /> 哇!E序输出Q?<br /> s1 == s2 <br /> s1 equals s2 <br /> 原来Q程序新Z(jin) s2 之后Q又用intern()把他打翻在了(jin)池里 <br /> 哈哈Q这?s2 ?s1 有引用了(jin)同样的对象了(jin) <br /> 我们成功的减了(jin)内存的占?<br /> <br /> <font color="red">5. == ?equals() 的争?</font><br /> String 是个对象Q要Ҏ(gu)两个不同的String对象的值是否相?<br /> 明显的要用到 equals() q个Ҏ(gu) <br /> 可是如果E序里面有那么多的String对象Q有那么多次的要用到 equals Q?<br /> 哦,天哪Q真慢啊 <br /> 更好的办法:(x) <br /> 把所有的String都intern()到缓冲池d <br /> 最好在用到new的时候就q行q个操作 <br /> String s2 = new String("Monday").intern(); <br /> 嗯,大家都在水池里(chng)着?jin)吗Q哈?<br /> 现在我可以无所ֿ的用 == 来比?String 对象的g(jin) <br /> 真是爽啊Q又快又方便Q?<br /> <script language="javascript"> <!-- leadcode('Content1419085'); //--> </script><br /> <br /> <br /> 看看 String q一ơ又怎么闹事儿吧 <br /> <font color="red">1. 回顾一下坏脾气?String 老弟 </font><br /> 例程1Q?br /> <font color="blue">class Str { <br />     public static void main(String[] args) { <br />         String s = "Hi!"; <br />         String t = "Hi!"; <br />         if (s == t) <br />             System.out.println("equals"); <br />         else <br />              System.out.println("not equals"); <br />     } <br /> } </font>E序输出什么呢Q?<br />   E序输出Qequals <br /> <font color="red">2. 哦,天哪Q它又在搅؜水了(jin)</font> <br /> 例程2Q?br /> <font color="blue">class Str { <br />     public static void main(String[] args) { <br />         String s = "HELLO"; <br />         String t = s.toUpperCase(); <br />         if (s == t) <br />             System.out.println("equals"); <br />         else <br />             System.out.println("not equals"); <br />     } <br /> } </font>那么q个E序有输Z么呢Q?<br /> 慎重Q再慎重Q不要被 String q个qؕ的家伙所qhQ?<br /> 它输出:(x)equals<br />           WHY!!! <br /> 把程序简单的更改一下:(x) <br /> <font color="blue">class Str2 { <br />     public static void main(String[] args) { <br />         String s = "Hello"; <br />         String t = s.toUpperCase(); <br />         if (s == t) <br />             System.out.println("equals"); <br />         else <br />             System.out.println("not equals"); <br />     } <br /> } </font>你可能会(x)_(d)(x)不是一样吗Q不Q千真万的Q不一Pq一ơ输出:(x) <br /> not equals  Oh MyGOD!!! <br /> 谁来教训一下这?String 啊! <br /> <font color="red">3. 你了(jin)解你的马吗? </font><br /> “要驯服脱~的野马Q就要了(jin)解它的秉?#8221;牛仔们说道?<br /> 你了(jin)?String 吗?解读 String ?API Q可以看刎ͼ(x)toUpperCase() ?toLowerCase() Ҏ(gu)q回一个新的String对象Q它?yu)原字符串表C字W串的大写或写形势Q但是要注意Q如果原字符串本w就是大写Ş式或写形式Q那么返回原始对象。这是Z么第二个E序?s ?t U缠不清的缘故对待这个淘气的、屡教不改的 String Q似乎没有更好的办法?jin)让我们解剖它,看看它到底有什么结构吧Q?<br /> <font color="blue">(1) charAt(int n)</font> q回字符串内n位置的字W,W一个字W位|ؓ(f)0Q最后一个字W的位置为length()-1Q访问错误的位置?x)扔Z块大砖头QStringIndexOutOfBoundsException 真够大的 <br /> <font color="blue">(2) concat(String str)</font> 在原对象之后q接一?str Q但是返回一个新?String 对象 <br /> <font color="blue">(3) EqualsIgnoreCase(String str) </font>忽略大小写的 equals Ҏ(gu)q个Ҏ(gu)的实质是首先调用?rn)态字W方法toUpperCase() 或?toLowerCase() 对比的两个字符转换Q然后进?== q算 <br /> <font color="blue">(4) trim()</font> q回一个新的对象,它将原对象的开头和l尾的空白字W切掉同L(fng)Q如果结果与原对象没有差别,则返回原对象 <br /> <font color="blue">(5) toString()</font> String cM?toString() Ҏ(gu)吗?真是一个有的问题Q可是如果没有它Q你?String 对象说不定真的不能用在System.out.println() 里面啊小?j),它返回对象自己String c还有很多其他方法,掌握他们?x)带来很多方便也会(x)有很多困惑Q所以坚持原则,是最关键?<br /> <font color="red">4. 我想CҎ(gu)好的马来购买更驯服温和的 String 的小?StringBuffer ?</font><br /> q时候会(x)有h反对Q?<br /> 它很好用Q它效率很高Q它怎么能够是小弟呢Q?<br /> 很简单,它的交互功能要比 String ,如果你要~辑字符串它q不方便Q你?x)对它失望但q不意味着它不强大public final class String implements Serializable, Comparable, CharSequencepublic final class StringBuffer implements Serializable, CharSequence很明昄Q小弟少?jin)一些东东,不过q不?x)干扰它的前途StringBuffer 不是?String l承来的不过要注意兄弟它也是 final 啊,本是同根生看看他的方法吧Q这么多E_可靠的方法,用v来比皮?String 要有效率的多? Java 为需要改变的字符串对象提供了(jin)独立?StringBuffer cd的实例不可变QfinalQ,之所以要把他们分开是因为,字符串的修改要求pȝ的开销量增大,占用更多的空间也更复杂,怿当有10000人挤在一个狭的游泳池里游泳而岸边又?0000人等待进入游x(chng)而焦急上火又?0000人在旁边看热闹的时候,你这?String 游泳池的理员(sh)?x)焦头烂额在你无需改变字符串的情况下,单的 String cd_你唤的?jin),而当要频J的更改字符串的内容的时候,p借助于宰相肚里能撑船的StringBuffer ?<br /> <font color="red">5. 宰相肚里能撑?</font><br /> (1) length() ?capacity()String 中的 length() q回字符串的长度兄弟 StringBuffer 也是如此Q他们都由对象包含的字符长度军_capacity()呢? <br /> <font color="blue">public class TestCapacity { <br />     public static void main(String[] args)?<br />      StringBuffer buf = new StringBuffer("it was the age of wisdom,"); <br />        System.out.println("buf = " + buf); <br />         System.out.println("buf.length() = " + buf.length()); <br />         System.out.println("buf.capacity() = " + buf.capacity()); <br />         String str = buf.toString(); <br />         System.out.println("str = " + str); <br />         System.out.println("str.length() = " + str.length()); <br />         buf.append(" " + str.substring(0,18)).append("foolishness,"); <br />         System.out.println("buf = " + buf); <br />         System.out.println("buf.length() = " + buf.length()); <br />         System.out.println("buf.capacity() = " + buf.capacity()); <br />        System.out.println("str = " + str); <br />    } <br /> } </font><br /> E序输出Q?<br /> <font color="blue">buf = it was the age of wisdom.buf.length() = 25 <br /> buf.capacity() = 41 <br /> str = it was the age of wisdomstr.length() = 25 <br /> buf = it was the age of wisdom, it was the age of foolishness, <br /> buf.length() = 56 <br /> buf.capacity() = 84 <br /> str = it was the age of wisdom,</font> <br /> 可以看到Q在内容更改之后Qcapacity也随之改变(sh)(jin)长度随着向字W串d字符而增加而容量只是在新的长度过?jin)现在的定w之后才增加StringBuffer 的容量在操作pȝ需要的时候是自动改变的程序员?sh)对capacity所能够做的仅仅是可以在初始?StringBuffer对象的时候?br /> <br /> <br /> 转蝲自:(x)http://www.leadbbs.com/a/a.asp?B=222&ID=1419085 <img src ="http://www.aygfsteel.com/xiexin80/aggbug/244830.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/xiexin80/" target="_blank">天长</a> 2008-12-07 08:21 <a href="http://www.aygfsteel.com/xiexin80/archive/2008/12/07/244830.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>自定义对象重写equalsҎ(gu)http://www.aygfsteel.com/xiexin80/archive/2008/12/07/244829.html天长天长Sat, 06 Dec 2008 23:15:00 GMThttp://www.aygfsteel.com/xiexin80/archive/2008/12/07/244829.htmlhttp://www.aygfsteel.com/xiexin80/comments/244829.htmlhttp://www.aygfsteel.com/xiexin80/archive/2008/12/07/244829.html#Feedback0http://www.aygfsteel.com/xiexin80/comments/commentRss/244829.htmlhttp://www.aygfsteel.com/xiexin80/services/trackbacks/244829.html 重写?jin)equalsҎ(gu)?br />
class Test2 {
    
public static void main(String args[]) {
        Dog d1 
= new Dog(1);
        Dog d2 
= new Dog(1);
        Dog d3 
= new Dog(2);
        System.out.println(d1 
== d2);//false
        System.out.println(d1.equals(d2));//true
        System.out.println(d1.equals(d3));//false
    }
}

class Dog {
    
private int i;
    Dog(
int i) {
        
this.i = i;
    }
    
public boolean equals(Object o) {
        
if(o != null) {
            
if(o instanceof Dog) {  //判断传过来的 对象o 是否属于Dogcd?/span>
                Dog d = (Dog)o;  //把对象o强制转换成Dog对象cd
                if(this.i == d.i) {
                    
return true;
                }
            }
        }
        
return false;
    }
}


天长 2008-12-07 07:15 发表评论
]]>
Object中的Ҏ(gu)cStringQtoString()、equals?=Q?/title><link>http://www.aygfsteel.com/xiexin80/archive/2008/12/06/244730.html</link><dc:creator>天长</dc:creator><author>天长</author><pubDate>Sat, 06 Dec 2008 00:53:00 GMT</pubDate><guid>http://www.aygfsteel.com/xiexin80/archive/2008/12/06/244730.html</guid><wfw:comment>http://www.aygfsteel.com/xiexin80/comments/244730.html</wfw:comment><comments>http://www.aygfsteel.com/xiexin80/archive/2008/12/06/244730.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.aygfsteel.com/xiexin80/comments/commentRss/244730.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/xiexin80/services/trackbacks/244730.html</trackback:ping><description><![CDATA[<p><span style="font-size: 14pt"><span style="font-size: 14pt"><span style="font-size: 12pt"><span style="font-size: 12pt">我有很多概念都没明白Q现在反q头来认识?/span></span></span></span></p> <p><span style="font-size: 14pt"><span style="font-size: 14pt"><span style="font-size: 12pt"><span style="font-size: 12pt">Object是所有类的父cR?br /> java.lang.String也是l承自java.lang.Object中?/span></span></span></span></p> <p><span style="font-size: 14pt"><span style="font-size: 14pt"><span style="font-size: 12pt"><span style="font-size: 12pt">先比较一?个类的toString()、equals()? = Q很多面试题都有Q,再来说一下String 的特D?/span></span></span></span></p> <p><span style="font-size: 14pt"><span style="font-size: 14pt"><span style="font-size: 12pt"><span style="font-size: 12pt; color: red">2个类toString()的比较:(x)</span></span></span></span></p> <div style="border-right: #cccccc 1px solid; padding-right: 5px; border-top: #cccccc 1px solid; padding-left: 4px; font-size: small; padding-bottom: 4px; border-left: #cccccc 1px solid; width: 98%; word-break: break-all; padding-top: 4px; border-bottom: #cccccc 1px solid; background-color: #eeeeee"><span style="color: #0000ff"><span style="font-size: 12pt">class</span></span><span style="font-size: 12pt"><span style="color: #000000"> Test {<br />     </span><span style="color: #0000ff">public</span><span style="color: #000000"> </span><span style="color: #0000ff">static</span><span style="color: #000000"> </span><span style="color: #0000ff">void</span></span><span style="font-size: 12pt"><span style="color: #000000"> main(String [] args) {<br />         Object obj </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">new</span></span><span style="font-size: 12pt"><span style="color: #000000"> Object();<br />         Dog d </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">new</span></span><span style="font-size: 12pt"><span style="color: #000000"> Dog();<br />         String s </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">new</span><span style="color: #000000"> String(</span><span style="color: #000000">"</span><span style="color: #000000">abc</span><span style="color: #000000">"</span></span><span style="font-size: 12pt"><span style="color: #000000">);<br />         <br />         System.out.println(</span><span style="color: #000000">"</span><span style="color: #000000">obj = </span><span style="color: #000000">"</span><span style="color: #000000"> </span><span style="color: #000000">+</span></span><span style="font-size: 12pt"><span style="color: #000000"> obj);    <br />         System.out.println(</span><span style="color: #000000">"</span><span style="color: #000000">d = </span><span style="color: #000000">"</span><span style="color: #000000"> </span><span style="color: #000000">+</span></span><span style="font-size: 12pt"><span style="color: #000000"> d);<br />         System.out.println(</span><span style="color: #000000">"</span><span style="color: #000000">s = </span><span style="color: #000000">"</span><span style="color: #000000"> </span><span style="color: #000000">+</span></span><span style="font-size: 12pt"><span style="color: #000000"> s);<br />     }<br /> }<br /> <br /> </span><span style="color: #0000ff">class</span></span><span style="font-size: 12pt"><span style="color: #000000"> Dog {}<br /> <br /> l果Q?br /> <br /> obj </span><span style="color: #000000">=</span></span><span style="font-size: 12pt"><span style="color: #000000"> java.lang.Object@c17164<br /> d </span><span style="color: #000000">=</span></span><span style="font-size: 12pt"><span style="color: #000000"> Dog@1fb8ee3<br /> s </span><span style="color: #000000">=</span></span><span style="color: #000000"><span style="font-size: 12pt"> abc<br /> </span></span></div> <p><br /> <span style="font-size: 14pt"><span style="font-size: 12pt">查阅API文档Q?br /> </span></p> <div style="border-right: #cccccc 1px solid; padding-right: 5px; border-top: #cccccc 1px solid; padding-left: 4px; font-size: small; padding-bottom: 4px; border-left: #cccccc 1px solid; width: 98%; word-break: break-all; padding-top: 4px; border-bottom: #cccccc 1px solid; background-color: #eeeeee"><span style="font-size: 12pt"><span style="color: #000000">Objectcȝ </span><span style="color: #0000ff">public</span></span><span style="font-size: 12pt"><span style="color: #000000"> String toString()   returns  <br />                     getClass().getName() </span><span style="color: #000000">+</span><span style="color: #000000"> </span><span style="color: #000000">'</span><span style="color: #000000">@</span><span style="color: #000000">'</span><span style="color: #000000"> </span><span style="color: #000000">+</span></span><span style="font-size: 12pt"><span style="color: #000000"> Integer.toHexString(hashCode())<br /> Stringcȝ  </span><span style="color: #0000ff">public</span></span><span style="color: #000000"><span style="font-size: 12pt"> String toString()   returns  <br />                       the string itself.</span></span></div> <p><br /> <span style="font-size: 12pt">说明Stringc重写了(jin)Object的toStringҎ(gu)?br /> </span></span><br /> <span style="font-size: 14pt"><span style="font-size: 12pt; color: red">2个类equals()? =比较Q?/span></span></p> <div style="border-right: #cccccc 1px solid; padding-right: 5px; border-top: #cccccc 1px solid; padding-left: 4px; font-size: small; padding-bottom: 4px; border-left: #cccccc 1px solid; width: 98%; word-break: break-all; padding-top: 4px; border-bottom: #cccccc 1px solid; background-color: #eeeeee"><span style="color: #0000ff"><span style="font-size: 12pt">class</span></span><span style="font-size: 12pt"><span style="color: #000000"> Test {<br />     </span><span style="color: #0000ff">public</span><span style="color: #000000"> </span><span style="color: #0000ff">static</span><span style="color: #000000"> </span><span style="color: #0000ff">void</span></span><span style="font-size: 12pt"><span style="color: #000000"> main(String [] args) {<br />         <br />         </span><span style="color: #008000">//</span><span style="color: #008000">比较equals()?=</span></span><span style="color: #008000"><br /> </span><span style="font-size: 12pt"><span style="color: #000000">        Dog d1 </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">new</span><span style="color: #000000"> Dog(</span><span style="color: #000000">1</span></span><span style="font-size: 12pt"><span style="color: #000000">);<br />         Dog d2 </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">new</span><span style="color: #000000"> Dog(</span><span style="color: #000000">1</span></span><span style="font-size: 12pt"><span style="color: #000000">);<br />         <br />         System.out.println(</span><span style="color: #000000">"</span><span style="color: #000000">d1 == d2 </span><span style="color: #000000">"</span><span style="color: #000000"> </span><span style="color: #000000">+</span><span style="color: #000000"> (d1 </span><span style="color: #000000">==</span><span style="color: #000000"> d2)); </span><span style="color: #008000">//</span><span style="color: #008000">false</span></span><span style="color: #008000"><br /> </span><span style="font-size: 12pt"><span style="color: #000000">        System.out.println(</span><span style="color: #000000">"</span><span style="color: #000000">d1.equals(d2) </span><span style="color: #000000">"</span><span style="color: #000000"> </span><span style="color: #000000">+</span><span style="color: #000000"> d1.equals(d2)); </span><span style="color: #008000">//</span><span style="color: #008000">false</span></span><span style="color: #008000"><br /> </span><span style="font-size: 12pt"><span style="color: #000000">        <br />         String s1 </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">new</span><span style="color: #000000"> String(</span><span style="color: #000000">"</span><span style="color: #000000">abc</span><span style="color: #000000">"</span></span><span style="font-size: 12pt"><span style="color: #000000">);<br />         String s2 </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">new</span><span style="color: #000000"> String(</span><span style="color: #000000">"</span><span style="color: #000000">abc</span><span style="color: #000000">"</span></span><span style="font-size: 12pt"><span style="color: #000000">);<br />         String s3 </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #000000">"</span><span style="color: #000000">abc</span><span style="color: #000000">"</span></span><span style="font-size: 12pt"><span style="color: #000000">;<br />         <br />         System.out.println(</span><span style="color: #000000">"</span><span style="color: #000000">s1 == s2 </span><span style="color: #000000">"</span><span style="color: #000000"> </span><span style="color: #000000">+</span><span style="color: #000000"> (s1 </span><span style="color: #000000">==</span><span style="color: #000000"> s2)); </span><span style="color: #008000">//</span><span style="color: #008000">false</span></span><span style="color: #008000"><br /> </span><span style="font-size: 12pt"><span style="color: #000000">        System.out.println(</span><span style="color: #000000">"</span><span style="color: #000000">s1.equals(s2) </span><span style="color: #000000">"</span><span style="color: #000000"> </span><span style="color: #000000">+</span><span style="color: #000000"> s1.equals(s2)); </span><span style="color: #008000">//</span><span style="color: #008000">ture</span></span><span style="color: #008000"><br /> </span><span style="font-size: 12pt"><span style="color: #000000">        <br />         System.out.println(</span><span style="color: #000000">"</span><span style="color: #000000">s1 == s3 </span><span style="color: #000000">"</span><span style="color: #000000"> </span><span style="color: #000000">+</span><span style="color: #000000"> (s1 </span><span style="color: #000000">==</span><span style="color: #000000"> s3)); </span><span style="color: #008000">//</span><span style="color: #008000">false</span></span><span style="color: #008000"><br /> </span><span style="font-size: 12pt"><span style="color: #000000">        System.out.println(</span><span style="color: #000000">"</span><span style="color: #000000">s1.equals(s3) </span><span style="color: #000000">"</span><span style="color: #000000"> </span><span style="color: #000000">+</span><span style="color: #000000"> s1.equals(s3)); </span><span style="color: #008000">//</span><span style="color: #008000">true</span></span><span style="color: #008000"><br /> </span><span style="font-size: 12pt"><span style="color: #000000">    }<br /> }<br /> <br /> </span><span style="color: #0000ff">class</span></span><span style="font-size: 12pt"><span style="color: #000000"> Dog {<br />     </span><span style="color: #0000ff">private</span><span style="color: #000000"> </span><span style="color: #0000ff">int</span></span><span style="font-size: 12pt"><span style="color: #000000"> i;<br />     Dog(){}<br />     Dog(</span><span style="color: #0000ff">int</span></span><span style="font-size: 12pt"><span style="color: #000000"> i){<br />         </span><span style="color: #0000ff">this</span><span style="color: #000000">.i </span><span style="color: #000000">=</span></span><span style="color: #000000"><span style="font-size: 12pt"> i;<br />     }<br /> }<br /> </span></span></div> <p><span style="font-size: 12pt"> </span></p> <p><span style="font-size: 14pt"><span style="font-size: 12pt"><span style="font-size: 12pt"><span style="font-size: 12pt">"=="是比?个对象的引用是否指向同一个地址Q如果是比较2个基本类型,那么是比较2个值是否相{?br /> 我new?个Dogc,他们2个对象的栈引用肯定不能指向同一个堆地址。第二个String对象"=="的比较也是同样道理。所以都q回?jin)false(W三个比较体C(jin)String的特D类Q等下说??/span></span></span></span></p> <p><span style="font-size: 14pt"><span style="font-size: 12pt"><span style="font-size: 12pt"><span style="font-size: 12pt">查阅jdk的源代码Q?equals"在Object中定义ؓ(f)<br /> <br /> </span></span></span></span></p> <div style="border-right: #cccccc 1px solid; padding-right: 5px; border-top: #cccccc 1px solid; padding-left: 4px; font-size: small; padding-bottom: 4px; border-left: #cccccc 1px solid; width: 98%; word-break: break-all; padding-top: 4px; border-bottom: #cccccc 1px solid; background-color: #eeeeee"><span style="font-size: 12pt"><span style="color: #0000ff">public</span><span style="color: #000000"> </span><span style="color: #0000ff">boolean</span></span><span style="font-size: 12pt"><span style="color: #000000"> equals(Object obj) {<br />     </span><span style="color: #0000ff">return</span><span style="color: #000000"> (</span><span style="color: #0000ff">this</span><span style="color: #000000"> </span><span style="color: #000000">==</span></span><span style="color: #000000"><span style="font-size: 12pt"> obj);<br /> }</span></span></div> <br /> <p><span style="font-size: 14pt"><span style="font-size: 12pt"><span style="font-size: 12pt">q就表示一个类如果没有重写该方法的话,它就是比较两个的引用是否指向?jin)同一个地址Q和"=="比较是一致的Q?br /> cd里的cd本都重写的该Ҏ(gu)Q所以是比较2个对象的内容是否一L(fng)QStringҎ(gu)重写?jin)该?gu)Q所以第二个equals比较q回tureQ,对于外面自己自定义的c,则需要自己重写该Ҏ(gu)来达到内Ҏ(gu)否相{的逻辑?/span><br /> </span></span></p> <p><span style="font-size: 14pt"><span style="font-size: 12pt"><br /> <span style="color: red">Ҏ(gu)对象String的equals()? =比较</span>Q?br /> <br /> String s = newString("abc");和String s = "abc";<br /> 首先2个对象在内存分布上是不一L(fng)。第2个对象是s的栈引用指向数据片段区的地址Q存攑֭W串帔RQ静(rn)态成员变量)(j)。第1个new出来的对象是s的栈引用指向堆中的地址Q存攑֜堆中Q。通过API文档Q新创徏的字W串对象s是该参数字符Ԍ"abc"Q的副本?br /> <br /> 所以如代码所表示Q?=="比较是不一L(fng)QequalsҎ(gu)׃String重写?jin),比较是字W序列?/span></span></p> <p><span style="font-size: 14pt"><span style="font-size: 12pt"> </span></span></p> <p><span style="font-size: 14pt"><span style="font-size: 14pt"><span style="font-size: 12pt">String s1 = "abc"    ?nbsp; String s2 = "abc"比较Q?br /> <br /> "=="和equalsҎ(gu)都返回tureQ?br /> s1分配?jin)字W常?abc"Q那么再?abc"要引用给对象Q都不会(x)再分配内存空间了(jin)?br /> 所以s1 和s2 指向的空间相同,内容也相同?/span><br /> </span></span></p> <img src ="http://www.aygfsteel.com/xiexin80/aggbug/244730.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/xiexin80/" target="_blank">天长</a> 2008-12-06 08:53 <a href="http://www.aygfsteel.com/xiexin80/archive/2008/12/06/244730.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>java 数组的排?NumSort.javahttp://www.aygfsteel.com/xiexin80/articles/244723.html天长天长Fri, 05 Dec 2008 22:20:00 GMThttp://www.aygfsteel.com/xiexin80/articles/244723.htmlhttp://www.aygfsteel.com/xiexin80/comments/244723.htmlhttp://www.aygfsteel.com/xiexin80/articles/244723.html#Feedback0http://www.aygfsteel.com/xiexin80/comments/commentRss/244723.htmlhttp://www.aygfsteel.com/xiexin80/services/trackbacks/244723.html刚来的,先试一下手?br /> 以后再慢慢搬?/span>
1.选择排序

class NumSort 
{
    
public static void main(String[] args) 
    {
        
int [] a = new int[args.length];
        
for(int i=0; i<a.length ; i++)
        {
            a[i] 
= Integer.parseInt(args[i]);
        }
        print(a);
        selectSort(a);
        print(a);
    }

    
private static void selectSort(int [] a)
    {
        
for(int i=0; i<a.length; i++)
        {
            
for(int j=i+1; j<a.length; j++)
            {
                
if(a[i] > a[j])
                {
                    
int temp = a[i];
                    a[i] 
= a[j];
                    a[j] 
= temp;
                }
            }
        }
    }

    
private static void print(int [] a)
    {
        
for(int i=0; i<a.length ; i++)
        {
            System.out.print(a[i] 
+ " ");
        }
        System.out.println();
    }
}

2.冒(chng)排序

天长 2008-12-06 06:20 发表评论
]]>
վ֩ģ壺 | | ޻| | ¬| ˮ| | | | | Ž| | | | «Ϫ| ʯ| ɽ| | ̫| ɽ| ־| | | γ| | | ɳ| | Ǹ| γ| ʯ| | | | | | | | ζ| | ݳ|