<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>fatkun&#039;s blog &#187; java基础</title>
	<atom:link href="http://fatkun.com/tag/java%e5%9f%ba%e7%a1%80/feed" rel="self" type="application/rss+xml" />
	<link>http://fatkun.com</link>
	<description>又一个 WordPress 站点</description>
	<lastBuildDate>Sat, 19 May 2012 17:25:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>List中的remove使用注意</title>
		<link>http://fatkun.com/2010/12/list-remove.html</link>
		<comments>http://fatkun.com/2010/12/list-remove.html#comments</comments>
		<pubDate>Tue, 21 Dec 2010 05:09:50 +0000</pubDate>
		<dc:creator>fatkun</dc:creator>
				<category><![CDATA[J2EE]]></category>
		<category><![CDATA[java基础]]></category>
		<category><![CDATA[list]]></category>

		<guid isPermaLink="false">http://fatkun.com/?p=717</guid>
		<description><![CDATA[先来看API是怎样写的。 boolean remove(Object o) 从此列表中移除第一次出现的指定元素（如果存在）（可选操作）。如果列表不包含元素，则不更改列表。更确切地讲，移除满足 (o==null ? get(i)==null : o.equals(get(i))) 的最低索引 i 的元素（如果存在这样的元素）。如果此列表已包含指定元素（或者此列表由于调用而发生更改），则返回 true。 可以看到是用o.equals来判断是否删除的，在一般情况下使用都没有问题。但我有一次犯了一个错误。 有一个List&#60;子类&#62; list，我用 list.remve(父类对象);这样很明显是不行的，equals不了，所以就删除不了。 解决方法：1，是remove同一类对象 2，在父类覆盖equals方法，用instanceof来判断是不是子类，然后判断相等。 方法2较为麻烦。还是建议方法1。]]></description>
			<content:encoded><![CDATA[<p>先来看API是怎样写的。</p>
<blockquote><p>boolean <strong>remove</strong>(<a title="java.lang 中的类">Object</a> o)</p>
<dl>
<dd>从此列表中移除第一次出现的指定元素（如果存在）（可选操作）。如果列表不包含元素，则不更改列表。更确切地讲，移除满足  <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt> 的最低索引 <tt>i</tt> 的元素（如果存在这样的元素）。如果此列表已包含指定元素（或者此列表由于调用而发生更改），则返回 <tt>true</tt>。 </dd>
</dl>
</blockquote>
<p>可以看到是用o.equals来判断是否删除的，在一般情况下使用都没有问题。但我有一次犯了一个错误。</p>
<p>有一个List&lt;子类&gt; list，我用 list.remve(父类对象);这样很明显是不行的，equals不了，所以就删除不了。</p>
<p>解决方法：1，是remove同一类对象</p>
<p>2，在父类覆盖equals方法，用instanceof来判断是不是子类，然后判断相等。</p>
<p>方法2较为麻烦。还是建议方法1。</p>
]]></content:encoded>
			<wfw:commentRss>http://fatkun.com/2010/12/list-remove.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>JAVA的==与Equals</title>
		<link>http://fatkun.com/2010/11/java-equals.html</link>
		<comments>http://fatkun.com/2010/11/java-equals.html#comments</comments>
		<pubDate>Sun, 21 Nov 2010 03:38:21 +0000</pubDate>
		<dc:creator>fatkun</dc:creator>
				<category><![CDATA[J2EE]]></category>
		<category><![CDATA[equal]]></category>
		<category><![CDATA[java基础]]></category>

		<guid isPermaLink="false">http://fatkun.com/?p=669</guid>
		<description><![CDATA[以下是我的理解，可能有误 Java中的“==”用来判断是否是同一个对象（对象的引用是不是一样），equals方法一般会被覆盖，主要是比较值，而且一般只和同一类对象比较。 String的比较 public class TestEqual &#123; &#160; public static void main&#40;String&#91;&#93; args&#41; &#123; String a = &#34;abc&#34;; String b = &#34;abc&#34;; String c = new String&#40;&#34;abc&#34;&#41;;//新建了一个对象 String d = new String&#40;&#34;abc&#34;&#41;; String e = &#34;ab&#34;+&#34;c&#34;;//使用常量池中的对象 String f = &#34;ab&#34;; String g = f + &#34;c&#34;;//string是固定字符，需要新建对象 System.out.println&#40;a == b&#41;;//true，第一次a把字符串放在常量池，那b继续用这个常量，e也一样 System.out.println&#40;a.equals&#40;b&#41;&#41;;//true System.out.println&#40;&#34;a == c?&#34;+&#40;a == c&#41;&#41;;//false [...]]]></description>
			<content:encoded><![CDATA[<p>以下是我的理解，可能有误</p>
<p>Java中的“==”用来判断是否是同一个对象（对象的引用是不是一样），equals方法一般会被覆盖，主要是比较值，而且一般只和同一类对象比较。</p>
<h2>String的比较</h2>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> TestEqual <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003399;">String</span> a <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;abc&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">String</span> b <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;abc&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">String</span> c <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;abc&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//新建了一个对象</span>
		<span style="color: #003399;">String</span> d <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;abc&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">String</span> e <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;ab&quot;</span><span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;c&quot;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//使用常量池中的对象</span>
		<span style="color: #003399;">String</span> f <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;ab&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">String</span> g <span style="color: #339933;">=</span> f <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;c&quot;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//string是固定字符，需要新建对象</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>a <span style="color: #339933;">==</span> b<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//true，第一次a把字符串放在常量池，那b继续用这个常量，e也一样</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>a.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>b<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//true</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;a == c?&quot;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span>a <span style="color: #339933;">==</span> c<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//false</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>a.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>c<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//true</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>c<span style="color: #339933;">==</span>d<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//false</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>a<span style="color: #339933;">==</span>e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//true</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>a<span style="color: #339933;">==</span>g<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//false</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>还有个String.intern()不太理解<br />
更多的信息可以看这里：<a href="http://developer.51cto.com/art/200602/21445.htm">http://developer.51cto.com/art/200602/21445.htm</a></p>
<h2>数字的比较</h2>
<p>需要理解autoBoxing的概念！</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> TestEqual <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003399;">Integer</span> i1 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Integer</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
		<span style="color: #003399;">Integer</span> i2 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Integer</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
		<span style="color: #000066; font-weight: bold;">int</span> i3 <span style="color: #339933;">=</span> <span style="color: #cc66cc;">9</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">int</span> i4 <span style="color: #339933;">=</span> <span style="color: #cc66cc;">9</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">Integer</span> i5 <span style="color: #339933;">=</span> <span style="color: #cc66cc;">9</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//自动装箱(box)操作，基本类型int 9可以自动box成对象Integer</span>
		<span style="color: #003399;">Integer</span> i6 <span style="color: #339933;">=</span> <span style="color: #cc66cc;">9</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">Long</span> long1 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Long</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>i1<span style="color: #339933;">==</span>i2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//false</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>i1<span style="color: #339933;">==</span>i3<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//true</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;i3==i4?&quot;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span>i3<span style="color: #339933;">==</span>i4<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//true，常量池</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;i1==i3?&quot;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span>i1<span style="color: #339933;">==</span>i3<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//true</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;i5==i6?&quot;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span>i5<span style="color: #339933;">==</span>i6<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//true，当-128&lt;i5&lt;127时为true，其他为false</span>
		<span style="color: #666666; font-style: italic;">//http://blog.csdn.net/newsonglin/archive/2008/10/15/3079865.aspx</span>
		<span style="color: #666666; font-style: italic;">// If the value p being boxed is true, false, a byte, a char in the range \u0000 to \u007f, or an int or short number between -128 and 127, then let r1 and r2 be the results of any two boxing conversions of p. It is always the case that r1 == r2.</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;i3==i5?&quot;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span>i3<span style="color: #339933;">==</span>i5<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//false</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;i1==i5?&quot;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span>i1<span style="color: #339933;">==</span>i5<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//false</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>i1.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>i2<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//true，同类型比较值</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>i1.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>long1<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//false，不同类型不进行比较</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>更多的信息可以参考这篇文章:<a href="http://blog.csdn.net/newsonglin/archive/2008/10/15/3079865.aspx">http://blog.csdn.net/newsonglin/archive/2008/10/15/3079865.aspx</a></p>
]]></content:encoded>
			<wfw:commentRss>http://fatkun.com/2010/11/java-equals.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Comparator和Comparable的区别使用</title>
		<link>http://fatkun.com/2010/11/comparator-and-comparable.html</link>
		<comments>http://fatkun.com/2010/11/comparator-and-comparable.html#comments</comments>
		<pubDate>Sat, 06 Nov 2010 15:47:58 +0000</pubDate>
		<dc:creator>fatkun</dc:creator>
				<category><![CDATA[J2EE]]></category>
		<category><![CDATA[Comparable]]></category>
		<category><![CDATA[Comparator]]></category>
		<category><![CDATA[java基础]]></category>

		<guid isPermaLink="false">http://fatkun.com/?p=648</guid>
		<description><![CDATA[Camparable与Comparator的区别 简单说： 实现Camparable接口可以直接使用sort方法Collections.sort(List list)排序 实现Comparator接口的可以用Collections.sort(List list, Comparator c)排序 一个类实现了Camparable接口则表明这个类的对象之间是可以相互比较的，这个类对象组成的集合就可以直接使用sort方法排序。 Comparator可以看成一种算法的实现，将算法和数据分离，Comparator也可以在下面两种环境下使用： 1、类的设计师没有考虑到比较问题而没有实现Comparable，可以通过Comparator来实现排序而不必改变对象本身 2、可以使用多种排序标准，比如升序、降序等 Camparable实例 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 [...]]]></description>
			<content:encoded><![CDATA[<h2>Camparable与Comparator的区别</h2>
<p>简单说：</p>
<ul>
<li>实现Camparable接口可以直接使用sort方法Collections.sort(List list)排序</li>
<li>实现Comparator接口的可以用Collections.sort(List list, Comparator c)排序</li>
</ul>
<p>一个类实现了Camparable接口则表明这个类的对象之间是可以相互比较的，这个类对象组成的集合就可以<strong>直接使用sort方法</strong>排序。<br />
Comparator可以看成一种算法的实现，将算法和数据分离，Comparator也可以在下面两种环境下使用：<br />
1、类的设计师没有考虑到比较问题而没有实现Comparable，可以通过Comparator来实现排序而不必改变对象本身<br />
2、可以使用多种排序标准，比如升序、降序等</p>
<h2>Camparable实例</h2>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Arrays</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> User <span style="color: #000000; font-weight: bold;">implements</span> <span style="color: #003399;">Comparable</span> <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> id<span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> age<span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> User<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> id, <span style="color: #000066; font-weight: bold;">int</span> age<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">id</span> <span style="color: #339933;">=</span> id<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">age</span> <span style="color: #339933;">=</span> age<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> getAge<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">return</span> age<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setAge<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> age<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">age</span> <span style="color: #339933;">=</span> age<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getId<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">return</span> id<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setId<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> id<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">id</span> <span style="color: #339933;">=</span> id<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> compareTo<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> o<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">age</span> <span style="color: #339933;">-</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>User<span style="color: #009900;">&#41;</span> o<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getAge</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #008000; font-style: italic; font-weight: bold;">/**
   * 测试方法
   */</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    User<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> users <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> User<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000000; font-weight: bold;">new</span> User<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;a&quot;</span>, <span style="color: #cc66cc;">30</span><span style="color: #009900;">&#41;</span>, <span style="color: #000000; font-weight: bold;">new</span> User<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;b&quot;</span>, <span style="color: #cc66cc;">20</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #003399;">Arrays</span>.<span style="color: #006633;">sort</span><span style="color: #009900;">&#40;</span>users<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> users.<span style="color: #006633;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      User user <span style="color: #339933;">=</span> users<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
      <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>user.<span style="color: #006633;">getId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; &quot;</span> <span style="color: #339933;">+</span> user.<span style="color: #006633;">getAge</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h2>Comparator实例</h2>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Arrays</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Comparator</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SampleComparator <span style="color: #000000; font-weight: bold;">implements</span> <span style="color: #003399;">Comparator</span> <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> compare<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> o1, <span style="color: #003399;">Object</span> o2<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">return</span> toInt<span style="color: #009900;">&#40;</span>o1<span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> toInt<span style="color: #009900;">&#40;</span>o2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> toInt<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> o<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003399;">String</span> str <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#41;</span> o<span style="color: #339933;">;</span>
    str <span style="color: #339933;">=</span> str.<span style="color: #006633;">replaceAll</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;一&quot;</span>, <span style="color: #0000ff;">&quot;1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    str <span style="color: #339933;">=</span> str.<span style="color: #006633;">replaceAll</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;二&quot;</span>, <span style="color: #0000ff;">&quot;2&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    str <span style="color: #339933;">=</span> str.<span style="color: #006633;">replaceAll</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;三&quot;</span>, <span style="color: #0000ff;">&quot;3&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">//</span>
    <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #003399;">Integer</span>.<span style="color: #006633;">parseInt</span><span style="color: #009900;">&#40;</span>str<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #008000; font-style: italic; font-weight: bold;">/**
   * 测试方法
   */</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> array <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> <span style="color: #0000ff;">&quot;一二&quot;</span>, <span style="color: #0000ff;">&quot;三&quot;</span>, <span style="color: #0000ff;">&quot;二&quot;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #003399;">Arrays</span>.<span style="color: #006633;">sort</span><span style="color: #009900;">&#40;</span>array, <span style="color: #000000; font-weight: bold;">new</span> SampleComparator<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> array.<span style="color: #006633;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>array<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>原文来自：<a href="http://blog.csdn.net/turkeyzhou/archive/2009/09/16/4558392.aspx">Comparator和Comparable的区别</a></p>
]]></content:encoded>
			<wfw:commentRss>http://fatkun.com/2010/11/comparator-and-comparable.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java的访问控制</title>
		<link>http://fatkun.com/2010/07/java-access.html</link>
		<comments>http://fatkun.com/2010/07/java-access.html#comments</comments>
		<pubDate>Fri, 30 Jul 2010 09:00:55 +0000</pubDate>
		<dc:creator>fatkun</dc:creator>
				<category><![CDATA[J2EE]]></category>
		<category><![CDATA[java基础]]></category>
		<category><![CDATA[Thinking in Java 读书笔记]]></category>
		<category><![CDATA[权限]]></category>

		<guid isPermaLink="false">http://fatkun.com/?p=514</guid>
		<description><![CDATA[包（Package） 创建自己的包时，要求 package语句必须是文件中的第一个“非注释”代码。 如果类名冲突时，可这样写java.util.Vector v = new java.util.Vector(); 可能（但并常见）有一个编译单元根本没有任何公共类。此时，可按自己的意愿任意指定文件名。 访问控制 修饰符 类内部 同一个包 子类 任何地方 private √ default √ √ protected √ √ √ public √ √ √ √ 类的修饰符只有public 和 default，默认时只能被同一个文件或包内访问。 下面是代码解释 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 [...]]]></description>
			<content:encoded><![CDATA[<h2>包（Package）</h2>
<ol>
<li>创建自己的包时，要求 package语句必须是文件中的第一个“非注释”代码。</li>
<li>如果类名冲突时，可这样写java.util.Vector v = new java.util.Vector();</li>
<li>可能（但并常见）有一个编译单元根本没有任何公共类。此时，可按自己的意愿任意指定文件名。</li>
</ol>
<h2>访问控制</h2>
<table border="0" cellspacing="0" cellpadding="0" width="584">
<tbody>
<tr>
<td width="105">
<p align="center"><strong>修饰符</strong></p>
</td>
<td width="139">
<p align="center"><strong>类内部</strong></p>
</td>
<td width="87">
<p align="center"><strong>同一个包</strong></p>
</td>
<td width="104">
<p align="center"><strong>子类</strong></p>
</td>
<td width="149">
<p align="center"><strong>任何地方</strong></p>
</td>
</tr>
<tr>
<td width="105">
<p align="center">private</p>
</td>
<td width="139">
<p align="center">√</p>
</td>
<td width="87"></td>
<td width="104"></td>
<td width="149"></td>
</tr>
<tr>
<td width="105">
<p align="center">default</p>
</td>
<td width="139">
<p align="center">√</p>
</td>
<td width="87">
<p align="center">√</p>
</td>
<td width="104"></td>
<td width="149"></td>
</tr>
<tr>
<td width="105">
<p align="center">protected</p>
</td>
<td width="139">
<p align="center">√</p>
</td>
<td width="87">
<p align="center">√</p>
</td>
<td width="104">
<p align="center">√</p>
</td>
<td width="149"></td>
</tr>
<tr>
<td width="105">
<p align="center">public</p>
</td>
<td width="139">
<p align="center">√</p>
</td>
<td width="87">
<p align="center">√</p>
</td>
<td width="104">
<p align="center">√</p>
</td>
<td width="149">
<p align="center">√</p>
</td>
</tr>
</tbody>
</table>
<p>类的修饰符只有public 和 default，默认时只能被同一个文件或包内访问。</p>
<p><span id="more-514"></span></p>
<p><strong>下面是代码解释</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//PackageClass.java</span>
<span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.fatkun.util</span><span style="color: #339933;">;</span>
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * 在包com.fatkun.util内，分别有a,b,c,d四个变量，它们的修饰符都不一样。
 * @author fatkun
 *
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> PackageClass <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> a<span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">int</span> b<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">int</span> c<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> d<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		PackageClass p <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PackageClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>p.<span style="color: #006633;">a</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>p.<span style="color: #006633;">b</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>p.<span style="color: #006633;">c</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>p.<span style="color: #006633;">d</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">new</span> InsideClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">class</span> InsideClass <span style="color: #009900;">&#123;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//AnotherPackageClass.java</span>
<span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.fatkun.util</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * 同一个包内的类，private的不能访问
 * @author fatkun
 *
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> AnotherPackageClass <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		PackageClass p <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PackageClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>p.<span style="color: #006633;">a</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//private只能在类内部访问</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>p.<span style="color: #006633;">b</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>p.<span style="color: #006633;">c</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>p.<span style="color: #006633;">d</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//内部类在同一个包内可以访问</span>
		<span style="color: #000000; font-weight: bold;">new</span> InsideClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//ExtendClass.java</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.fatkun.util.*</span><span style="color: #339933;">;</span>
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * PackageClass的子类
 * @author fatkun
 *
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ExtendClass <span style="color: #000000; font-weight: bold;">extends</span> PackageClass <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		ExtendClass e <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ExtendClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>e.<span style="color: #006633;">a</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//private只能在类内部访问</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>e.<span style="color: #006633;">b</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//default只能在类内部和同一个包内访问</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>e.<span style="color: #006633;">c</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//protect能在子类访问</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>e.<span style="color: #006633;">d</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//内部类在子类不可以访问</span>
		<span style="color: #000000; font-weight: bold;">new</span> InsideClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//DefaultClass.java</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.fatkun.util.*</span><span style="color: #339933;">;</span>
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * 不同包内
 * @author fatkun
 *
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> DefaultClass <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		PackageClass p <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PackageClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>p.<span style="color: #006633;">a</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//private只能在类内部访问</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>p.<span style="color: #006633;">b</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//default只能在类内部和同一个包内访问</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>p.<span style="color: #006633;">c</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//protect只能在类内部、同一个包、子类访问</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>p.<span style="color: #006633;">d</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//public能在任何地方访问</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//内部类在不同包内不可以访问</span>
		<span style="color: #000000; font-weight: bold;">new</span> InsideClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://fatkun.com/2010/07/java-access.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Java运算符优先级(表格)</title>
		<link>http://fatkun.com/2010/07/java-operator-priority.html</link>
		<comments>http://fatkun.com/2010/07/java-operator-priority.html#comments</comments>
		<pubDate>Wed, 28 Jul 2010 14:45:18 +0000</pubDate>
		<dc:creator>fatkun</dc:creator>
				<category><![CDATA[J2EE]]></category>
		<category><![CDATA[java基础]]></category>
		<category><![CDATA[Thinking in Java 读书笔记]]></category>

		<guid isPermaLink="false">http://fatkun.com/?p=504</guid>
		<description><![CDATA[Java运算符优先级参考图表 本文来源 http://blog.csdn.net/xiaoli_feng/archive/2009/09/18/4567184.aspx 优先级 运算符 结合性 1 () [] . 从左到右 2 ! +(正)  -(负) ~ ++ &#8211; 从右向左 3 * / % 从左向右 4 +(加) -(减) 从左向右 5 &#60;&#60; &#62;&#62; &#62;&#62;&#62; 从左向右 6 &#60; &#60;= &#62; &#62;= instanceof 从左向右 7 ==   != 从左向右 8 &#38;(按位与) 从左向右 9 ^ 从左向右 10 &#124; 从左向右 11 &#38;&#38; 从左向右 12 &#124;&#124; 从左向右 13 ?: 从右向左 14 = += -= *= /= %= &#38;= &#124;= ^=  ~=  &#60;&#60;= &#62;&#62;= &#62;&#62;&#62;= 从右向左 说明： 1、 该表中优先级按照从高到低的顺序书写，也就是优先级为1的优先级最高，优先级14的优先级最低。 2、 [...]]]></description>
			<content:encoded><![CDATA[<p>Java运算符优先级参考图表</p>
<p>本文来源 <a href="http://blog.csdn.net/xiaoli_feng/archive/2009/09/18/4567184.aspx">http://blog.csdn.net/xiaoli_feng/archive/2009/09/18/4567184.aspx</a></p>
<table border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td width="73" valign="top">
<p align="left">优先级</p>
</td>
<td width="462" valign="top">
<p align="left">运算符</p>
</td>
<td width="85" valign="top">
<p align="left">结合性</p>
</td>
</tr>
<tr>
<td width="73" valign="top">
<p align="left">1</p>
</td>
<td width="462" valign="top">
<p align="left">() [] .</p>
</td>
<td width="85" valign="top">
<p align="left">从左到右</p>
</td>
</tr>
<tr>
<td width="73" valign="top">
<p align="left">2</p>
</td>
<td width="462" valign="top">
<p align="left">! +(正)  -(负) ~ ++ &#8211;</p>
</td>
<td width="85" valign="top">
<p align="left">从右向左</p>
</td>
</tr>
<tr>
<td width="73" valign="top">
<p align="left">3</p>
</td>
<td width="462" valign="top">
<p align="left">* / %</p>
</td>
<td width="85" valign="top">
<p align="left">从左向右</p>
</td>
</tr>
<tr>
<td width="73" valign="top">
<p align="left">4</p>
</td>
<td width="462" valign="top">
<p align="left">+(加) -(减)</p>
</td>
<td width="85" valign="top">
<p align="left">从左向右</p>
</td>
</tr>
<tr>
<td width="73" valign="top">
<p align="left">5</p>
</td>
<td width="462" valign="top">
<p align="left">&lt;&lt; &gt;&gt; &gt;&gt;&gt;</p>
</td>
<td width="85" valign="top">
<p align="left">从左向右</p>
</td>
</tr>
<tr>
<td width="73" valign="top">
<p align="left">6</p>
</td>
<td width="462" valign="top">
<p align="left">&lt; &lt;= &gt; &gt;= instanceof</p>
</td>
<td width="85" valign="top">
<p align="left">从左向右</p>
</td>
</tr>
<tr>
<td width="73" valign="top">
<p align="left">7</p>
</td>
<td width="462" valign="top">
<p align="left">==   !=</p>
</td>
<td width="85" valign="top">
<p align="left">从左向右</p>
</td>
</tr>
<tr>
<td width="73" valign="top">
<p align="left">8</p>
</td>
<td width="462" valign="top">
<p align="left">&amp;(按位与)</p>
</td>
<td width="85" valign="top">
<p align="left">从左向右</p>
</td>
</tr>
<tr>
<td width="73" valign="top">
<p align="left">9</p>
</td>
<td width="462" valign="top">
<p align="left">^</p>
</td>
<td width="85" valign="top">
<p align="left">从左向右</p>
</td>
</tr>
<tr>
<td width="73" valign="top">
<p align="left">10</p>
</td>
<td width="462" valign="top">
<p align="left">|</p>
</td>
<td width="85" valign="top">
<p align="left">从左向右</p>
</td>
</tr>
<tr>
<td width="73" valign="top">
<p align="left">11</p>
</td>
<td width="462" valign="top">
<p align="left">&amp;&amp;</p>
</td>
<td width="85" valign="top">
<p align="left">从左向右</p>
</td>
</tr>
<tr>
<td width="73" valign="top">
<p align="left">12</p>
</td>
<td width="462" valign="top">
<p align="left">||</p>
</td>
<td width="85" valign="top">
<p align="left">从左向右</p>
</td>
</tr>
<tr>
<td width="73" valign="top">
<p align="left">13</p>
</td>
<td width="462" valign="top">
<p align="left">?:</p>
</td>
<td width="85" valign="top">
<p align="left">从右向左</p>
</td>
</tr>
<tr>
<td width="73" valign="top">
<p align="left">14</p>
</td>
<td width="462" valign="top">
<p align="left">= += -= *= /= %= &amp;= |= ^=    ~=  &lt;&lt;= &gt;&gt;= &gt;&gt;&gt;=</p>
</td>
<td width="85">
<p align="left">从右向左</p>
</td>
</tr>
</tbody>
</table>
<p>说明：</p>
<p>1、 该表中优先级按照从高到低的顺序书写，也就是优先级为1的优先级最高，优先级14的优先级最低。</p>
<p>2、 结合性是指运算符结合的顺序，通常都是从左到右。从右向左的运算符最典型的就是负号，例如3+-4，则意义为3加-4，符号首先和运算符右侧的内容结合。</p>
<p>3、 instanceof作用是判断对象是否为某个类或接口类型。</p>
<p>4、 注意区分正负号和加减号，以及按位与和逻辑与的区别</p>
<p>其实在实际的开发中，不需要去记忆运算符的优先级别，也不要刻意的使用运算符的优先级别，对于不清楚优先级的地方使用小括号去进行替代，示例代码：</p>
<p>int m = 12;</p>
<p>int n = m &lt;&lt; 1 + 2;</p>
<p>int n = m &lt;&lt; (1 + 2); //这样更直观</p>
<p>这样书写代码，更方便编写代码，也便于代码的阅读和维护。</p>
]]></content:encoded>
			<wfw:commentRss>http://fatkun.com/2010/07/java-operator-priority.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Java的重载(Overload)与重写(Override)</title>
		<link>http://fatkun.com/2010/07/java-overload-and-overrid.html</link>
		<comments>http://fatkun.com/2010/07/java-overload-and-overrid.html#comments</comments>
		<pubDate>Wed, 28 Jul 2010 14:10:25 +0000</pubDate>
		<dc:creator>fatkun</dc:creator>
				<category><![CDATA[未分类]]></category>
		<category><![CDATA[java基础]]></category>
		<category><![CDATA[Thinking in Java 读书笔记]]></category>

		<guid isPermaLink="false">http://fatkun.com/?p=507</guid>
		<description><![CDATA[重载与重写之间的差别 区别点 重载方法 重写方法 参数列表 必须修改 一定不能修改 返回类型 可以修改 一定不能修改 异常 可以修改 可以减少或删除，一定不能抛出新的或者更广的异常 访问 可以修改 一定不能做更严格的限制（可以降低限制） 重载（Overload） 每个重载的方法（或者构造函数）都必须有一个独一无二的参数类型列表。 只能重载构造函数 规则 被重载的方法必须改变参数列表； 被重载的方法可以改变返回类型； 被重载的方法可以改变访问修饰符； 被重载的方法可以声明新的或更广的检查异常； 方法能够在同一个类中或者在一个子类中被重载。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 [...]]]></description>
			<content:encoded><![CDATA[<h2>重载与重写之间的差别</h2>
<table border="0" cellspacing="0" cellpadding="0" width="492">
<tbody>
<tr>
<td width="72">
<p align="left"><strong>区别点</strong></p>
</td>
<td width="80">
<p align="left"><strong>重载方法</strong> <strong> </strong></p>
</td>
<td width="340">
<p align="left"><strong>重写方法</strong></p>
</td>
</tr>
<tr>
<td width="72">
<p align="left">参数列表</p>
</td>
<td width="80">
<p align="left">必须修改</p>
</td>
<td width="340">
<p align="left">一定不能修改</p>
</td>
</tr>
<tr>
<td width="72">
<p align="left">返回类型</p>
</td>
<td width="80">
<p align="left">可以修改</p>
</td>
<td width="340">
<p align="left">一定不能修改</p>
</td>
</tr>
<tr>
<td width="72">
<p align="left">异常</p>
</td>
<td width="80">
<p align="left">可以修改</p>
</td>
<td width="340">
<p align="left">可以减少或删除，一定不能抛出新的或者更广的异常</p>
</td>
</tr>
<tr>
<td width="72">
<p align="left">访问</p>
</td>
<td width="80">
<p align="left">可以修改</p>
</td>
<td width="340">
<p align="left">一定不能做更严格的限制（可以降低限制）</p>
</td>
</tr>
</tbody>
</table>
<h2>重载（Overload）</h2>
<p align="left">每个重载的方法（或者构造函数）都必须有一个独一无二的参数类型列表。<br />
只能重载构造函数<br />
规则</p>
<ul>
<li>被重载的方法<strong><span style="text-decoration: underline;">必须</span></strong>改变参数列表；</li>
<li>被重载的方法<strong>可以</strong>改变返回类型；</li>
<li>被重载的方法<strong>可以</strong>改变访问修饰符；</li>
<li>被重载的方法<strong>可以</strong>声明新的或更广的检查异常；</li>
<li>方法能够在同一个类中或者在一个子类中被重载。</li>
</ul>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.fatkun</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * Overloading（重载，过载）
 * 方法名相同，参数类型不同或者参数类型顺序不同
 * 返回值，访问修饰符，异常可以不一样
 * @author fatkun
 *
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Overloading <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> test<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;test1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> test<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> a<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;test2&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>	
&nbsp;
	<span style="color: #666666; font-style: italic;">//以下两个参数类型顺序不同</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> test<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> a,<span style="color: #003399;">String</span> s<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;test3&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;returntest3&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>	
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> test<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> s,<span style="color: #000066; font-weight: bold;">int</span> a<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;test4&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;returntest4&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>	
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		Overloading o <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Overloading<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>o.<span style="color: #006633;">test</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		o.<span style="color: #006633;">test</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>o.<span style="color: #006633;">test</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span>,<span style="color: #0000ff;">&quot;test3&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>o.<span style="color: #006633;">test</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;test4&quot;</span>,<span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h2>重写（Override）</h2>
<p align="left">能够在需要新的子类特有行为时重新在子类中定义方法。<br />
规则</p>
<ul>
<li>参数列表<strong><span style="text-decoration: underline;">必须完全</span></strong>与被重写方法的相同；</li>
<li>返回类型<strong><span style="text-decoration: underline;">必须完全</span></strong>与被重写方法的返回类型相同；</li>
<li>访问级别的限制性<strong>一定不能</strong>比被重写方法的强；</li>
<li>访问级别的限制性可以比被重写方法的弱；</li>
<li>重写方法<strong>一定不能抛出新的检查异常或比被重写的方法声明的检查异常更广泛</strong>的检查异常</li>
<li>重写的方法能够抛出更少或更有限的异常（也就是说，被重写的方法声明了异常，但重写的方法可以什么也不声明）</li>
<li><strong>不能重写被标示为</strong><strong>final</strong><strong>的方法</strong>；</li>
<li>如果不能继承一个方法，则不能重写这个方法。</li>
</ul>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.fatkun</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * Overriding(重写，覆盖)
 * 重写是子类继承父类对父类的方法进行修改。方法名，参数，返回值必须一样。
 * 访问级别的限制性和异常不能比被重写的方法强
 * @author fatkun
 *
 */</span>
<span style="color: #000000; font-weight: bold;">class</span> TestClass <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> test<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;这是TestClass的test方法&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Overriding <span style="color: #000000; font-weight: bold;">extends</span> TestClass <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">new</span> Overriding<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">test</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> test<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;这是Overriding的test方法，重写了TestClass中的方法&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h2>调用</h2>
<p>重载方法：<br />
参数类型决定选择哪个重载版本（根据声明的参数类型），这发生在编译时。被调用的实际方法仍是发生在运行时期的虚拟方法调用。但是编译器已经知道所调用的方法的签名。因此，在运行时期，参数匹配已经明确，只是还不知道该方法所在的实际类。<br />
重写方法：<br />
对象类型（即：堆上实际实例的类型决定调用选择哪个方法，这发生在运行时期）</p>
<p>文章来源:<a href="http://chen1984.javaeye.com/blog/353342">http://chen1984.javaeye.com/blog/353342</a> 代码来源:fatkun</p>
]]></content:encoded>
			<wfw:commentRss>http://fatkun.com/2010/07/java-overload-and-overrid.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Java数据类型转换</title>
		<link>http://fatkun.com/2010/07/data-type-conversion.html</link>
		<comments>http://fatkun.com/2010/07/data-type-conversion.html#comments</comments>
		<pubDate>Wed, 28 Jul 2010 13:39:04 +0000</pubDate>
		<dc:creator>fatkun</dc:creator>
				<category><![CDATA[J2EE]]></category>
		<category><![CDATA[java基础]]></category>
		<category><![CDATA[Thinking in Java 读书笔记]]></category>

		<guid isPermaLink="false">http://fatkun.com/?p=500</guid>
		<description><![CDATA[1.在Java中看到象“1.39e-47f”这样的表达式时，它真正的含义是“1.39×10 的-47次方”。 2.注意如果编译器能够正确地识别类型，就不必使用尾随字符。对于下述语句： long n3 = 200; 它并不存在含混不清的地方，所以 200后面的一个 L大可省去。然而，对于下述语句： float f4 = 1e-47f; //10的幂数 编译器通常会将指数作为双精度数（double）处理，所以假如没有这个尾随的 f，就会收到一条出错提示，告诉我们须用一个“造型”将double 转换成 float。 3.通常，表达式中最大的数据类型是决定了表达式最终结果大小的那个类型。若将一个float 值与一个double值相乘，结果就是 double；如将一个 int和一个 long 值相加，则结果为long。 4.将一个 float或double 值造型成整数值后，总是将小数部分“砍掉”，不作任何进位处理。 5.Math.random()的输出值范围是[0,1) 返回带正号的 double 值，该值大于等于 0.0 且小于 1.0。 6.小数据类型转换成大数据类型可以自动转换，不会丢失精度。大数据类型转换为小数据类型必须显式的转换，可能丢失精度。]]></description>
			<content:encoded><![CDATA[<p>1.在Java中看到象“1.39e-47f”这样的表达式时，它真正的含义是“1.39×10 的-47次方”。</p>
<p>2.注意如果编译器能够正确地识别类型，就不必使用尾随字符。对于下述语句：</p>
<p>long n3 = 200;</p>
<p>它并不存在含混不清的地方，所以 200后面的一个 L大可省去。然而，对于下述语句：</p>
<p>float f4 = 1e-47f; //10的幂数</p>
<p>编译器通常会将指数作为双精度数（double）处理，所以假如没有这个尾随的 f，就会收到一条出错提示，告诉我们须用一个“造型”将double 转换成 float。</p>
<p>3.通常，表达式中最大的数据类型是决定了表达式最终结果大小的那个类型。若将一个float 值与一个double值相乘，结果就是 double；如将一个 int和一个 long 值相加，则结果为long。</p>
<p>4.将一个 float或double 值造型成整数值后，总是将小数部分“砍掉”，不作任何进位处理。</p>
<p>5.Math.random()的输出值范围是[0,1) 返回带正号的 <code>double</code> 值，该值大于等于 <code>0.0</code> 且小于 <code>1.0</code>。</p>
<p>6.小数据类型转换成大数据类型可以自动转换，不会丢失精度。大数据类型转换为小数据类型必须显式的转换，可能丢失精度。</p>
]]></content:encoded>
			<wfw:commentRss>http://fatkun.com/2010/07/data-type-conversion.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Java的运算符号（逻辑与、或、非、移位运算）</title>
		<link>http://fatkun.com/2010/07/java-operator.html</link>
		<comments>http://fatkun.com/2010/07/java-operator.html#comments</comments>
		<pubDate>Wed, 28 Jul 2010 03:45:53 +0000</pubDate>
		<dc:creator>fatkun</dc:creator>
				<category><![CDATA[J2EE]]></category>
		<category><![CDATA[java基础]]></category>
		<category><![CDATA[Thinking in Java 读书笔记]]></category>

		<guid isPermaLink="false">http://fatkun.com/?p=496</guid>
		<description><![CDATA[1.只可将AND，OR 或NOT 应用于布尔值。与在C 及C++中不同，不可将一个非布尔值当作布尔值在逻辑表达式中使用。 2.在AND（&#38;&#38;）运算中a()&#38;&#38;b()&#38;&#38;c()，当a为false时，b与c都不再执行，因为整个表达式都是false了，没必要再执行下去，OR（&#124;&#124;）也是一样，当有一个为true时就结束。 1 2 3 4 5 6 7 8 9 10 11 public class CalClass &#123; static Boolean test&#40;int num&#41;&#123; System.out.println&#40;num+&#34;&#62;2&#34;+&#40;num&#62;2&#41;&#41;; return num&#62;2; &#125; public static void main&#40;String&#91;&#93; args&#41;&#123; int i = 1, j =3 , k = 4; System.out.println&#40;test&#40;i&#41;&#124;&#124;test&#40;j&#41;&#124;&#124;test&#40;k&#41;&#41;; System.out.println&#40;&#34;end&#34;&#41;; &#125; &#125; 3.对于布尔值，按位运算符（如&#38;）具有与逻辑运算符(如&#38;&#38;)相同的效果，只是它们不会中途“短路”。 移位运算符 左移位运算符（&#60;&#60;）能将运算符左边的运算对象向左移动运算符右侧指定的位数（在低位补 0）。“有符号”右移位运算符（&#62;&#62;）则将运算符左边的运算对象向右移动运算符右侧指定的位数。“有符号”右移位运算符使用了“符号扩展”：若值为正，则在高位插入0；若值为负，则在高位插入1。Java 也添加了一种“无符号”右移位运算符（&#62;&#62;&#62;），它使用了“零扩展”：无论正负，都在高位插入0。 若对char，byte 或者short 进行移位处理，那么在移位进行之前，它们会自动转换成一个int。只有右侧的5个低位才会用到。这样可防止我们在一个 [...]]]></description>
			<content:encoded><![CDATA[<p>1.只可将AND，OR 或NOT 应用于布尔值。与在C 及C++中不同，不可将一个非布尔值当作布尔值在逻辑表达式中使用。<br />
2.在AND（&amp;&amp;）运算中a()&amp;&amp;b()&amp;&amp;c()，当a为false时，b与c都不再执行，因为整个表达式都是false了，没必要再执行下去，OR（||）也是一样，当有一个为true时就结束。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> CalClass <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">static</span> <span style="color: #003399;">Boolean</span> test<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> num<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>num<span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;&gt;2&quot;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span>num<span style="color: #339933;">&gt;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">return</span> num<span style="color: #339933;">&gt;</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span>, j <span style="color: #339933;">=</span><span style="color: #cc66cc;">3</span> , k <span style="color: #339933;">=</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>test<span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">||</span>test<span style="color: #009900;">&#40;</span>j<span style="color: #009900;">&#41;</span><span style="color: #339933;">||</span>test<span style="color: #009900;">&#40;</span>k<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;end&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>3.对于布尔值，按位运算符（如&amp;）具有与逻辑运算符(如&amp;&amp;)相同的效果，只是它们不会中途“短路”。</p>
<h2>移位运算符</h2>
<p>左移位运算符（&lt;&lt;）能将运算符左边的运算对象向左移动运算符右侧指定的位数（在低位补 0）。“有符号”右移位运算符（&gt;&gt;）则将运算符左边的运算对象向右移动运算符右侧指定的位数。“有符号”右移位运算符使用了“符号扩展”：若值为正，则在高位插入0；若值为负，则在高位插入1。Java 也添加了一种“无符号”右移位运算符（&gt;&gt;&gt;），它使用了“零扩展”：无论正负，都在高位插入0。<br />
若对char，byte 或者short 进行移位处理，那么在移位进行之前，它们会自动转换成一个int。只有右侧的5个低位才会用到。这样可防止我们在一个 int数里移动不切实际的位数。若对一个long 值进行处理，最后得到的结果也是long。此时只会用到右侧的 6个低位，防止移动超过 long 值里现成的位数。但在进行“无符号”右移位时，也可能遇到一个问题。若对 byte 或short 值进行右移位运算，得到的可能不是正确的结果（Java 1.0 和Java 1.1 特别突出）。它们会自动转换成int 类型，并进行右移位。但“零扩展”不会发生。</p>
<p>http://www.blogjava.net/rosen/archive/2005/08/12/9955.html</p>
<p>“&gt;&gt; 右移”；“&lt;&lt; 左移”；“&gt;&gt;&gt; 无符号右移”<br />
例子：</p>
<p>例子：<br />
-5&gt;&gt;3=-1<br />
1111 1111 1111 1111 1111 1111 1111 1011<br />
1111 1111 1111 1111 1111 1111 1111 1111<br />
其结果与 Math.floor((double)-5/(2*2*2)) 完全相同。</p>
<p>-5&lt;&lt;3=-40<br />
1111 1111 1111 1111 1111 1111 1111 1011<br />
1111 1111 1111 1111 1111 1111 1101 1000<br />
其结果与 -5*2*2*2 完全相同。</p>
<p>-5&gt;&gt;&gt;3=536870911<br />
1111 1111 1111 1111 1111 1111 1111 1011<br />
0001 1111 1111 1111 1111 1111 1111 1111</p>
]]></content:encoded>
			<wfw:commentRss>http://fatkun.com/2010/07/java-operator.html/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>java变量的作用域</title>
		<link>http://fatkun.com/2010/07/the-scope-of-variables.html</link>
		<comments>http://fatkun.com/2010/07/the-scope-of-variables.html#comments</comments>
		<pubDate>Tue, 27 Jul 2010 06:37:38 +0000</pubDate>
		<dc:creator>fatkun</dc:creator>
				<category><![CDATA[J2EE]]></category>
		<category><![CDATA[java基础]]></category>

		<guid isPermaLink="false">http://fatkun.com/?p=493</guid>
		<description><![CDATA[1 2 3 4 5 6 7 8 9 10 11 12 13 public class OneClass &#123; static int x;//会赋值默认值0 public static void main&#40;String&#91;&#93; args&#41;&#123; //int x = 2; //不能在同一个方法内定义同一个变量，不然会报错 Duplicate local variable x System.out.println&#40;&#34;第一个x:&#34;+x&#41;; &#123; int x = 5;//在方法内的变量必须初始化，否则x会是随机值而不是0 //如果不赋值会报错The local variable x may not have been initialized System.out.println&#40;&#34;第二个x:&#34;+x&#41;; &#125; &#125; &#125;]]></description>
			<content:encoded><![CDATA[
<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> OneClass <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">int</span> x<span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//会赋值默认值0</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">//int x = 2;</span>
		<span style="color: #666666; font-style: italic;">//不能在同一个方法内定义同一个变量，不然会报错 Duplicate local variable x</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;第一个x:&quot;</span><span style="color: #339933;">+</span>x<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000066; font-weight: bold;">int</span> x <span style="color: #339933;">=</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//在方法内的变量必须初始化，否则x会是随机值而不是0</span>
			<span style="color: #666666; font-style: italic;">//如果不赋值会报错The local variable x may not have been initialized</span>
			<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;第二个x:&quot;</span><span style="color: #339933;">+</span>x<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://fatkun.com/2010/07/the-scope-of-variables.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Java的主要类型及它们的取值范围</title>
		<link>http://fatkun.com/2010/07/java-data-type.html</link>
		<comments>http://fatkun.com/2010/07/java-data-type.html#comments</comments>
		<pubDate>Mon, 26 Jul 2010 17:11:29 +0000</pubDate>
		<dc:creator>fatkun</dc:creator>
				<category><![CDATA[J2EE]]></category>
		<category><![CDATA[byte取值范围]]></category>
		<category><![CDATA[java基础]]></category>
		<category><![CDATA[数据类型]]></category>

		<guid isPermaLink="false">http://fatkun.com/?p=487</guid>
		<description><![CDATA[Java的主要类型 主类型 默认值 大小（位） 最小值 最大值 封装器类型 boolean false 1 - - Boolean char &#8216;\u0000&#8242;(null) 16 Unicode 0 Unicode 2^16-1 Character byte (byte)0 8 -128 127 Byte short (short)0 16 -2^15 +2^15-1 Short int 0 32 -2^31 +2^31-1 Integer long 0L 64 -2^63 -2^63-1 Long float 0.0f 32 IEEE754 IEEE754 Float double 0.0d 64 IEEE754 [...]]]></description>
			<content:encoded><![CDATA[<h2>Java的主要类型</h2>
<table border="0" cellspacing="0" cellpadding="0" width="651">
<tbody>
<tr>
<td width="75">
<p align="center"><strong>主类型 </strong></p>
</td>
<td width="139">
<p align="center"><strong>默认值 </strong></p>
</td>
<td width="87">
<p align="center"><strong>大小（位）</strong></p>
</td>
<td width="104">
<p align="center"><strong>最小值</strong></p>
</td>
<td width="149">
<p align="center"><strong>最大值</strong></p>
</td>
<td width="97">
<p align="center"><strong>封装器类型</strong></p>
</td>
</tr>
<tr>
<td width="75">
<p align="center">boolean</p>
</td>
<td width="139">
<p align="center">false</p>
</td>
<td width="87">
<p align="center">1</p>
</td>
<td width="104">
<p align="center">-</p>
</td>
<td width="149">
<p align="center">-</p>
</td>
<td width="97">
<p align="center">Boolean</p>
</td>
</tr>
<tr>
<td width="75">
<p align="center">char</p>
</td>
<td width="139">
<p align="center">&#8216;\u0000&#8242;(null)</p>
</td>
<td width="87">
<p align="center">16</p>
</td>
<td width="104">
<p align="center">Unicode 0</p>
</td>
<td width="149">
<p align="center">Unicode 2^16-1</p>
</td>
<td width="97">
<p align="center">Character</p>
</td>
</tr>
<tr>
<td width="75">
<p align="center">byte</p>
</td>
<td width="139">
<p align="center">(byte)0</p>
</td>
<td width="87">
<p align="center">8</p>
</td>
<td width="104">
<p align="center">-128</p>
</td>
<td width="149">
<p align="center">127</p>
</td>
<td width="97">
<p align="center">Byte</p>
</td>
</tr>
<tr>
<td width="75">
<p align="center">short</p>
</td>
<td width="139">
<p align="center">(short)0</p>
</td>
<td width="87">
<p align="center">16</p>
</td>
<td width="104">
<p align="center">-2^15</p>
</td>
<td width="149">
<p align="center">+2^15-1</p>
</td>
<td width="97">
<p align="center">Short</p>
</td>
</tr>
<tr>
<td width="75">
<p align="center">int</p>
</td>
<td width="139">
<p align="center">0</p>
</td>
<td width="87">
<p align="center">32</p>
</td>
<td width="104">
<p align="center">-2^31</p>
</td>
<td width="149">
<p align="center">+2^31-1</p>
</td>
<td width="97">
<p align="center">Integer</p>
</td>
</tr>
<tr>
<td width="75">
<p align="center">long</p>
</td>
<td width="139">
<p align="center">0L</p>
</td>
<td width="87">
<p align="center">64</p>
</td>
<td width="104">
<p align="center">-2^63</p>
</td>
<td width="149">
<p align="center">-2^63-1</p>
</td>
<td width="97">
<p align="center">Long</p>
</td>
</tr>
<tr>
<td width="75">
<p align="center">float</p>
</td>
<td width="139">
<p align="center">0.0f</p>
</td>
<td width="87">
<p align="center">32</p>
</td>
<td width="104">
<p align="center">IEEE754</p>
</td>
<td width="149">
<p align="center">IEEE754</p>
</td>
<td width="97">
<p align="center">Float</p>
</td>
</tr>
<tr>
<td width="75">
<p align="center">double</p>
</td>
<td width="139">
<p align="center">0.0d</p>
</td>
<td width="87">
<p align="center">64</p>
</td>
<td width="104">
<p align="center">IEEE754</p>
</td>
<td width="149">
<p align="center">IEEE754</p>
</td>
<td width="97">
<p align="center">Double</p>
</td>
</tr>
</tbody>
</table>
<p>对于float:共32个bits，Bit 31是MSB（Most Significant Bit），Bit 0是LSB（Least Significant Bit），则<br />
Bit 31是符号位，接下来的8位是指数位，指数位被视为一个无符号的数，它与127的差就是以2为底的指数的部分。 最后的23位是小数部分。</p>
<h2>在byte(8位)中的取值范围为-128 到 127的问题</h2>
<p>在电脑用是使用补码来存储数字的，我搜索了很多，还不是很明白。以前的反码之类的没认真学习。。。</p>
<p>因为 -0 和 +0 在补码中是不一样的，把 1000 0000 作为-128(补)  ，127(补)=0111 1111，0(补) = 0000 0000， -127(补) = 1111 1111， -128在9位中表示应该为 1 1000 0000，取低八位就变成了  1000 0000</p>
<p>摘录一篇文章如下，更详细可以点链接查看</p>
<p><a href="http://topic.csdn.net/t/20050828/10/4235813.html">http://topic.csdn.net/t/20050828/10/4235813.html</a></p>
<p>在机器中<br />
负数的补码是这样算的:<br />
先将该负数取绝对值,再用二进制表示出这个绝对值<br />
对该二进制数进行取反加一操作就得到负数的补码了<br />
-128   绝对值是   128<br />
128的二进制表示为:<br />
1000   0000<br />
取反<br />
0111   1111<br />
加1<br />
1000   0000<br />
这就是-128的补码</p>
]]></content:encoded>
			<wfw:commentRss>http://fatkun.com/2010/07/java-data-type.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

