﻿<?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-Metal's workspace</title><link>http://www.blogjava.net/mazhen5040/</link><description /><language>zh-cn</language><lastBuildDate>Thu, 07 May 2026 06:14:22 GMT</lastBuildDate><pubDate>Thu, 07 May 2026 06:14:22 GMT</pubDate><ttl>60</ttl><item><title>JDK源码分析1：java.lang.Boolean</title><link>http://www.blogjava.net/mazhen5040/archive/2009/05/16/271023.html</link><dc:creator>Metal</dc:creator><author>Metal</author><pubDate>Sat, 16 May 2009 11:44:00 GMT</pubDate><guid>http://www.blogjava.net/mazhen5040/archive/2009/05/16/271023.html</guid><wfw:comment>http://www.blogjava.net/mazhen5040/comments/271023.html</wfw:comment><comments>http://www.blogjava.net/mazhen5040/archive/2009/05/16/271023.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/mazhen5040/comments/commentRss/271023.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/mazhen5040/services/trackbacks/271023.html</trackback:ping><description><![CDATA[接触Java已经四年了，可是一直觉得自己的知识比较散乱，也不是很扎实。往往是指其然，而不知其所以然。静下心来仔细想想，觉得原因主要有两点，一是自己不求甚解，觉得会用就行；二是缺少总结积累，没有把以前的经验教训记录下来。痛定思痛，我决定回到起点，从分析JDK源码开始，使自己对Java有一个新的认识。<br />
<br />
由于本人水平有限，难免有很多错误，或分析的不够深入的地方，还请大家指正！<br />
<br />
我用的JDK是1.6，先从最简单的java.lang.Boolean开始。<br />
<br />
Boolean类是基本类型boolean的一个包装类，它包含了一个boolean类型的字段：<br />
<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">final</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);">&nbsp;Boolean&nbsp;</span><span style="color: rgb(0, 0, 255);">implements</span><span style="color: rgb(0, 0, 0);">&nbsp;java.io.Serializable,&nbsp;Comparable</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">Boolean</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">{<br />
</span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp; </span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">final</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">boolean</span><span style="color: rgb(0, 0, 0);">&nbsp;value;<br />
</span><span style="color: rgb(0, 0, 0);">}</span></div>
这是一个final类型的变量，因此一旦运行构造函数创建对象，它的值就无法更改。所以这个类是一个非可变类（对于非可变类的描述详见《Effective Java》第13条）<br />
<br />
Boolean类有两个构造函数，允许使用boolean类型的变量或String值来创建对象，但并不推荐使用这两个构造函数。由于boolean只有true和false两种状态，所以Boolean类中提供了两个对应的静态变量：<br />
<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">final</span><span style="color: rgb(0, 0, 0);">&nbsp;Boolean&nbsp;TRUE&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);">&nbsp;Boolean(</span><span style="color: rgb(0, 0, 255);">true</span><span style="color: rgb(0, 0, 0);">);<br />
</span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">final</span><span style="color: rgb(0, 0, 0);">&nbsp;Boolean&nbsp;FALSE&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);">&nbsp;Boolean(</span><span style="color: rgb(0, 0, 255);">false</span><span style="color: rgb(0, 0, 0);">);</span></div>
我们可以直接使用这两个对象，这样可以避免在内存中创建重复对象，也减少了相应的时间消耗,有可能显著的提高空间和时间性能。如：<br />
<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: rgb(0, 0, 0);">Boolean&nbsp;var&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;Boolean.True;</span></div>
<br />
如果我们需要根据一个boolean变量来创建一个对象时该怎么办呢，这时我们可以使用Boolean类提供的两个和构造方法相对应的工厂方法：<br />
<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);">&nbsp;Boolean&nbsp;valueOf(</span><span style="color: rgb(0, 0, 255);">boolean</span><span style="color: rgb(0, 0, 0);">&nbsp;b)&nbsp;{<br />
</span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp; </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">&nbsp;(b&nbsp;</span><span style="color: rgb(0, 0, 0);">?</span><span style="color: rgb(0, 0, 0);">&nbsp;TRUE&nbsp;:&nbsp;FALSE);<br />
</span><span style="color: rgb(0, 0, 0);">}<br />
</span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);">&nbsp;Boolean&nbsp;valueOf(String&nbsp;s)&nbsp;{<br />
</span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp; </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">&nbsp;toBoolean(s)&nbsp;</span><span style="color: rgb(0, 0, 0);">?</span><span style="color: rgb(0, 0, 0);">&nbsp;TRUE&nbsp;:&nbsp;FALSE;<br />
</span><span style="color: rgb(0, 0, 0);">}</span></div>
从代码中可以看出，工厂方法实际上并没有创建新的对象，而是返回上面所说的两个静态变量中的一个。<br />
<br />
其余的几个方法比较简单，如：<br />
<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;hashCode()&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">&nbsp;value&nbsp;</span><span style="color: rgb(0, 0, 0);">?</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">1231</span><span style="color: rgb(0, 0, 0);">&nbsp;:&nbsp;</span><span style="color: rgb(0, 0, 0);">1237</span><span style="color: rgb(0, 0, 0);">;<br />
}</span></div>
其中1231代表true，1237代表false。<br />
<br />
<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">boolean</span><span style="color: rgb(0, 0, 0);">&nbsp;equals(Object&nbsp;obj)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">&nbsp;(obj&nbsp;</span><span style="color: rgb(0, 0, 255);">instanceof</span><span style="color: rgb(0, 0, 0);">&nbsp;Boolean)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">&nbsp;value&nbsp;</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">&nbsp;((Boolean)obj).booleanValue();<br />
&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">false</span><span style="color: rgb(0, 0, 0);">;<br />
}</span></div>
<br />
另外Boolean类实现了Comparable接口：<br />
<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;compareTo(Boolean&nbsp;b)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">&nbsp;(b.value&nbsp;</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">&nbsp;value&nbsp;</span><span style="color: rgb(0, 0, 0);">?</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">&nbsp;:&nbsp;(value&nbsp;</span><span style="color: rgb(0, 0, 0);">?</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">&nbsp;:&nbsp;</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">));<br />
}</span></div>
<br />
<img src="/CuteSoft_Client/CuteEditor/images/emidea.gif" alt="" align="absmiddle" border="0" />小结<br />
<ol>
    <li>对于状态有限的Java类，可以用几个final的静态变量来表示所有的状态实例。例如季节，可以在Season类中定义春、夏、秋、冬四个变量，用户可以直接调用Season.Spring。</li>
    <li>访问静态实例还可以通过工厂方法来实现，如Boolean.valueOf(var)。</li>
</ol>
<br />
<br />
&nbsp;&nbsp;&nbsp; <br />
<img src ="http://www.blogjava.net/mazhen5040/aggbug/271023.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/mazhen5040/" target="_blank">Metal</a> 2009-05-16 19:44 <a href="http://www.blogjava.net/mazhen5040/archive/2009/05/16/271023.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>