﻿<?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-The blog of Astro Qi</title><link>http://www.blogjava.net/AstroQi/</link><description /><language>zh-cn</language><lastBuildDate>Wed, 29 Apr 2026 21:54:40 GMT</lastBuildDate><pubDate>Wed, 29 Apr 2026 21:54:40 GMT</pubDate><ttl>60</ttl><item><title>对 java.nio.ByteBuffer 的理解</title><link>http://www.blogjava.net/AstroQi/archive/2011/05/18/350457.html</link><dc:creator>Astro.Qi</dc:creator><author>Astro.Qi</author><pubDate>Wed, 18 May 2011 02:53:00 GMT</pubDate><guid>http://www.blogjava.net/AstroQi/archive/2011/05/18/350457.html</guid><wfw:comment>http://www.blogjava.net/AstroQi/comments/350457.html</wfw:comment><comments>http://www.blogjava.net/AstroQi/archive/2011/05/18/350457.html#Feedback</comments><slash:comments>4</slash:comments><wfw:commentRss>http://www.blogjava.net/AstroQi/comments/commentRss/350457.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/AstroQi/services/trackbacks/350457.html</trackback:ping><description><![CDATA[<div><div id="xspace-showmessage" style="word-break: break-all; margin-right: auto; margin-left: auto; margin-top: 0.5em; margin-bottom: 0.5em; width: 554px; overflow-x: auto; overflow-y: hidden; font-family: Arial, Helvetica, sans-serif; font-size: 12px; "><span class="Apple-style-span" style="line-height: 1.8em; "><strong style="word-break: break-all; line-height: normal; "><br />今晚用到 ByteBuffer, 我跟 joy 都是初学&nbsp;<a href="http://space.itpub.net/14734416/viewspace-434476" target="_self" style="word-break: break-all; text-decoration: underline; color: #000066; line-height: normal; "><u style="word-break: break-all; line-height: normal; "><strong style="word-break: break-all; line-height: normal; ">java</strong></u></a>, 文档里的中文翻译实在是看他母亲不懂, 晕了半天, 作了几个测试, 终于把这个类的用法搞清楚了, 顺便臆想了哈其工作原理.<wbr style="word-break: break-all; line-height: normal; "><br style="word-break: break-all; line-height: normal; " /><br style="word-break: break-all; line-height: normal; " /></strong>先列点代码片段:<br style="word-break: break-all; line-height: normal; " />// ...<br style="word-break: break-all; line-height: normal; " />//<br style="word-break: break-all; line-height: normal; " />// 此段代码功能为从 t.txt 里复制所有数据到 out_j.txt:<br style="word-break: break-all; line-height: normal; " />//<br style="word-break: break-all; line-height: normal; " />...<br style="word-break: break-all; line-height: normal; " />1　FileChannel fcin = new FileInputStream( "d:/t.txt" ).getChannel();<wbr style="word-break: break-all; line-height: normal; "><br style="word-break: break-all; line-height: normal; " />2　FileChannel fcout = new FileOutputStream( new File( "d:/out_j.txt" )).getChannel();<wbr style="word-break: break-all; line-height: normal; "><br style="word-break: break-all; line-height: normal; " />3　ByteBuffer buff = ByteBuffer.allocate( 1024 );<wbr style="word-break: break-all; line-height: normal; "><br style="word-break: break-all; line-height: normal; " />4　long t1 = System.currentTimeMillis();<wbr style="word-break: break-all; line-height: normal; "><br style="word-break: break-all; line-height: normal; " />5<wbr style="word-break: break-all; line-height: normal; "><br style="word-break: break-all; line-height: normal; " />6　while( fcin.read( buff ) != -1 )<wbr style="word-break: break-all; line-height: normal; "><br style="word-break: break-all; line-height: normal; " />7　{<wbr style="word-break: break-all; line-height: normal; "><br style="word-break: break-all; line-height: normal; " />8　　　buff.flip();<wbr style="word-break: break-all; line-height: normal; "><br style="word-break: break-all; line-height: normal; " />9　　　fcout.write( buff );<wbr style="word-break: break-all; line-height: normal; "><br style="word-break: break-all; line-height: normal; " />10　　　buff.clear();<wbr style="word-break: break-all; line-height: normal; "><br style="word-break: break-all; line-height: normal; " />11　}<wbr style="word-break: break-all; line-height: normal; "><br style="word-break: break-all; line-height: normal; " />12<wbr style="word-break: break-all; line-height: normal; "><br style="word-break: break-all; line-height: normal; " />13　long t2 = System.currentTimeMillis();<wbr style="word-break: break-all; line-height: normal; "><br style="word-break: break-all; line-height: normal; " />14　long size = fcin.size();<wbr style="word-break: break-all; line-height: normal; "><br style="word-break: break-all; line-height: normal; " />15　javax.swing.JOptionPane.showMessageDialog( null, size + " 字节, 耗时 " + ( t2 - t1 + 1 ) + " ms." );<wbr style="word-break: break-all; line-height: normal; "><br style="word-break: break-all; line-height: normal; " />...<br style="word-break: break-all; line-height: normal; " /><br style="word-break: break-all; line-height: normal; " />----------------------------------------------------------------------------------------------------<wbr style="word-break: break-all; line-height: normal; "><br style="word-break: break-all; line-height: normal; " /><br style="word-break: break-all; line-height: normal; " />SDK 文档里对 ByteBuffer 的说明为:<br style="word-break: break-all; line-height: normal; " /><br style="word-break: break-all; line-height: normal; " />public abstract class ByteBuffer<br style="word-break: break-all; line-height: normal; " />extends Buffer<br style="word-break: break-all; line-height: normal; " />implements Comparable &lt;ByteBuffer&gt;<br style="word-break: break-all; line-height: normal; " /><br style="word-break: break-all; line-height: normal; " />这说明 ByteBuffer 是继承于 Buffer 的抽象类, 实现了两个接口.<br style="word-break: break-all; line-height: normal; " /><br style="word-break: break-all; line-height: normal; " />行3 通过 allocate() 分配了一块 1024 字节的缓冲区, 并返回一个 ByteBuffer 对象. (抽象类不能直接 new)<br style="word-break: break-all; line-height: normal; " />行6 fcin.read() 将数据读入到 buff. 此处的 read() 是 FileChannel 类的一个虚函数.<br style="word-break: break-all; line-height: normal; " />行8 buff.flip() 这个调用就是开头一直无法理解的部分.<br style="word-break: break-all; line-height: normal; " /><br style="word-break: break-all; line-height: normal; " />----------------------------------------------------------------------------------------------------<wbr style="word-break: break-all; line-height: normal; "><br style="word-break: break-all; line-height: normal; " /><br style="word-break: break-all; line-height: normal; " />SDK 文档里的对 flip() 的说明是:<br style="word-break: break-all; line-height: normal; " /><br style="word-break: break-all; line-height: normal; " />public final Buffer flip()<br style="word-break: break-all; line-height: normal; " />反转<wbr style="word-break: break-all; line-height: normal; ">此缓冲区。首先对当前位置<wbr style="word-break: break-all; line-height: normal; ">设置限制<wbr style="word-break: break-all; line-height: normal; ">，然后将该位置设置为零。如果已定义了标记，则丢弃该标记。<br style="word-break: break-all; line-height: normal; " />当将数据从一个地方传输到另一个地方时，经常将此方法与 compact 方法一起使用。<br style="word-break: break-all; line-height: normal; " /><br style="word-break: break-all; line-height: normal; " />我最终的理解是: 文档翻译得太差了, 把不应该翻译的内容也译成了中文, 所以反而不容易理解.<br style="word-break: break-all; line-height: normal; " />关键就在以下 2 处:<br style="word-break: break-all; line-height: normal; " /><br style="word-break: break-all; line-height: normal; " />当前位置<wbr style="word-break: break-all; line-height: normal; ">: 这个可以直观地理解为缓冲区中的当前数据指针, 或是&nbsp;<a href="http://space.itpub.net/14734416/viewspace-434476" target="_self" style="word-break: break-all; text-decoration: underline; color: #000066; line-height: normal; "><u style="word-break: break-all; line-height: normal; "><strong style="word-break: break-all; line-height: normal; ">SQL</strong></u></a>&nbsp;中的游标, 记为 curPointer.<br style="word-break: break-all; line-height: normal; " />限制<wbr style="word-break: break-all; line-height: normal; ">: 这个可以理解成实际操作的缓冲区段的结束标记, 记为 endPointer.<br style="word-break: break-all; line-height: normal; " />反转<wbr style="word-break: break-all; line-height: normal; ">: 这个完全是对 flip 这个词不负责的翻译, 如果参照 DirectX 里的 flip() 而译为翻转/翻页, 那就好理解得多, 就像写信/看信, 写/看完一页后, 翻到下一页, 眼睛/笔从页底重新移回页首.<br style="word-break: break-all; line-height: normal; " />这个翻转背后的操作其实就是 "把 endPointer 定位到 curPointer 处, 并把 curPointer 设为 0".<br style="word-break: break-all; line-height: normal; " /><br style="word-break: break-all; line-height: normal; " />关于标记, 在这里不涉及. 下一句说到常与 compact 方法一起使用, 是可以想像的, 因为 compact 方法对数据进<br style="word-break: break-all; line-height: normal; " />行了压缩, 有效数据的真实长度发生了变化, 肯定需要用 flip 重新定位结束标记.<br style="word-break: break-all; line-height: normal; " /><br style="word-break: break-all; line-height: normal; " />在填充, 压缩等数据操作时, curPointer 估计都是自动更新了位置的, 总是指向最后一个有效数据, 所以每次调<br style="word-break: break-all; line-height: normal; " />用 flip() 后, endPointer 就指向了有效数据的结尾, 而 curPointer 指向了 0 (缓冲起始处).<br style="word-break: break-all; line-height: normal; " /><br style="word-break: break-all; line-height: normal; " /><strong style="word-break: break-all; line-height: normal; "><wbr style="word-break: break-all; line-height: normal; ">举个图例:</strong><wbr style="word-break: break-all; line-height: normal; "><br style="word-break: break-all; line-height: normal; " />(c 和 e 分别代表 curPointer 和 endPointer 两个指针)<br style="word-break: break-all; line-height: normal; " /><br style="word-break: break-all; line-height: normal; " />* 先是一个空的 ByteBuffer (大小为 10 字节)<br style="word-break: break-all; line-height: normal; " />-------------------<br style="word-break: break-all; line-height: normal; " />-------------------<br style="word-break: break-all; line-height: normal; " />c<br style="word-break: break-all; line-height: normal; " />e<br style="word-break: break-all; line-height: normal; " /><br style="word-break: break-all; line-height: normal; " /><br style="word-break: break-all; line-height: normal; " />* 然后填充 5 字节数据<br style="word-break: break-all; line-height: normal; " />-------------------<br style="word-break: break-all; line-height: normal; " />0 1 2 3 4<br style="word-break: break-all; line-height: normal; " />-------------------<br style="word-break: break-all; line-height: normal; " />e &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c<br style="word-break: break-all; line-height: normal; " />此时, endPointer 尚在 0 处, curPointer 移到了数据结尾.<br style="word-break: break-all; line-height: normal; " />经测试, 此时若取数据, 将得到 5 个字节, 内容通常为 0 (也有可能是未知), 因为实际上取到的是从 c 处到缓冲</span><span class="Apple-style-span" style="line-height: 1.8em;">区实际结束处的 5 个未初始化的字节.</span><span class="Apple-style-span" style="line-height: 21px;"><br style="word-break: break-all; " /></span><br style="word-break: break-all; line-height: normal; " /><span class="Apple-style-span" style="line-height: 1.8em;">* 调用一次 flip() 后</span><br style="word-break: break-all; line-height: normal; " /><span class="Apple-style-span" style="line-height: 1.8em;">-------------------</span><br style="word-break: break-all; line-height: normal; " /><span class="Apple-style-span" style="line-height: 1.8em;">0 1 2 3 4</span><br style="word-break: break-all; line-height: normal; " /><span class="Apple-style-span" style="line-height: 1.8em;">-------------------</span><br style="word-break: break-all; line-height: normal; " /><span class="Apple-style-span" style="line-height: 1.8em;">c &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e</span><br style="word-break: break-all; line-height: normal; " /><span class="Apple-style-span" style="line-height: 1.8em;">此时, endPointer 先被移到 curPointer, 然后 curPointer 移到 0.</span><br style="word-break: break-all; line-height: normal; " /><span class="Apple-style-span" style="line-height: 1.8em;">通过测试可见, ByteBuffer 取数据时, 是从 curPointer 起, 到 endPointer 止, 若 curPointer &gt; endPointer, 则取到缓冲区结束.</span><br style="word-break: break-all; line-height: normal; " /><span class="Apple-style-span" style="line-height: 21px;"><br style="word-break: break-all; " /></span><br style="word-break: break-all; line-height: normal; " /><span class="Apple-style-span" style="line-height: 1.8em;">再看上面代码的关键片段, 行 8 处调用 flip() 即有两个作用, 一是将 curPointer 移到 0, 二是将 endPointer 移到有效数据结尾.</span><br style="word-break: break-all; line-height: normal; " /><br style="word-break: break-all; line-height: normal; " /><span class="Apple-style-span" style="line-height: 1.8em;">此行可由以下两行代替:</span><br style="word-break: break-all; line-height: normal; " /><span class="Apple-style-span" style="line-height: 1.8em;">buff.limit( buff.position());</span><br style="word-break: break-all; line-height: normal; " /><span class="Apple-style-span" style="line-height: 1.8em;">buff.position( 0 );</span><br style="word-break: break-all; line-height: normal; " /><br style="word-break: break-all; line-height: normal; " /><span class="Apple-style-span" style="line-height: 1.8em;">可见对其工作原理的理解, 应该是正确的.</span><br style="word-break: break-all; line-height: normal; " /><br style="word-break: break-all; line-height: normal; " /><span class="Apple-style-span" style="line-height: 1.8em;">----------------------------</span><wbr style="word-break: break-all; line-height: normal; "><span class="Apple-style-span" style="line-height: 1.8em;">------------------------------------------------------------------------</span><wbr style="word-break: break-all; line-height: normal; "><br style="word-break: break-all; line-height: normal; " /><br style="word-break: break-all; line-height: normal; " /><strong style="word-break: break-all; line-height: normal; "><wbr style="word-break: break-all; line-height: normal; ">总结如下:</strong><wbr style="word-break: break-all; line-height: normal; "><wbr style="word-break: break-all; line-height: normal; "><br style="word-break: break-all; line-height: normal; " /><span class="Apple-style-span" style="line-height: 1.8em;">1. put 数据时, 不会自动清除缓冲区中现有的数据.</span><br style="word-break: break-all; line-height: normal; " /><span class="Apple-style-span" style="line-height: 1.8em;">2. 每一次 get 或 put 后, curPointer 都将向缓冲区尾部移动, 移动量=操作的数据量.</span><br style="word-break: break-all; line-height: normal; " /><span class="Apple-style-span" style="line-height: 1.8em;">3. get/put 均是从 curPointer 起, 到 curPointer + 操作的数据长度止.</span><br style="word-break: break-all; line-height: normal; " /><span class="Apple-style-span" style="line-height: 1.8em;">4. get/put 操作中, 若 curPointer 超过了 endPointer 或缓冲区总长度, 将抛出 java.nio.BufferUnderflowException 异常.</span><br style="word-break: break-all; line-height: normal; " /><br style="word-break: break-all; line-height: normal; " /><span class="Apple-style-span" style="line-height: 1.8em;">注: curPointer 和 endPointer 只是为文中方便描述命名的, 实际分别对应到 ByteBuffer.position() 和 ByteBuffer.limit() 两个方法.</span><br style="word-break: break-all; line-height: normal; " /><br style="word-break: break-all; line-height: normal; " /><span class="Apple-style-span" style="line-height: 1.8em;">----------------------------------------------------------------------------------------------------</span><wbr style="word-break: break-all; line-height: normal; "><br style="word-break: break-all; line-height: normal; " /><br style="word-break: break-all; line-height: normal; " /><strong style="word-break: break-all; line-height: normal; "><wbr style="word-break: break-all; line-height: normal; ">疑惑:</strong><wbr style="word-break: break-all; line-height: normal; "><wbr style="word-break: break-all; line-height: normal; "><br style="word-break: break-all; line-height: normal; " /><span class="Apple-style-span" style="line-height: 1.8em;">curPointer 是用 ByteBuffer.position() 取值, 用 ByteBuffer.position( int ) 赋值, 不知道 JDK 为什么要用多态来实现这两个功能, 按我的想法,&nbsp;</span><a href="http://space.itpub.net/14734416/viewspace-434476" target="_self" style="word-break: break-all; text-decoration: underline; color: #000066; line-height: normal; "><u style="word-break: break-all; line-height: normal; "><strong style="word-break: break-all; line-height: normal; ">设计</strong></u></a><span class="Apple-style-span" style="line-height: 1.8em;">成 getPosition(), setPosition() 不是要好看好记得多啊.</span><br style="word-break: break-all; line-height: normal; " /><br style="word-break: break-all; line-height: normal; " /><span class="Apple-style-span" style="line-height: 1.8em;">----------------------------------------------------------------------------------------------------</span><wbr style="word-break: break-all; line-height: normal; "><br style="word-break: break-all; line-height: normal; " /><br style="word-break: break-all; line-height: normal; " /><strong style="word-break: break-all; line-height: normal; "><wbr style="word-break: break-all; line-height: normal; ">跟 C++ 的简单比较:<wbr style="word-break: break-all; line-height: normal; "></strong><wbr style="word-break: break-all; line-height: normal; "><br style="word-break: break-all; line-height: normal; " /><span class="Apple-style-span" style="line-height: 1.8em;">C++ 里面没有类似 ByteBuffer 的现成实现, 实现上述类似的文件复制功能, 通常要自己创建</span><a href="http://space.itpub.net/14734416/viewspace-434476" target="_self" style="word-break: break-all; text-decoration: underline; color: #000066; line-height: normal; "><u style="word-break: break-all; line-height: normal; "><strong style="word-break: break-all; line-height: normal; ">管理</strong></u></a><span class="Apple-style-span" style="line-height: 1.8em;">缓冲区. C++ 里读写文件通常用 FileRead(), FileWrite() 函数, 在读/写的时候, 可以直接指定读/写的数据长度, 相比下显得</span><br style="word-break: break-all; line-height: normal; " /><span class="Apple-style-span" style="line-height: 1.8em;">直观方便些, 但 JDK 这个 ByteBuffer 的方式, 确实更方便好用.</span><br style="word-break: break-all; line-height: normal; " /><br style="word-break: break-all; line-height: normal; " /><span class="Apple-style-span" style="line-height: 1.8em;">ByteBuffer 作为继承自 Buffer 的抽象类, 实现了对 Byte 型缓冲的管理, 同时 JDK 里还有对应其他数据类型的</span><br style="word-break: break-all; line-height: normal; " /><span class="Apple-style-span" style="line-height: 1.8em;">继承自 Buffer 的抽象类, 分别实现对应类型的缓冲管理. 这种设计减少了编程时的工作. 如果在 C++ 中, 调用</span><br style="word-break: break-all; line-height: normal; " /><span class="Apple-style-span" style="line-height: 1.8em;">读/写函数时, 还需要考虑传入数据的类型, 通常用传入 sizeof(数据类型) 的方式指定, 除了函数调用时增加耗</span><br style="word-break: break-all; line-height: normal; " /><span class="Apple-style-span" style="line-height: 1.8em;">费外, 灵活性也更差些.</span><br style="word-break: break-all; line-height: normal; " /><br style="word-break: break-all; line-height: normal; " /><span class="Apple-style-span" style="line-height: 1.8em;">反过来再想, 为什么 C 要用这种方式? 个人认为, 这是 C 标准库的实现方式, 因为在不同 OS 平台上, 对文件和</span><br style="word-break: break-all; line-height: normal; " /><span class="Apple-style-span" style="line-height: 1.8em;">设备的访问方法在系统层不一定相同, 同时硬件平台(主要是 CPU)上基本数据类型宽度也有可能不同, 标准库通过</span><br style="word-break: break-all; line-height: normal; " /><span class="Apple-style-span" style="line-height: 1.8em;">sizeof 这个宏在编译时才能确定数据宽度, 所以标准 C 代码通常可以在不同平台上重新编译.</span><br style="word-break: break-all; line-height: normal; " /><br style="word-break: break-all; line-height: normal; " /><span class="Apple-style-span" style="line-height: 1.8em;">再想 C++ 为什么要用这种方式? 首先, C++ 里沿用 C 标准库的模式是可以理解的, 其次, 也许只是 C++ 标准库</span><br style="word-break: break-all; line-height: normal; " /><span class="Apple-style-span" style="line-height: 1.8em;">里没有类似的设计, 说不定早就有第三方通过模板实现的了.</span><br style="word-break: break-all; line-height: normal; " /><br style="word-break: break-all; line-height: normal; " /><span class="Apple-style-span" style="line-height: 1.8em;">个人认为, ByteBuffer 在实现上, 可以算是一种数据结构, 在类设计上, 可以算是一种设计模式了.</span></div></div><img src ="http://www.blogjava.net/AstroQi/aggbug/350457.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/AstroQi/" target="_blank">Astro.Qi</a> 2011-05-18 10:53 <a href="http://www.blogjava.net/AstroQi/archive/2011/05/18/350457.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>hibernate集合映射cascade和inverse详解</title><link>http://www.blogjava.net/AstroQi/archive/2011/04/22/348779.html</link><dc:creator>Astro.Qi</dc:creator><author>Astro.Qi</author><pubDate>Fri, 22 Apr 2011 02:51:00 GMT</pubDate><guid>http://www.blogjava.net/AstroQi/archive/2011/04/22/348779.html</guid><wfw:comment>http://www.blogjava.net/AstroQi/comments/348779.html</wfw:comment><comments>http://www.blogjava.net/AstroQi/archive/2011/04/22/348779.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/AstroQi/comments/commentRss/348779.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/AstroQi/services/trackbacks/348779.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: １、到底在哪用cascade="..."？cascade属性并不是多对多关系一定要用的，有了它只是让我们在插入或删除对像时更方便一些，只要在cascade的源头上插入或是删除，所有cascade的关系就会被自己动的插入或是删除。便是为了能正确的cascade，unsaved-value是个很重要的属性。Hibernate通过这个属性来判断一个对象应该save还是update，如果这个对象的id...&nbsp;&nbsp;<a href='http://www.blogjava.net/AstroQi/archive/2011/04/22/348779.html'>阅读全文</a><img src ="http://www.blogjava.net/AstroQi/aggbug/348779.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/AstroQi/" target="_blank">Astro.Qi</a> 2011-04-22 10:51 <a href="http://www.blogjava.net/AstroQi/archive/2011/04/22/348779.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Hibernate中cascade与inverse属性详解</title><link>http://www.blogjava.net/AstroQi/archive/2011/04/22/348777.html</link><dc:creator>Astro.Qi</dc:creator><author>Astro.Qi</author><pubDate>Fri, 22 Apr 2011 02:47:00 GMT</pubDate><guid>http://www.blogjava.net/AstroQi/archive/2011/04/22/348777.html</guid><wfw:comment>http://www.blogjava.net/AstroQi/comments/348777.html</wfw:comment><comments>http://www.blogjava.net/AstroQi/archive/2011/04/22/348777.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/AstroQi/comments/commentRss/348777.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/AstroQi/services/trackbacks/348777.html</trackback:ping><description><![CDATA[<p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">在学习Hibernate的过程中最不好理解的就是这两个属性了。<br style="line-height: normal; " />
(我当初学习Hibernate的时候，发现网上介绍这两个属性的文章倒是不少，但是，居然有好多都是转帖。。。还有的就是 照书搬~~-_-!!!)。。。</span></span></span></span></span></span></p>
<p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">据个例子：书上说inverse=false时，由主控方维持关系。。。<br style="line-height: normal; " />
由于我也是初学者。。。再加上语文水平偏低。。。不理解&#8220;维持关系是啥意思&#8221;囧~</span></span></span></span></span></span></p>
<p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">提示：<br style="line-height: normal; " />
(1)如果：您不了解Hibernate的one-to-many或many-to-one的概念。</span></span></span></span></span></span></p>
<p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">(2)如果：你不了解Hibernate的&#8220;自由态&#8221;&#8220;持久态&#8221;&#8220;游离态&#8221;的概念。</span></span></span></span></span></span></p>
<p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">(3)如果：您不了解Hibernate中的&#8220;脏数据&#8221;的概念。</span></span></span></span></span></span></p>
<p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">(4)如果：您对Hibernate中Session缓存，没有初步了解的话。<br style="line-height: normal; " />
(在Hibernate中调用save进行存储数据的时候,并不是马上就对数据库进行insert操作，而是会将其&#8220;数据对象(vo)&#8221;纳入Hibernate的Session缓存。)</span></span></span></span></span></span></p>
<p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">在上面的4条提示中，如果您对其中的某一条，不是很清楚的话。希望请先了解有关知识。<br style="line-height: normal; " />
否则，可能您将 &#8220;无法或很难&#8221;理解 cascade 或 inverse 这2个属性。</span></span></span></span></span></span></p>
<p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">首相，cascade 与 inverse 这两个属性，其实是完全不同的两个东西，想要了解他们各自的&#8220;用途与区别&#8221;，详见如下介绍：</span></span></span></span></span></span></p>
<p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">这里有两个表:</span></span></span></span></span></span></p>
<p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">（1）class&nbsp;&nbsp; (班级表)<br style="line-height: normal; " />
相应字段：<br style="line-height: normal; " />
cid&nbsp;&nbsp;&nbsp; varchar(32) 主键 not-null (班级id)<br style="line-height: normal; " />
cname varchar(16)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; not-null (班级名称)&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />
<br style="line-height: normal; " />
（2）student (学生表)<br style="line-height: normal; " />
相应字段：<br style="line-height: normal; " />
sid&nbsp;&nbsp;&nbsp; varchar(32) 主键 not-null (学生id)<br style="line-height: normal; " />
sname varchar(16)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; not-null (学生姓名)<br style="line-height: normal; " />
class_id varchar(32)&nbsp;&nbsp; not-null (学生所属班级)</span></span></span></span></span></span></p>
<p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">一个班级(class)对应多个学生(student)，所以班级表(class)就是&#8220;one-to-many&#8221;端<br style="line-height: normal; " />
反之student就是many-to-one</span></span></span></span></span></span></p>
<p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">//--------Class类的代码--------<br style="line-height: normal; " />
public class Class implements.....<br style="line-height: normal; " />
{<br style="line-height: normal; " />
private cId = "";<br style="line-height: normal; " />
private cName = "";<br style="line-height: normal; " />
private students = java.util.HashMap();<br style="line-height: normal; " />
// 省略对应的 geter setter<br style="line-height: normal; " />
}<br style="line-height: normal; " />
//--------Class.hbm.xml--------<br style="line-height: normal; " />
&lt;hibernate-mapping&gt;<br style="line-height: normal; " />
&lt;class name="lcx.vo.Class" table="class"<br style="line-height: normal; " />
&nbsp;&nbsp; catalog="demo"&gt;<br style="line-height: normal; " />
&nbsp;&nbsp; &lt;id name="cid" type="java.lang.String"&gt;<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; &lt;column name="cid" length="32" /&gt;<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; &lt;generator class="uuid.hex" /&gt;<br style="line-height: normal; " />
&nbsp;&nbsp; &lt;/id&gt;<br style="line-height: normal; " />
&nbsp;&nbsp; &lt;property name="name" type="java.lang.String"&gt;<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; &lt;column name="cname" length="16" not-null="true" /&gt;<br style="line-height: normal; " />
&nbsp;&nbsp; &lt;/property&gt;<br style="line-height: normal; " />
&nbsp;&nbsp;<br style="line-height: normal; " />
&nbsp;&nbsp; &lt;set name="students" table="student" cascade="save-update"&gt;<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; &lt;key column="class" /&gt;<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; &lt;one-to-many class="lcx.vo.Student" /&gt;<br style="line-height: normal; " />
&nbsp;&nbsp; &lt;/set&gt;&nbsp;<br style="line-height: normal; " />
&lt;/class&gt;<br style="line-height: normal; " />
&lt;/hibernate-mapping&gt;</span></span></span></span></span></span></p>
<p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">//--------Student类的代码;*******<br style="line-height: normal; " />
public class Student implements.....<br style="line-height: normal; " />
{<br style="line-height: normal; " />
private sId = "";<br style="line-height: normal; " />
private sName = "";<br style="line-height: normal; " />
private Class class = null;<br style="line-height: normal; " />
// 省略对应的 geter setter<br style="line-height: normal; " />
}<br style="line-height: normal; " />
// Student.hbm.xml<br style="line-height: normal; " />
&lt;hibernate-mapping&gt;<br style="line-height: normal; " />
&lt;class name="lcx.vo.Student" table="student" catalog="demo"&gt;<br style="line-height: normal; " />
&nbsp;&nbsp; &lt;id name="cid" type="java.lang.String"&gt;<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; &lt;column name="sid" length="32" /&gt;<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; &lt;generator class="uuid.hex" /&gt;<br style="line-height: normal; " />
&nbsp;&nbsp; &lt;/id&gt;<br style="line-height: normal; " />
&nbsp;&nbsp; &lt;many-to-one name="class"&nbsp;<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; class="lcx.vo.Class"<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; column="class_id"&nbsp;<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; not-null="true"&nbsp;<br style="line-height: normal; " />
&nbsp;&nbsp; /&gt;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />
&lt;/class&gt;<br style="line-height: normal; " />
&lt;/hibernate-mapping&gt;</span></span></span></span></span></span></p>
<p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">（一） cascade 的介绍：<br style="line-height: normal; " />
当Hibernate持久化一个&#8220;临时对象(也叫自由态对象)&#8221;时，在默认的情况下(即：没有设置cascade属性或cascade=none时)，Hibernate不会自动&#8220;持久化他所关联&#8221;的其他临时对象。</span></span></span></span></span></span></p>
<p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">上面这些话是什么意思呢？ 什么叫不会自动 &#8220;持久化&#8221;关联的临时对象呢？</span></span></span></span></span></span></p>
<p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">看如下代码：</span></span></span></span></span></span></p>
<p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">// 创建一个 临时对象(也叫自由态对象)&nbsp;<br style="line-height: normal; " />
// 也就是说这个 class 没有被Hibernate纳入Session缓存管理。<br style="line-height: normal; " />
Class class = new Class();<br style="line-height: normal; " />
//class.id 为自动生成<br style="line-height: normal; " />
class.setName("一年级1班");</span></span></span></span></span></span></p>
<p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">Student stu = new Student();<br style="line-height: normal; " />
//student.id 为自动生成<br style="line-height: normal; " />
stu.setName("小白兔");<br style="line-height: normal; " />
stu.setClass(class);</span></span></span></span></span></span></p>
<p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">// 关键就是这里。。。<br style="line-height: normal; " />
class.getStudents().add(stu);</span></span></span></span></span></span></p>
<p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">session.save(class);<br style="line-height: normal; " />
// 提交</span></span></span></span></span></span></p>
<p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">// 注意: Class.hbm.xml文件中,cascade="save-update"并且也没有设置inverse属性,也就是说inverse=false;<br style="line-height: normal; " />
// 此时如果你开启了Hibernate的显示HQL语句功能，那么控制台将会显示如下3条HQL：</span></span></span></span></span></span></p>
<p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">//----------------------------------------********<br style="line-height: normal; " />
insert into demo.class (cid, cname) values (66666666666666666666666666666666, 一年级1班)<br style="line-height: normal; " />
insert into demo.student (sid,sname,class_id) values (8888888888888888811cb2e04c888888, 小白兔, 66666666666666666666666666666666)<br style="line-height: normal; " />
update demo.student set class_id=66666666666666666666666666666666 where sid=8888888888888888811cb2e04c888888<br style="line-height: normal; " />
//----------------------------------------********</span></span></span></span></span></span></p>
<p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">那么为什么会出现，这3条HQL语句呢，我们来一一分析一下：</span></span></span></span></span></span></p>
<p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">第1条HQL语句:<br style="line-height: normal; " />
其实第一条HQL比较好理解，<br style="line-height: normal; " />
当我们调用 session.save(class) 后，在Hibernate进行提交的时候，<br style="line-height: normal; " />
会发现&#8220;有&#8221;一条&#8220;新&#8221;的数据要插入(insert)，所以就往class表中,插入了这条新的class记录。</span></span></span></span></span></span></p>
<p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">第2条HQL语句：<br style="line-height: normal; " />
注意问题就在这里：<br style="line-height: normal; " />
这里为什么又出现了一条insert语句呢？而且还是向student表中插入数据。<br style="line-height: normal; " />
我们在上面的代码中，并没有编写类似&#8220;session.save(student)&#8221;这样的语句啊。<br style="line-height: normal; " />
这是为什么呢？<br style="line-height: normal; " />
其实原因，是这么回事：因为我们在class端，设置了"级联更新"(即:cascade="save-update")，<br style="line-height: normal; " />
也就是说，当Hibernate在向class表中插入&#8220;新&#8221;对象记录时，会检查&#8220;Class对象&#8221;所关联的属性(就是&lt;set&gt;对应的属性)，是否发生过变化，如果发生了变化，就按照&#8220;级联属性(cascade)&#8221;所设定的内容<br style="line-height: normal; " />
进行操作。</span></span></span></span></span></span></p>
<p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">上面讲的这句话到底是什么意思呢？<br style="line-height: normal; " />
用你们&#8220;人&#8221;话说，就是：<br style="line-height: normal; " />
因为调用了 class.getStudents().add(stu);<br style="line-height: normal; " />
所以，在Hibernate在进行插入 class对象的时候，发现class对象，所关联的集合中，有一条<br style="line-height: normal; " />
&#8220;自由态&#8221;的对象，而又因为class端设置了&#8220;级联属性cascade&#8221;，所以，在插入这条 &#8220;新class对象&#8221;时，也一同把他内部的那些，还属于&#8220;自由态&#8221;的其他对象，也一同插入到，他们所对应的表中去了。</span></span></span></span></span></span></p>
<p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">还是不明白的话。。。可以看看。孙卫琴的《精通Hibernate》，在书上的第149页有。<br style="line-height: normal; " />
但是关于inverse的介绍。。。写的就有些书面化了，如果语文不好的话。。。就难懂咯~</span></span></span></span></span></span></p>
<p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">第3条HQL语句：<br style="line-height: normal; " />
第三条HQL语句是一条update语句，是不是觉得，很莫名其妙。。。。<br style="line-height: normal; " />
Hibernate大脑进水了吧，怎么吃饱了撑得，重复更新记录啊啊啊啊啊<br style="line-height: normal; " />
假如：我们把 class端的配置文档中的 invser属性设置为true(即：inverse=true)<br style="line-height: normal; " />
在执行上面的程序，发现，就变成2条insert语句啦。。。。。(update没啦。。。)<br style="line-height: normal; " />
看来第三条的update语句和inverse有着密切的关系（他两有一腿~）。</span></span></span></span></span></span></p>
<p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">所以我们下边，就来介绍一下inverse属性:</span></span></span></span></span></span></p>
<p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">当调用 Class.getStudents().add(stu)方法，进行添加操作时，&nbsp;<br style="line-height: normal; " />
(即：向 "这个Class对象"所属的&#8220;集合 (也就是调用getStudents方法所返回的那个Set集合)&#8221;中添加一个Student(即 add(stu))，也就是说，这个&#8220;新&#8221;添加的Student对象(stu)，&nbsp;<br style="line-height: normal; " />
他的Student.class_id字段&#8220;必须&#8221;，要等于&#8220;被添加方Class&#8221;的主键(即:Class.cid)。&nbsp;<br style="line-height: normal; " />
从&#8220;数据库&#8221;层面来讲，也就是说，这个&#8220;新&#8221;添加的&#8220;Student&#8221;的class_id字段，必须要与&#8220;Class&#8221;的cid字段，存在"主外键关联"。)</span></span></span></span></span></span></p>
<p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">正因为如此：所以Hibernate&#8220;怕&#8221; 在进行 "Class.getStudents().add(stu)" 这样的操作时，&nbsp;<br style="line-height: normal; " />
出现意外情况(如: stu.getClass=null，即：stu没有所属班级)，<br style="line-height: normal; " />
即&#8220;添加方&#8221;(Student)与&#8220;被添加方&#8221;(Class)，存在&#8220;外键&#8221;不一致的情况发生。&nbsp;<br style="line-height: normal; " />
所以就出现了 那条多余的update语句。即：one-to-many（Class端）主动去维护Child.Class_id&nbsp;<br style="line-height: normal; " />
所以就是说，Hibernate怕出错，就给你多执行一次无用的更新语句，以保证 add 到 Class&#8220;集合&#8221;中的所有Student<br style="line-height: normal; " />
都是要与Class有外键关联的。</span></span></span></span></span></span></p>
<p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">用普通话说就是:<br style="line-height: normal; " />
一年1班.getStudents().add(小白兔);<br style="line-height: normal; " />
一年1班.getStudents().add(大白兔);</span></span></span></span></span></span></p>
<p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">也就是说现在不管是 小白兔 还是 大白兔&nbsp;<br style="line-height: normal; " />
如果他们，目前还没有自己的班级的话，<br style="line-height: normal; " />
一年1班的班主任就会主动邀请他们成为一年1班的同学啦~。</span></span></span></span></span></span></p>
<p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">也就是说 一年1班的班主任 主动邀请 同学，而不是 同学自己来~~~ 所以效率也降低了。。。。</span></span></span></span></span></span></p>
<p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">所以我们一般把 一对多端 invser设置为true，即：不让主控端去维护主键关联，<br style="line-height: normal; " />
（即：让同学自己去找班级）<br style="line-height: normal; " />
说白了，就是，one-to-many端不用去管理 &#8220;新添加对象&#8221; 的主外键约束问题。</span></span></span></span></span></span></p>
<p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">把one-to-many端（即:class端)的invser设置为true<br style="line-height: normal; " />
(即：每次向class.getStudents这个集合中添加 student时，不去主动update对应的外键)，<br style="line-height: normal; " />
而是在student端去手动设置<br style="line-height: normal; " />
例如：<br style="line-height: normal; " />
student.setClass(class);<br style="line-height: normal; " />
session.save(student);<br style="line-height: normal; " />
这样手动设置 student与class关联啦。。。。<br style="line-height: normal; " />
所以上面的程序&#8220;最好&#8221;还是写成这样：</span></span></span></span></span></span></p>
<p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">Class class = new Class();<br style="line-height: normal; " />
class.setName("一年级1班");<br style="line-height: normal; " />
session.save(class);</span></span></span></span></span></span></p>
<p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">Student stu = new Student();<br style="line-height: normal; " />
stu.setName("小白兔");<br style="line-height: normal; " />
stu.setClass(class);<br style="line-height: normal; " />
session.save(class);</span></span></span></span></span></span></p>
<p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">/*<br style="line-height: normal; " />
此时向class集合add内容，不会进行数据库操作(update)。<br style="line-height: normal; " />
&#8220;更新&#8221;的只是session缓存中，数据镜像。<br style="line-height: normal; " />
这样做的好处是：不仅减少了update语句，<br style="line-height: normal; " />
而且，同时也更新了session缓存。<br style="line-height: normal; " />
------------------------<br style="line-height: normal; " />
而在原来:<br style="line-height: normal; " />
one-to-many端inverse=false时，虽然也更新seesion缓存中的class集合，<br style="line-height: normal; " />
但是有却又多余update<br style="line-height: normal; " />
*/<br style="line-height: normal; " />
class.getStudents().add(stu);<br style="line-height: normal; " />
// 提交</span></span></span></span></span></span></p>
<p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">总结:<br style="line-height: normal; " />
当inverse=false 并且向one-to-many端的关联集合，添加&#8220;新对象(即: 自由态对象)&#8221; 时，<br style="line-height: normal; " />
Hibernate就会自动，去update那&#8220;个刚刚到来的&#8221; &#8220;自由态对象&#8221;的外键。<br style="line-height: normal; " />
（如果你向，one-to-many端添的集合中，add一个&#8220;已经持久化了的对象&#8221;，那就不会出现update了(因为已经持久化过了)，除非，你去 更改&#8220;那个持久化对象&#8221;所对应的外键。。。那样的话。。。呵呵呵~~~<br style="line-height: normal; " />
你可以试一试，应该不会报错，你可以当做练习去做一下，加深cascade和inverse这两个属性的理解）</span></span></span></span></span></span></p>
<p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><br style="line-height: normal; " />
<span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">// 如果看懂了上面的内容。来看一下，下面的东西。<br style="line-height: normal; " />
假如，将one-to-many端(即:Class端)的 hbm.xml 文档中的cascade移除掉 或把cascade="none"。<br style="line-height: normal; " />
那么上面的代码会出现什么情况呢。<br style="line-height: normal; " />
结果会出现2条HQL，和一堆Exception</span></span></span></span></span></span></p>
<p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">insert into demo.class (cid, cname) values (66666666666666666666666666666666, 一年级1班)<br style="line-height: normal; " />
update demo.student set class_id=66666666666666666666666666666666 where sid=8888888888888888811cb2e04c888888<br style="line-height: normal; " />
Hibernate Exceptinon......................................</span></span></span></span></span></span></p>
<p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">相比较cascade被设置"save-update"的时候，缺少了1条 insert语句，而且也多了一些Exception。</span></span></span></span></span></span></p>
<p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">那么，到底是少了哪1条insert语句呢？<br style="line-height: normal; " />
就是这条：<br style="line-height: normal; " />
insert into demo.student (sid,sname,class_id) values (8888888888888888811cb2e04c888888, 小白兔, 66666666666666666666666666666666)</span></span></span></span></span></span></p>
<p style="line-height: normal; color: #666666; font-family: Arial; font-size: 12px; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 14pt; "><span style="line-height: normal; font-size: 10pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 12pt; "><span style="line-height: normal; font-size: 10pt; ">之所以会出现，这样的现象，想必您已经早就看出来了。<br style="line-height: normal; " />
因为，我没有设置Class端的Cascade，所以在save(class)的时候，并没有自动将其所关联的&#8220;自由态对象&#8221;进行持久化操作。<br style="line-height: normal; " />
然而，又因为 Class端的inverse=false，所以，Class会自动去维持，那个 &#8220;新来的student&#8221; 的外键。<br style="line-height: normal; " />
所以会出现，没有insert就要update啦。。。。<br style="line-height: normal; " />
然后在就是Exception了</span></span></span></span></span></span></p>
<img src ="http://www.blogjava.net/AstroQi/aggbug/348777.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/AstroQi/" target="_blank">Astro.Qi</a> 2011-04-22 10:47 <a href="http://www.blogjava.net/AstroQi/archive/2011/04/22/348777.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Android各种屏幕尺寸</title><link>http://www.blogjava.net/AstroQi/archive/2011/04/21/348698.html</link><dc:creator>Astro.Qi</dc:creator><author>Astro.Qi</author><pubDate>Thu, 21 Apr 2011 05:35:00 GMT</pubDate><guid>http://www.blogjava.net/AstroQi/archive/2011/04/21/348698.html</guid><wfw:comment>http://www.blogjava.net/AstroQi/comments/348698.html</wfw:comment><comments>http://www.blogjava.net/AstroQi/archive/2011/04/21/348698.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/AstroQi/comments/commentRss/348698.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/AstroQi/services/trackbacks/348698.html</trackback:ping><description><![CDATA[<div style="font-family: 'Comic Sans MS', 宋体, Arial, sans-serif; font-size: 13px; line-height: 19px; ">QVGA即"Quarter VGA"。顾名思义即VGA的四分之一尺寸，亦即在液晶屏幕（LCD）上输出的分辨率是240&#215;320像素。QVGA支持屏幕旋转，可以开发出相应的程序，以显示旋转90&#176;、180&#176;、270&#176;屏幕位置。由HandEra公司发布。多用于手持/移动设备。&nbsp;<br />
需要说明的是有些媒体把QVGA屏幕当成与TFT和TFD等LCD材质相同的东西是错误的，QVGA屏幕的说法多见与日本的一些手机中，目前采用微软Pocket PC操作系统的智能手机屏幕也大多是320&#215;240像素的QVGA屏幕。&nbsp;<br />
所谓QVGA液晶技术，就是在液晶屏幕上输出的分辨率是240&#215;320的液晶输出方式。这个分辨率其实和屏幕本身的大小并没有关系。比如说，如果2.1英寸液晶显示屏幕可以显示240&#215;320分辨率的图像，就叫做&#8220;QVGA 2.1英寸液晶显示屏&#8221;；如果3.8英寸液晶显示屏幕可以显示240&#215;320的图像，就叫做&#8220;QVGA 3.8英寸液晶显示屏&#8221;，以上两种情况虽然具有相同的分辨率，但是由于尺寸的不同实际的视觉效果也不同，一般<br />
<br />
HVGA 即VGA(640*480)的一半,分辨率为(480*320),(3:2宽高比)<br />
它是用于各种各样的PDA设备，首先是2002年的索尼Clie PEG - NR70，<br />
来说屏幕小的一个画面自然也会细腻一些。<br />
<br />
WVGA 数码产品屏幕材质的一种，VGA的另一种形式，比VGA分辨率高，别名 ： Wide VGA, ，其分辩率为800&#215;480象素。是扩大了VGA（640&#215;480)的分辨率。应用于PDA和手机等，因为很多网页的宽度都是800，所以WVGA的屏幕会更加适和于浏览网页，可以说是未来手持设备的分辨率的大趋势</div>
<p style="font-size: 13px; line-height: 1.4; margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: 'Comic Sans MS', 宋体, Arial, sans-serif; "><br />
</p>
<p style="font-size: 13px; line-height: 1.4; margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: 'Comic Sans MS', 宋体, Arial, sans-serif; ">&nbsp;</p>
<div style="font-family: 'Comic Sans MS', 宋体, Arial, sans-serif; font-size: 13px; line-height: 19px; "><span style="line-height: 1.5; font-size: small; "><strong><span style="line-height: 1.5; font-size: 13px; font-family: 宋体; ">术语和概念</span></strong></span>
<table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse; ">
    <tbody>
        <tr>
            <td valign="top" width="155" style="font-size: 12px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; padding-top: 0cm; padding-right: 5.4pt; padding-bottom: 0cm; padding-left: 5.4pt; width: 116.35pt; background-color: transparent; ">
            <p align="center" style="font-size: 13px; line-height: 1.4; margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span style="line-height: 1.5; font-size: 13px; font-family: 宋体; "><span style="line-height: 1.5; font-size: small; ">术语</span></span></p>
            </td>
            <td valign="top" width="280" style="font-size: 12px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; padding-top: 0cm; padding-right: 5.4pt; padding-bottom: 0cm; padding-left: 5.4pt; width: 210pt; background-color: transparent; ">
            <p align="center" style="font-size: 13px; line-height: 1.4; margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span style="line-height: 1.5; font-size: 13px; font-family: 宋体; "><span style="line-height: 1.5; font-size: small; ">说明</span></span></p>
            </td>
            <td valign="top" width="133" style="font-size: 12px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; padding-top: 0cm; padding-right: 5.4pt; padding-bottom: 0cm; padding-left: 5.4pt; width: 99.75pt; background-color: transparent; ">
            <p align="center" style="font-size: 13px; line-height: 1.4; margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span style="line-height: 1.5; font-size: 13px; font-family: 宋体; "><span style="line-height: 1.5; font-size: small; ">备注</span></span></p>
            </td>
        </tr>
        <tr>
            <td valign="top" width="155" style="font-size: 12px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; padding-top: 0cm; padding-right: 5.4pt; padding-bottom: 0cm; padding-left: 5.4pt; width: 116.35pt; background-color: transparent; ">
            <p style="font-size: 13px; line-height: 1.4; margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span style="line-height: 1.5; font-size: small; "><span style="line-height: 1.5; font-size: 13px; font-family: 宋体; ">Screen size</span><span style="line-height: 1.5; font-size: 13px; font-family: 宋体; ">（屏幕尺寸)</span></span></p>
            </td>
            <td valign="top" width="280" style="font-size: 12px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; padding-top: 0cm; padding-right: 5.4pt; padding-bottom: 0cm; padding-left: 5.4pt; width: 210pt; background-color: transparent; ">
            <p style="font-size: 13px; line-height: 1.4; margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span style="line-height: 1.5; font-size: 13px; font-family: 宋体; "><span style="line-height: 1.5; font-size: small; ">指的是手机实际的物理尺寸，比如常用的2.8英寸，3.2英寸，3.5英寸，3.7英寸</span></span></p>
            </td>
            <td valign="top" width="133" style="font-size: 12px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; padding-top: 0cm; padding-right: 5.4pt; padding-bottom: 0cm; padding-left: 5.4pt; width: 99.75pt; background-color: transparent; ">
            <p style="font-size: 13px; line-height: 1.4; margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span style="line-height: 1.5; font-size: 13px; font-family: 宋体; "><span style="line-height: 1.5; font-size: small; ">摩托罗拉milestone手机是3.7英寸</span></span></p>
            </td>
        </tr>
        <tr>
            <td valign="top" width="155" style="font-size: 12px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; padding-top: 0cm; padding-right: 5.4pt; padding-bottom: 0cm; padding-left: 5.4pt; width: 116.35pt; background-color: transparent; ">
            <p style="font-size: 13px; line-height: 1.4; margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span style="line-height: 1.5; font-size: small; "><span style="line-height: 1.5; font-size: 13px; font-family: 'Times New Roman'; ">Aspect Ratio(</span><span style="line-height: 1.5; font-size: 13px; font-family: 宋体; ">宽高比率)</span></span></p>
            </td>
            <td valign="top" width="280" style="font-size: 12px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; padding-top: 0cm; padding-right: 5.4pt; padding-bottom: 0cm; padding-left: 5.4pt; width: 210pt; background-color: transparent; ">
            <p style="font-size: 13px; line-height: 1.4; margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span style="line-height: 1.5; font-size: 13px; font-family: 宋体; "><span style="line-height: 1.5; font-size: small; ">指的是实际的物理尺寸宽高比率,分为long和nolong</span></span></p>
            </td>
            <td valign="top" width="133" style="font-size: 12px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; padding-top: 0cm; padding-right: 5.4pt; padding-bottom: 0cm; padding-left: 5.4pt; width: 99.75pt; background-color: transparent; ">
            <p style="font-size: 13px; line-height: 1.4; margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span style="line-height: 1.5; font-size: small; "><span style="line-height: 1.5; font-size: 13px; font-family: 宋体; ">Milestone</span><span style="line-height: 1.5; font-size: 13px; font-family: 宋体; ">是16：9,属于long</span></span></p>
            </td>
        </tr>
        <tr>
            <td valign="top" width="155" style="font-size: 12px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; padding-top: 0cm; padding-right: 5.4pt; padding-bottom: 0cm; padding-left: 5.4pt; width: 116.35pt; background-color: transparent; ">
            <p style="font-size: 13px; line-height: 1.4; margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span style="line-height: 1.5; font-size: small; "><span style="line-height: 1.5; font-size: 13px; font-family: 'Times New Roman'; ">R</span><span style="line-height: 1.5; font-size: 13px; font-family: 宋体; ">esolution(</span><span style="line-height: 1.5; font-size: 13px; font-family: 宋体; ">分辨率)</span></span></p>
            </td>
            <td valign="top" width="280" style="font-size: 12px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; padding-top: 0cm; padding-right: 5.4pt; padding-bottom: 0cm; padding-left: 5.4pt; width: 210pt; background-color: transparent; ">
            <p style="font-size: 13px; line-height: 1.4; margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span style="line-height: 1.5; font-size: 13px; font-family: 宋体; "><span style="line-height: 1.5; font-size: small; ">和电脑的分辨率概念一样，指手机屏幕纵、横方向像素个数</span></span></p>
            </td>
            <td valign="top" width="133" style="font-size: 12px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; padding-top: 0cm; padding-right: 5.4pt; padding-bottom: 0cm; padding-left: 5.4pt; width: 99.75pt; background-color: transparent; ">
            <p style="font-size: 13px; line-height: 1.4; margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span style="line-height: 1.5; font-size: small; "><span style="line-height: 1.5; font-size: 13px; font-family: 宋体; ">Milestone</span><span style="line-height: 1.5; font-size: 13px; font-family: 宋体; ">是854*480</span></span></p>
            </td>
        </tr>
        <tr>
            <td valign="top" width="155" style="font-size: 12px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; padding-top: 0cm; padding-right: 5.4pt; padding-bottom: 0cm; padding-left: 5.4pt; width: 116.35pt; background-color: transparent; ">
            <p style="font-size: 13px; line-height: 1.4; margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span style="line-height: 1.5; font-size: small; "><span style="line-height: 1.5; font-size: 13px; font-family: 'Times New Roman'; ">DPI(</span><span style="line-height: 1.5; font-size: 13px; font-family: 宋体; ">dot per inch)</span></span></p>
            </td>
            <td valign="top" width="280" style="font-size: 12px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; padding-top: 0cm; padding-right: 5.4pt; padding-bottom: 0cm; padding-left: 5.4pt; width: 210pt; background-color: transparent; ">
            <p style="font-size: 13px; line-height: 1.4; margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span style="line-height: 1.5; font-size: small; "><span style="line-height: 1.5; font-size: 13px; font-family: 宋体; ">每英寸像素数，如120</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Times New Roman'; ">dpi,16</span><span style="line-height: 1.5; font-size: 13px; font-family: 宋体; ">0</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Times New Roman'; ">dpi</span><span style="line-height: 1.5; font-size: 13px; font-family: 宋体; ">等,假设QVGA(320*240)分辨率的屏幕物理尺寸是（2英寸*1.5英寸），dpi=160</span></span></p>
            </td>
            <td valign="top" width="133" style="font-size: 12px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; padding-top: 0cm; padding-right: 5.4pt; padding-bottom: 0cm; padding-left: 5.4pt; width: 99.75pt; background-color: transparent; ">
            <p style="font-size: 13px; line-height: 1.4; margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span style="line-height: 1.5; font-size: 13px; font-family: 宋体; "><span style="line-height: 1.5; font-size: small; ">可以反映屏幕的清晰度，用于缩放UI的</span></span></p>
            </td>
        </tr>
        <tr style="height: 34.45pt; ">
            <td valign="top" width="155" style="font-size: 12px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; padding-top: 0cm; padding-right: 5.4pt; padding-bottom: 0cm; padding-left: 5.4pt; width: 116.35pt; height: 34.45pt; background-color: transparent; ">
            <p style="font-size: 13px; line-height: 1.4; margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span style="line-height: 1.5; font-size: small; "><span style="line-height: 1.5; font-size: 13px; font-family: 宋体; ">Density(</span><span style="line-height: 1.5; font-size: 13px; font-family: 宋体; ">密度)</span></span></p>
            </td>
            <td valign="top" width="280" style="font-size: 12px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; padding-top: 0cm; padding-right: 5.4pt; padding-bottom: 0cm; padding-left: 5.4pt; width: 210pt; height: 34.45pt; background-color: transparent; ">
            <p style="font-size: 13px; line-height: 1.4; margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span style="line-height: 1.5; font-size: 13px; font-family: 宋体; "><span style="line-height: 1.5; font-size: small; ">屏幕里像素值浓度，resolution/Screen size可以反映出手机密度</span></span></p>
            </td>
            <td valign="top" width="133" style="font-size: 12px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; padding-top: 0cm; padding-right: 5.4pt; padding-bottom: 0cm; padding-left: 5.4pt; width: 99.75pt; height: 34.45pt; background-color: transparent; ">
            <p style="font-size: 13px; line-height: 1.4; margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p>
            </td>
        </tr>
        <tr>
            <td valign="top" width="155" style="font-size: 12px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; padding-top: 0cm; padding-right: 5.4pt; padding-bottom: 0cm; padding-left: 5.4pt; width: 116.35pt; background-color: transparent; ">
            <p style="font-size: 13px; line-height: 1.4; margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span style="line-height: 1.5; font-size: small; font-family: 'Times New Roman'; ">Density-independent pixel (dip)</span></p>
            </td>
            <td valign="top" width="280" style="font-size: 12px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; padding-top: 0cm; padding-right: 5.4pt; padding-bottom: 0cm; padding-left: 5.4pt; width: 210pt; background-color: transparent; ">
            <p style="font-size: 13px; line-height: 1.4; margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span style="line-height: 1.5; font-size: 13px; font-family: 宋体; "><span style="line-height: 1.5; font-size: small; ">指的是逻辑密度计算单位,dip和具体像素值的对应公式是dip/pixel=dpi值/160</span></span></p>
            </td>
            <td valign="top" width="133" style="font-size: 12px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; padding-top: 0cm; padding-right: 5.4pt; padding-bottom: 0cm; padding-left: 5.4pt; width: 99.75pt; background-color: transparent; ">
            <p style="font-size: 13px; line-height: 1.4; margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p>
            </td>
        </tr>
    </tbody>
</table>
</div>
<span  style="font-family: 'Comic Sans MS', 宋体, Arial, sans-serif; font-size: 13px; line-height: 19px; ">&nbsp;</span><span  style="font-family: 'Comic Sans MS', 宋体, Arial, sans-serif; font-size: 13px; line-height: 19px; "><br />
</span>
<div style="font-family: 'Comic Sans MS', 宋体, Arial, sans-serif; font-size: 13px; line-height: 19px; "><span style="line-height: 1.5; font-size: small; "><span style="line-height: 1.5; font-size: 13px; font-family: 宋体; ">手机屏幕分类和像素密度的对应关系如表1所示：</span></span>
<table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse; ">
    <tbody>
        <tr>
            <td valign="top" width="97" style="font-size: 12px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; padding-top: 0cm; padding-right: 5.4pt; padding-bottom: 0cm; padding-left: 5.4pt; width: 72.8pt; background-color: transparent; ">
            <p style="font-size: 13px; line-height: 1.4; margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p>
            </td>
            <td valign="top" width="151" style="font-size: 12px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; padding-top: 0cm; padding-right: 5.4pt; padding-bottom: 0cm; padding-left: 5.4pt; background-image: none; background-attachment: scroll; background-origin: initial; background-clip: initial; background-color: #f3f3f3; width: 113.6pt; background-position: 0% 0%; background-repeat: repeat repeat; ">
            <p style="font-size: 13px; line-height: 1.4; margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; background-image: none; background-attachment: scroll; background-origin: initial; background-clip: initial; background-color: #f3f3f3; background-position: 0% 0%; background-repeat: repeat repeat; "><span style="line-height: 1.5; font-size: 13px; background-image: none; background-attachment: scroll; background-origin: initial; background-clip: initial; background-color: #f3f3f3; background-position: 0% 0%; background-repeat: repeat repeat; "><span style="line-height: 1.5; font-size: small; "><span style="line-height: 1.5; font-size: 13px; font-family: 'Times New Roman'; ">Low density (120), ldpi</span></span></span></p>
            </td>
            <td valign="top" width="175" style="font-size: 12px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; padding-top: 0cm; padding-right: 5.4pt; padding-bottom: 0cm; padding-left: 5.4pt; background-image: none; background-attachment: scroll; background-origin: initial; background-clip: initial; background-color: #f3f3f3; width: 131.4pt; background-position: 0% 0%; background-repeat: repeat repeat; ">
            <p style="font-size: 13px; line-height: 1.4; margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; background-image: none; background-attachment: scroll; background-origin: initial; background-clip: initial; background-color: #f3f3f3; background-position: 0% 0%; background-repeat: repeat repeat; "><span style="line-height: 1.5; font-size: 13px; background-image: none; background-attachment: scroll; background-origin: initial; background-clip: initial; background-color: #f3f3f3; background-position: 0% 0%; background-repeat: repeat repeat; "><span style="line-height: 1.5; font-size: small; "><span style="line-height: 1.5; font-size: 13px; font-family: 'Times New Roman'; ">Medium density (160), mdpi</span></span></span></p>
            </td>
            <td valign="top" width="153" style="font-size: 12px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; padding-top: 0cm; padding-right: 5.4pt; padding-bottom: 0cm; padding-left: 5.4pt; background-image: none; background-attachment: scroll; background-origin: initial; background-clip: initial; background-color: #f3f3f3; width: 114.45pt; background-position: 0% 0%; background-repeat: repeat repeat; ">
            <p style="font-size: 13px; line-height: 1.4; margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; background-image: none; background-attachment: scroll; background-origin: initial; background-clip: initial; background-color: #f3f3f3; background-position: 0% 0%; background-repeat: repeat repeat; "><span style="line-height: 1.5; font-size: 13px; background-image: none; background-attachment: scroll; background-origin: initial; background-clip: initial; background-color: #f3f3f3; background-position: 0% 0%; background-repeat: repeat repeat; "><span style="line-height: 1.5; font-size: small; "><span style="line-height: 1.5; font-size: 13px; font-family: 'Times New Roman'; ">High density (240), hdpi</span></span></span></p>
            </td>
        </tr>
        <tr>
            <td valign="top" width="97" style="font-size: 12px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; padding-top: 0cm; padding-right: 5.4pt; padding-bottom: 0cm; padding-left: 5.4pt; background-image: none; background-attachment: scroll; background-origin: initial; background-clip: initial; background-color: #f3f3f3; width: 72.8pt; background-position: 0% 0%; background-repeat: repeat repeat; ">
            <p style="font-size: 13px; line-height: 1.4; margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; background-image: none; background-attachment: scroll; background-origin: initial; background-clip: initial; background-color: #f3f3f3; background-position: 0% 0%; background-repeat: repeat repeat; "><span style="line-height: 1.5; font-size: 13px; background-image: none; background-attachment: scroll; background-origin: initial; background-clip: initial; background-color: #f3f3f3; background-position: 0% 0%; background-repeat: repeat repeat; "><span style="line-height: 1.5; font-size: small; "><span style="line-height: 1.5; font-size: 13px; font-family: 'Times New Roman'; ">Small screen</span></span></span></p>
            </td>
            <td valign="top" width="151" style="font-size: 12px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; padding-top: 0cm; padding-right: 5.4pt; padding-bottom: 0cm; padding-left: 5.4pt; width: 113.6pt; background-color: transparent; ">
            <p style="font-size: 13px; line-height: 1.4; margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span style="line-height: 1.5; font-size: small; font-family: 'Times New Roman'; ">QVGA (240x320)</span></p>
            </td>
            <td valign="top" width="175" style="font-size: 12px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; padding-top: 0cm; padding-right: 5.4pt; padding-bottom: 0cm; padding-left: 5.4pt; width: 131.4pt; background-color: transparent; ">
            <p style="font-size: 13px; line-height: 1.4; margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p>
            </td>
            <td valign="top" width="153" style="font-size: 12px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; padding-top: 0cm; padding-right: 5.4pt; padding-bottom: 0cm; padding-left: 5.4pt; width: 114.45pt; background-color: transparent; ">
            <p style="font-size: 13px; line-height: 1.4; margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p>
            </td>
        </tr>
        <tr>
            <td valign="top" width="97" style="font-size: 12px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; padding-top: 0cm; padding-right: 5.4pt; padding-bottom: 0cm; padding-left: 5.4pt; background-image: none; background-attachment: scroll; background-origin: initial; background-clip: initial; background-color: #f3f3f3; width: 72.8pt; background-position: 0% 0%; background-repeat: repeat repeat; ">
            <p style="font-size: 13px; line-height: 1.4; margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; background-image: none; background-attachment: scroll; background-origin: initial; background-clip: initial; background-color: #f3f3f3; background-position: 0% 0%; background-repeat: repeat repeat; "><span style="line-height: 1.5; font-size: 13px; background-image: none; background-attachment: scroll; background-origin: initial; background-clip: initial; background-color: #f3f3f3; background-position: 0% 0%; background-repeat: repeat repeat; "><span style="line-height: 1.5; font-size: small; "><span style="line-height: 1.5; font-size: 13px; font-family: 'Times New Roman'; ">Normal screen</span></span></span></p>
            </td>
            <td valign="top" width="151" style="font-size: 12px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; padding-top: 0cm; padding-right: 5.4pt; padding-bottom: 0cm; padding-left: 5.4pt; width: 113.6pt; background-color: transparent; ">
            <p style="font-size: 13px; line-height: 1.4; margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span style="line-height: 1.5; font-size: small; font-family: 'Times New Roman'; ">WQVGA400 (240x400)WQVGA432 (240x432)</span></p>
            </td>
            <td valign="top" width="175" style="font-size: 12px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; padding-top: 0cm; padding-right: 5.4pt; padding-bottom: 0cm; padding-left: 5.4pt; width: 131.4pt; background-color: transparent; ">
            <p style="font-size: 13px; line-height: 1.4; margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span style="line-height: 1.5; font-size: small; font-family: 'Times New Roman'; ">HVGA (320x480)</span></p>
            </td>
            <td valign="top" width="153" style="font-size: 12px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; padding-top: 0cm; padding-right: 5.4pt; padding-bottom: 0cm; padding-left: 5.4pt; width: 114.45pt; background-color: transparent; ">
            <p style="font-size: 13px; line-height: 1.4; margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span style="line-height: 1.5; font-size: small; font-family: 'Times New Roman'; ">WVGA800 (480x800)WVGA854 (480x854)</span></p>
            </td>
        </tr>
        <tr style="height: 29.2pt; ">
            <td valign="top" width="97" style="font-size: 12px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; padding-top: 0cm; padding-right: 5.4pt; padding-bottom: 0cm; padding-left: 5.4pt; background-image: none; background-attachment: scroll; background-origin: initial; background-clip: initial; background-color: #f3f3f3; width: 72.8pt; height: 29.2pt; background-position: 0% 0%; background-repeat: repeat repeat; ">
            <p style="font-size: 13px; line-height: 1.4; margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; background-image: none; background-attachment: scroll; background-origin: initial; background-clip: initial; background-color: #f3f3f3; background-position: 0% 0%; background-repeat: repeat repeat; "><span style="line-height: 1.5; font-size: 13px; background-image: none; background-attachment: scroll; background-origin: initial; background-clip: initial; background-color: #f3f3f3; background-position: 0% 0%; background-repeat: repeat repeat; "><span style="line-height: 1.5; font-size: small; "><span style="line-height: 1.5; font-size: 13px; font-family: 'Times New Roman'; ">Large screen</span></span></span></p>
            </td>
            <td valign="top" width="151" style="font-size: 12px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; padding-top: 0cm; padding-right: 5.4pt; padding-bottom: 0cm; padding-left: 5.4pt; width: 113.6pt; height: 29.2pt; background-color: transparent; ">
            <p style="font-size: 13px; line-height: 1.4; margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p>
            </td>
            <td valign="top" width="175" style="font-size: 12px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; padding-top: 0cm; padding-right: 5.4pt; padding-bottom: 0cm; padding-left: 5.4pt; width: 131.4pt; height: 29.2pt; background-color: transparent; ">
            <p style="font-size: 13px; line-height: 1.4; margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span style="line-height: 1.5; font-size: small; font-family: 'Times New Roman'; ">WVGA800* (480x800)WVGA854* (480x854)</span></p>
            </td>
            <td valign="top" width="153" style="font-size: 12px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; padding-top: 0cm; padding-right: 5.4pt; padding-bottom: 0cm; padding-left: 5.4pt; width: 114.45pt; height: 29.2pt; background-color: transparent; ">
            <p style="font-size: 13px; line-height: 1.4; margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p>
            </td>
        </tr>
    </tbody>
</table>
</div>
<span  style="font-family: 'Comic Sans MS', 宋体, Arial, sans-serif; font-size: 13px; line-height: 19px; "><br />
</span>
<p style="font-size: 13px; line-height: 1.4; margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: 'Comic Sans MS', 宋体, Arial, sans-serif; ">&nbsp;</p>
<p style="font-size: 13px; line-height: 1.4; margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: 'Comic Sans MS', 宋体, Arial, sans-serif; ">&nbsp;</p>
<span  style="font-family: 'Comic Sans MS', 宋体, Arial, sans-serif; font-size: 13px; line-height: 19px; ">单位：像素</span><span  style="font-family: 'Comic Sans MS', 宋体, Arial, sans-serif; font-size: 13px; line-height: 19px; ">&nbsp;</span><span  style="font-family: 'Comic Sans MS', 宋体, Arial, sans-serif; font-size: 13px; line-height: 19px; "><br />
</span>
<p style="font-size: 13px; line-height: 1.4; margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: 'Comic Sans MS', 宋体, Arial, sans-serif; ">&nbsp;</p>
<p style="font-size: 13px; line-height: 1.4; margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: 'Comic Sans MS', 宋体, Arial, sans-serif; ">WVGA854: 854*480</p>
<p style="font-size: 13px; line-height: 1.4; margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: 'Comic Sans MS', 宋体, Arial, sans-serif; ">&nbsp;</p>
<div style="font-family: 'Comic Sans MS', 宋体, Arial, sans-serif; font-size: 13px; line-height: 19px; ">WVGA800: 800*480</div>
<span  style="font-family: 'Comic Sans MS', 宋体, Arial, sans-serif; font-size: 13px; line-height: 19px; ">HVGA: 640*480</span><span  style="font-family: 'Comic Sans MS', 宋体, Arial, sans-serif; font-size: 13px; line-height: 19px; ">&nbsp;</span><span  style="font-family: 'Comic Sans MS', 宋体, Arial, sans-serif; font-size: 13px; line-height: 19px; "><br />
</span>
<p style="font-size: 13px; line-height: 1.4; margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: 'Comic Sans MS', 宋体, Arial, sans-serif; ">&nbsp;</p>
<p style="font-size: 13px; line-height: 1.4; margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: 'Comic Sans MS', 宋体, Arial, sans-serif; ">QVGA: 320*240<br />
</p>
<p style="font-size: 13px; line-height: 1.4; margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: 'Comic Sans MS', 宋体, Arial, sans-serif; ">&nbsp;</p>
<p style="font-size: 13px; line-height: 1.4; margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: 'Comic Sans MS', 宋体, Arial, sans-serif; ">&nbsp;</p>
<div style="font-family: 'Comic Sans MS', 宋体, Arial, sans-serif; font-size: 13px; line-height: 19px; ">WQVGA432：432*240</div>
<p style="font-size: 13px; line-height: 1.4; margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: 'Comic Sans MS', 宋体, Arial, sans-serif; ">&nbsp;</p>
<p style="font-size: 13px; line-height: 1.4; margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: 'Comic Sans MS', 宋体, Arial, sans-serif; ">WQVGA400：400*240&nbsp;<br />
</p>
<p style="font-size: 13px; line-height: 1.4; margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: 'Comic Sans MS', 宋体, Arial, sans-serif; ">&nbsp;</p>
<p style="font-size: 13px; line-height: 1.4; margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: 'Comic Sans MS', 宋体, Arial, sans-serif; "></p>
<div style="font-family: 'Comic Sans MS', 宋体, Arial, sans-serif; font-size: 13px; line-height: 19px; ">Android3.0 WXGA：800*1280</div>
<img src ="http://www.blogjava.net/AstroQi/aggbug/348698.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/AstroQi/" target="_blank">Astro.Qi</a> 2011-04-21 13:35 <a href="http://www.blogjava.net/AstroQi/archive/2011/04/21/348698.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>基于Carbide.C++ 的Symbian开发环境搭建</title><link>http://www.blogjava.net/AstroQi/archive/2010/12/06/339925.html</link><dc:creator>Astro.Qi</dc:creator><author>Astro.Qi</author><pubDate>Mon, 06 Dec 2010 11:37:00 GMT</pubDate><guid>http://www.blogjava.net/AstroQi/archive/2010/12/06/339925.html</guid><wfw:comment>http://www.blogjava.net/AstroQi/comments/339925.html</wfw:comment><comments>http://www.blogjava.net/AstroQi/archive/2010/12/06/339925.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/AstroQi/comments/commentRss/339925.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/AstroQi/services/trackbacks/339925.html</trackback:ping><description><![CDATA[<div class="postTitle">
<h1><a id="ctl04_TitleUrl" class="postTitle2" href="http://www.cnblogs.com/jolboy/archive/2010/03/04/1678026.html">基于Carbide.C++ 的Symbian开发环境搭建</a></h1>
</div>
<p>1.开发环境工具选用Jdk1.5 + ActivePerl-5.6 + S60_5th_Edition_SDK_v1_0+ Carbide.C++.V2.3</p>
<p>JDK1.5 下载的地方很多，就不提供了。</p>
<p>ActivePerl-5.6 下载地址：ftp://ftp.activestate.com/ActivePerl/Windows/5.6/ActivePerl-5.6.1.635-MSWin32-x86.msi</p>
<p>S60_5th_Edition_SDK_v1_0下载地址：<a href="http://sw.nokia.com/id/577ad48d-290c-4bb5-8bdf-779ea8a5bc6c/S60_5th_Edition_SDK_v1_0_en.zip" target="_blank">http://sw.nokia.com/id/577ad48d-290c-4bb5-8bdf-779ea8a5bc6c/S60_5th_Edition_SDK_v1_0_en.zip</a></p>
<p>Carbide.C++.V2.3下载地址：<a href="http://www.forum.nokia.com/info/sw.nokia.com/id/dbb8841d-832c-43a6-be13-f78119a2b4cb.html" target="_blank">http://www.forum.nokia.com/info/sw.nokia.com/id/dbb8841d-832c-43a6-be13-f78119a2b4cb.html</a></p>
<p>&nbsp;</p>
<p>安装步骤如下：</p>
<p>1、在C盘中建立一个文件夹，如 SymbianApp</p>
<p>2、安装jdk到该目录下</p>
<p>3、安装ActivePerl-5.6到该目录下，安装这个Perl的时候，要注意，如果曾经在系统中装过oracle10G，那么会出现冲
突，oracle10G中自带了一个ActivePerl 5.8版本的，在环境变量中
Path中可以找到相应的路径，需要把5.8版本的路径从环境变量中删除，否则会造成运行出错，无法编译。</p>
<p>4、安装S60_5th_Edition_SDK，这里要注意了，安装到SymbianApp这个目录下的时候，&nbsp;该SDK默认的安装地址是
C:\Program
Files\S60\devices\S60_5th_Edition_SDK_v1.0，要改为c:\SymbianApp
\S60_5th_Edition_SDK_v1.0,这么改的目的，尽量让这个sdk模拟器的路径越短越好，一长串的后果，就是模拟器一打开就直接关
闭。</p>
<p>5、安装Carbide.C++.V2.3</p>
<p>按照上述步骤做完，有可能会出现，Carbide.C++中没有配置默认的sdk包，就需要在软件中手动设置。</p>
<img src ="http://www.blogjava.net/AstroQi/aggbug/339925.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/AstroQi/" target="_blank">Astro.Qi</a> 2010-12-06 19:37 <a href="http://www.blogjava.net/AstroQi/archive/2010/12/06/339925.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>字符编码详解及由来(UNICODE,UTF-8,GBK)</title><link>http://www.blogjava.net/AstroQi/archive/2010/11/18/338412.html</link><dc:creator>Astro.Qi</dc:creator><author>Astro.Qi</author><pubDate>Thu, 18 Nov 2010 09:51:00 GMT</pubDate><guid>http://www.blogjava.net/AstroQi/archive/2010/11/18/338412.html</guid><wfw:comment>http://www.blogjava.net/AstroQi/comments/338412.html</wfw:comment><comments>http://www.blogjava.net/AstroQi/archive/2010/11/18/338412.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/AstroQi/comments/commentRss/338412.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/AstroQi/services/trackbacks/338412.html</trackback:ping><description><![CDATA[<p style="margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; ">一直对字符的各种编码方式懵懵懂懂，什么ANSI、UNICODE、UTF-8、GB2312、GBK、DBCS、UCS&#8230;&#8230;是不是看的很晕，假如您细细的阅读本文你一定可以清晰的理解他们。<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp; 很久很久以前，有一群人，他们决定用8个可以开合的晶体管来组合成不同的状态，以表示世界上的万物。他们看到8个开关状态是好的，于是他们把这称为"字节"。</p>
<p style="margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; ">&nbsp;&nbsp;&nbsp; 再后来，他们又做了一些可以处理这些字节的机器，机器开动了，可以用字节来组合出很多状态，状态开始变来变去。他们看到这样是好的，于是它们就这机器称为"计算机"。<br />
&nbsp;&nbsp;&nbsp; 开始计算机只在美国用。八位的字节一共可以组合出256(2的8次方)种不同的状态。&nbsp;<br />
&nbsp;&nbsp;&nbsp; 他们把其中的编号从0开始的32种状态分别规定了特殊的用途，一但终端、打印机遇上约定好的这些字节被传过来时，就要做一些约定的动作。遇上00x10, 终端就换行，遇上0x07, 终端就向人们嘟嘟叫，例如遇上0x1b, 打印机就打印反白的字，或者终端就用彩色显示字母。他们看到这样很好，于是就把这些0x20以下的字节状态称为"控制码"。&nbsp;<br />
&nbsp;&nbsp;&nbsp; 他们又把所有的空格、标点符号、数字、大小写字母分别用连续的字节状态表示，一直编到了第127号，这样计算机就可以用不同字节来存储英语的文字了。大家 看到这样，都感觉很好，于是大家都把这个方案叫做 ANSI 的"Ascii"编码（American Standard Code for Information Interchange，美国信息互换标准代码）。当时世界上所有的计算机都用同样的ASCII方案来保存英文文字。&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp; 后来，就像建造巴比伦塔一样，世界各地的都开始使用计算机，但是很多国家用的不是英文，他们的字母里有许多是ASCII里没有的，为了可以在计算机保存他 们的文字，他们决定采用127号之后的空位来表示这些新的字母、符号，还加入了很多画表格时需要用下到的横线、竖线、交叉等形状，一直把序号编到了最后一 个状态255。从128到255这一页的字符集被称"扩展字符集"。从此之后，贪婪的人类再没有新的状态可以用了，美帝国主义可能没有想到还有第三世界国 家的人们也希望可以用到计算机吧！</p>
<p style="margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; ">&nbsp;&nbsp;&nbsp; 等中国人们得到计算机时，已经没有可以利用的字节状态来表示汉字，况且有6000多个常用汉字需要保存呢。但是这难不倒智慧的中国人民，我们不客气地把那些127号之后的奇异符号们直接取消掉,&nbsp;<br />
&nbsp;&nbsp;&nbsp; 规定：一个小于127的字符的意义与原来相同，但两个大于127的字符连在一起时，就表示一个汉字，前面的一个字节（他称之为高字节）从0xA1用到 0xF7，后面一个字节（低字节）从0xA1到0xFE，这样我们就可以组合出大约7000多个简体汉字了。在这些编码里，我们还把数学符号、罗马希腊的 字母、日文的假名们都编进去了，连在 ASCII 里本来就有的数字、标点、字母都统统重新编了两个字节长的编码，这就是常说的"全角"字符，而原来在127号以下的那些就叫"半角"字符了。&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 中国人民看到这样很不错，于是就把这种汉字方案叫做 "GB2312"。GB2312 是对 ASCII 的中文扩展。&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 但是中国的汉字太多了，我们很快就就发现有许多人的人名没有办法在这里打出来，特别是某些很会麻烦别人的国家领导人。于是我们不得不继续把 GB2312 没有用到的码位找出来老实不客气地用上。&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 后来还是不够用，于是干脆不再要求低字节一定是127号之后的内码，只要第一个字节是大于127就固定表示这是一个汉字的开始，不管后面跟的是不是扩展字 符集里的内容。结果扩展之后的编码方案被称为 GBK 标准，GBK 包括了 GB2312 的所有内容，同时又增加了近20000个新的汉字（包括繁体字）和符号。&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 后来少数民族也要用电脑了，于是我们再扩展，又加了几千个新的少数民族的字，GBK 扩成了GB18030。从此之后，中华民族的文化就可以在计算机时代中传承了。&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 中国的程序员们看到这一系列汉字编码的标准是好的，于是通称他们叫做 "DBCS"（Double Byte Charecter Set 双字节字符集）。在DBCS系列标准里，最大的特点是两字节长的汉字字符和一字节长的英文字符并存于同一套编码方案里，因此他们写的程序为了支持中文处 理，必须要注意字串里的每一个字节的值，如果这个值是大于127的，那么就认为一个双字节字符集里的字符出现了。那时候凡是受过加持，会编程的计算机僧侣 们都要每天念下面这个咒语数百遍：</p>
<p style="margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "一个汉字算两个英文字符！一个汉字算两个英文字符&#8230;&#8230;"<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 因为当时各个国家都像中国这样搞出一套自己的编码标准，结果互相之间谁也不懂谁的编码，谁也不支持别人的编码，连大陆和台湾这样只相隔了150海里，使用 着同一种语言的兄弟地区，也分别采用了不同的 DBCS 编码方案——当时的中国人想让电脑显示汉字，就必须装上一个"汉字系统"，专门用来处理汉字的显示、输入的问题，但是那个台湾的愚昧封建人士写的算命程序 就必须加装另一套支持 BIG5&nbsp; 编码的什么"倚天汉字系统"才可以用，装错了字符系统，显示就会乱了套！这怎么办？而且世界民族之林中还有那些一时用不上电脑的穷苦人民，他们的文字又怎 么办？</p>
<p style="margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 真是计算机的巴比伦塔命题啊！&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 正在这时，大天使加百列及时出现了——一个叫 ISO（国际标谁化组织）的国际组织决定着手解决这个问题。他们采用的方法很简单：废了所有的地区性编码方案，重新搞一个包括了地球上所有文化、所有字母 和符号的编码！他们打算叫它"Universal Multiple-Octet Coded Character Set"，简称 UCS, 俗称 "UNICODE"。&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; UNICODE 开始制订时，计算机的存储器容量极大地发展了，空间再也不成为问题了。于是 ISO 就直接规定必须用两个字节，也就是16位来统一表示所有的字符，对于ascii里的那些&#8220;半角&#8221;字符，UNICODE 包持其原编码不变，只是将其长度由原来的8位扩展为16位，而其他文化和语言的字符则全部重新统一编码。由于"半角"英文符号只需要用到低8位，所以其高 8位永远是0，因此这种大气的方案在保存英文文本时会多浪费一倍的空间。</p>
<p style="margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 这时候，从旧社会里走过来的程序员开始发现一个奇怪的现象：他们的strlen函数靠不住了，一个汉字不再是相当于两个字符了，而是一个！是的，从 UNICODE 开始，无论是半角的英文字母，还是全角的汉字，它们都是统一的"一个字符"！同时，也都是统一的"两个字节"，请注意"字符"和"字节"两个术语的不 同，&#8220;字节&#8221;是一个8位的物理存贮单元，而&#8220;字符&#8221;则是一个文化相关的符号。在UNICODE 中，一个字符就是两个字节。一个汉字算两个英文字符的时代已经快过去了。&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 从前多种字符集存在时，那些做多语言软件的公司遇上过很大麻烦，他们为了在不同的国家销售同一套软件，就不得不在区域化软件时也加持那个双字节字符集咒 语，不仅要处处小心不要搞错，还要把软件中的文字在不同的字符集中转来转去。UNICODE 对于他们来说是一个很好的一揽子解决方案，于是从 Windows NT 开始，MS 趁机把它们的操作系统改了一遍，把所有的核心代码都改成了用 UNICODE 方式工作的版本，从这时开始，WINDOWS 系统终于无需要加装各种本土语言系统，就可以显示全世界上所有文化的字符了。&nbsp;<br />
&nbsp;&nbsp;&nbsp; 但是，UNICODE 在制订时没有考虑与任何一种现有的编码方案保持兼容，这使得 GBK 与UNICODE 在汉字的内码编排上完全是不一样的，没有一种简单的算术方法可以把文本内容从UNICODE编码和另一种编码进行转换，这种转换必须通过查表来进行。&nbsp;<br />
&nbsp;&nbsp;&nbsp; 如前所述，UNICODE 是用两个字节来表示为一个字符，他总共可以组合出65535不同的字符，这大概已经可以覆盖世界上所有文化的符号。如果还不够也没有关系，ISO已经准备 了UCS-4方案，说简单了就是四个字节来表示一个字符，这样我们就可以组合出21亿个不同的字符出来（最高位有其他用途），这大概可以用到银河联邦成立 那一天吧！<br />
&nbsp;&nbsp;&nbsp; UNICODE 来到时，一起到来的还有计算机网络的兴起，UNICODE 如何在网络上传输也是一个必须考虑的问题，于是面向传输的众多 UTF（UCS Transfer Format）标准出现了，顾名思义，UTF8就是每次8个位传输数据，而UTF16就是每次16个位，只不过为了传输时的可靠性，从UNICODE到 UTF时并不是直接的对应，而是要过一些算法和规则来转换。</p>
<p style="margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; ">&nbsp;&nbsp;&nbsp; 受到过网络编程加持的计算机僧侣们都知道，在网络里传递信息时有一个很重要的问题，就是对于数据高低位的解读方式，一些计算机是采用低位先发送的方法，例 如我们PC机采用的 INTEL 架构，而另一些是采用高位先发送的方式，在网络中交换数据时，为了核对双方对于高低位的认识是否是一致的，采用了一种很简便的方法，就是在文本流的开始时 向对方发送一个标志符——如果之后的文本是高位在位，那就发送"FEFF"，反之，则发送"FFFE"。不信你可以用二进制方式打开一个UTF-X格式的 文件，看看开头两个字节是不是这两个字节？<br />
&nbsp;&nbsp;&nbsp; 讲到这里，我们再顺便说说一个很著名的奇怪现象：当你在 windows 的记事本里新建一个文件，输入"联通"两个字之后，保存，关闭，然后再次打开，你会发现这两个字已经消失了，代之的是几个乱码！呵呵，有人说这就是联通之所以拼不过移动的原因。</p>
<p style="margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; ">&nbsp;&nbsp;&nbsp; 其实这是因为GB2312编码与UTF8编码产生了编码冲撞的原因。&nbsp;<br />
&nbsp;&nbsp;&nbsp; 从网上引来一段从UNICODE到UTF8的转换规则：<br />
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Unicode &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;UTF-8<br />
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; (0-127) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0000 - 007F &nbsp; &nbsp;&nbsp;0xxxxxxx</p>
<p style="margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; ">
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<span style="font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19px; ">(128-2047) &nbsp; &nbsp;&nbsp;</span>0080 - 07FF &nbsp; &nbsp;&nbsp;110xxxxx 10xxxxxx&nbsp;</p>
<p style="margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; (2048-65535)<span style="font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19px; ">&nbsp;&nbsp;</span>0800 - FFFF &nbsp; &nbsp;&nbsp;1110xxxx 10xxxxxx 10xxxxxx</p>
<p style="margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; ">
&nbsp;&nbsp;&nbsp; 例如"汉"字的Unicode编码是6C49。6C49在0800-FFFF之间，所以要用3字节模板：1110xxxx 10xxxxxx 10xxxxxx。将6C49写成二进制是：0110 1100 0100 1001，将这个比特流按三字节模板的分段方法分为0110 110001 001001，依次代替模板中的x，得到：1110-0110 10-110001 10-001001，即E6 B1 89，这就是其UTF8的编码。&nbsp;<br />
&nbsp;&nbsp;&nbsp; 而当你新建一个文本文件时，记事本的编码默认是ANSI,如果你在ANSI的编码输入汉字，那么他实际就是GB系列的编码方式，在这种编码下，"联通"的内码是：&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; c1 1100 0001&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; aa 1010 1010&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cd 1100 1101&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; a8 1010 1000&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp; 注意到了吗？第一二个字节、第三四个字节的起始部分的都是"110"和"10"，正好与UTF8规则里的两字节模板是一致的，于是再次打开记事本时，记事 本就误认为这是一个UTF8编码的文件，让我们把第一个字节的110和第二个字节的10去掉，我们就得到了"00001 101010"，再把各位对齐，补上前导的0，就得到了"0000 0000 0110 1010"，不好意思，这是UNICODE的006A，也就是小写的字母"j"，而之后的两字节用UTF8解码之后是0368，这个字符什么也不是。这就 是只有"联通"两个字的文件没有办法在记事本里正常显示的原因。</p>
<p style="margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; ">&nbsp;&nbsp;&nbsp; 而如果你在"联通"之后多输入几个字，其他的字的编码不见得又恰好是110和10开始的字节，这样再次打开时，记事本就不会坚持这是一个utf8编码的文件，而会用ANSI的方式解读之，这时乱码又不出现了。<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp; 好了，终于可以回答NICO的问题了，在数据库里，有n前缀的字串类型就是UNICODE类型，这种类型中，固定用两个字节来表示一个字符，无论这个字符是汉字还是英文字母，或是别的什么。</p>
<p style="margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; ">&nbsp;&nbsp;&nbsp; 如果你要测试"abc汉字"这个串的长度，在没有n前缀的数据类型里，这个字串是7个字符的长度，因为一个汉字相当于两个字符。而在有n前缀的数据类型里，同样的测试串长度的函数将会告诉你是5个字符，因为一个汉字就是一个字符。</p>
<img src ="http://www.blogjava.net/AstroQi/aggbug/338412.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/AstroQi/" target="_blank">Astro.Qi</a> 2010-11-18 17:51 <a href="http://www.blogjava.net/AstroQi/archive/2010/11/18/338412.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>SLF4J + logBack</title><link>http://www.blogjava.net/AstroQi/archive/2010/11/17/338255.html</link><dc:creator>Astro.Qi</dc:creator><author>Astro.Qi</author><pubDate>Wed, 17 Nov 2010 06:33:00 GMT</pubDate><guid>http://www.blogjava.net/AstroQi/archive/2010/11/17/338255.html</guid><wfw:comment>http://www.blogjava.net/AstroQi/comments/338255.html</wfw:comment><comments>http://www.blogjava.net/AstroQi/archive/2010/11/17/338255.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/AstroQi/comments/commentRss/338255.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/AstroQi/services/trackbacks/338255.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 一.	Log4j+commons-logging&nbsp;	JAR包&nbsp;commons-logging-1.1.jar&nbsp;log4j-1.2.15.jar&nbsp;	配置文件&nbsp;commons-logging.properties&nbsp;log4j.xml&nbsp;	commons-logging.jar包读取commons-lo...&nbsp;&nbsp;<a href='http://www.blogjava.net/AstroQi/archive/2010/11/17/338255.html'>阅读全文</a><img src ="http://www.blogjava.net/AstroQi/aggbug/338255.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/AstroQi/" target="_blank">Astro.Qi</a> 2010-11-17 14:33 <a href="http://www.blogjava.net/AstroQi/archive/2010/11/17/338255.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>byte[] to int</title><link>http://www.blogjava.net/AstroQi/archive/2010/11/12/337898.html</link><dc:creator>Astro.Qi</dc:creator><author>Astro.Qi</author><pubDate>Fri, 12 Nov 2010 04:09:00 GMT</pubDate><guid>http://www.blogjava.net/AstroQi/archive/2010/11/12/337898.html</guid><wfw:comment>http://www.blogjava.net/AstroQi/comments/337898.html</wfw:comment><comments>http://www.blogjava.net/AstroQi/archive/2010/11/12/337898.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/AstroQi/comments/commentRss/337898.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/AstroQi/services/trackbacks/337898.html</trackback:ping><description><![CDATA[<div>
<div>public class NumberBytesUtils {</div>
<div><br />
</div>
<div><span class="Apple-tab-span" style="white-space:pre">	</span>public static void main(String[] args) {</div>
<div><span class="Apple-tab-span" style="white-space:pre">		</span>byte[] bytes = new byte[4];</div>
<div><span class="Apple-tab-span" style="white-space:pre">		</span>bytes[0] = (byte) 65;</div>
<div><span class="Apple-tab-span" style="white-space:pre">		</span>bytes[1] = (byte) 0;</div>
<div><span class="Apple-tab-span" style="white-space:pre">		</span>bytes[2] = (byte) 97;</div>
<div><span class="Apple-tab-span" style="white-space:pre">		</span>bytes[3] = (byte) 0;</div>
<div><span class="Apple-tab-span" style="white-space:pre">		</span></div>
<div><span class="Apple-tab-span" style="white-space:pre">		</span>System.out.println("String: " + new String(bytes));</div>
<div><span class="Apple-tab-span" style="white-space:pre">		</span></div>
<div><span class="Apple-tab-span" style="white-space:pre">		</span>int iv = bytesToInt(bytes);</div>
<div><span class="Apple-tab-span" style="white-space:pre">		</span>float fv = bytesToFloat(bytes);</div>
<div><span class="Apple-tab-span" style="white-space:pre">		</span>byte[] bs = intToBytes(iv);</div>
<div><span class="Apple-tab-span" style="white-space:pre">		</span>byte[] fs = floatToBytes(fv);</div>
<div><span class="Apple-tab-span" style="white-space:pre">		</span></div>
<div><span class="Apple-tab-span" style="white-space:pre">		</span>System.out.println("Int: " + iv);</div>
<div><span class="Apple-tab-span" style="white-space:pre">		</span>System.out.println("Float: " + fv);</div>
<div><span class="Apple-tab-span" style="white-space:pre">		</span>System.out.println("------------");</div>
<div><span class="Apple-tab-span" style="white-space:pre">		</span></div>
<div><span class="Apple-tab-span" style="white-space:pre">		</span>for (int i = 0; i &lt; bs.length; i++) {</div>
<div><span class="Apple-tab-span" style="white-space:pre">			</span>System.out.println(bs[i]);</div>
<div><span class="Apple-tab-span" style="white-space:pre">		</span>}</div>
<div><span class="Apple-tab-span" style="white-space:pre">		</span>System.out.println("============");</div>
<div><span class="Apple-tab-span" style="white-space:pre">		</span>for (int i = 0; i &lt; fs.length; i++) {</div>
<div><span class="Apple-tab-span" style="white-space:pre">			</span>System.out.println(fs[i]);</div>
<div><span class="Apple-tab-span" style="white-space:pre">		</span>}</div>
<div><span class="Apple-tab-span" style="white-space:pre">		</span>System.out.println("============");</div>
<div><span class="Apple-tab-span" style="white-space:pre">		</span>System.out.println(bytesToFloat(floatToBytes(-0.45367f)));</div>
<div><span class="Apple-tab-span" style="white-space:pre">		</span>System.out.println("************");</div>
<div>//<span class="Apple-tab-span" style="white-space:pre">		</span>System.out.println(0xff);</div>
<div>//<span class="Apple-tab-span" style="white-space:pre">		</span>System.out.println(0xff00);</div>
<div>//<span class="Apple-tab-span" style="white-space:pre">		</span>System.out.println(0xff0000);</div>
<div>//<span class="Apple-tab-span" style="white-space:pre">		</span>System.out.println(0xff000000);</div>
<div><span class="Apple-tab-span" style="white-space:pre">		</span></div>
<div><span class="Apple-tab-span" style="white-space:pre">		</span>byte[] bytesL = new byte[8];</div>
<div><span class="Apple-tab-span" style="white-space:pre">		</span>System.arraycopy(bytes, 0, bytesL, 0, bytes.length);</div>
<div><span class="Apple-tab-span" style="white-space:pre">		</span>bytesL[4] = (byte) 0;</div>
<div><span class="Apple-tab-span" style="white-space:pre">		</span>bytesL[5] = (byte) 0;</div>
<div><span class="Apple-tab-span" style="white-space:pre">		</span>bytesL[6] = (byte) 0;</div>
<div><span class="Apple-tab-span" style="white-space:pre">		</span>bytesL[7] = (byte) 0;</div>
<div><span class="Apple-tab-span" style="white-space:pre">		</span></div>
<div><span class="Apple-tab-span" style="white-space:pre">		</span>long lv = bytesToLong(bytesL);</div>
<div><span class="Apple-tab-span" style="white-space:pre">		</span>double dv = bytesToDouble(bytesL);</div>
<div><span class="Apple-tab-span" style="white-space:pre">		</span>byte[] bls = longToBytes(lv);</div>
<div><span class="Apple-tab-span" style="white-space:pre">		</span>byte[] dls = doubleToBytes(dv);</div>
<div><span class="Apple-tab-span" style="white-space:pre">		</span>System.out.println("Long: " + lv);</div>
<div><span class="Apple-tab-span" style="white-space:pre">		</span>System.out.println("Double: " + dv);</div>
<div><span class="Apple-tab-span" style="white-space:pre">		</span>System.out.println("----");</div>
<div><span class="Apple-tab-span" style="white-space:pre">		</span>for (int i = 0; i &lt; bls.length; i++) {</div>
<div><span class="Apple-tab-span" style="white-space:pre">			</span>System.out.println(bls[i]);</div>
<div><span class="Apple-tab-span" style="white-space:pre">		</span>}</div>
<div><span class="Apple-tab-span" style="white-space:pre">		</span>System.out.println("----");</div>
<div><span class="Apple-tab-span" style="white-space:pre">		</span>for (int i = 0; i &lt; dls.length; i++) {</div>
<div><span class="Apple-tab-span" style="white-space:pre">			</span>System.out.println(dls[i]);</div>
<div><span class="Apple-tab-span" style="white-space:pre">		</span>}</div>
<div><span class="Apple-tab-span" style="white-space:pre">		</span>System.out.println("****");</div>
<div><span class="Apple-tab-span" style="white-space:pre">		</span>System.out.println(bytesToDouble(doubleToBytes(2.345)));</div>
<div><span class="Apple-tab-span" style="white-space:pre">		</span></div>
<div><span class="Apple-tab-span" style="white-space:pre">	</span>}</div>
<div><span class="Apple-tab-span" style="white-space:pre">	</span></div>
<div><span class="Apple-tab-span" style="white-space:pre">	</span>/**</div>
<div><span class="Apple-tab-span" style="white-space:pre">	</span> * bytes[3] = value &gt;&gt; 24</div>
<div><span class="Apple-tab-span" style="white-space:pre">	</span> * bytes[2] = value &gt;&gt; 16</div>
<div><span class="Apple-tab-span" style="white-space:pre">	</span> * bytes[1] = value &gt;&gt; &nbsp;8</div>
<div><span class="Apple-tab-span" style="white-space:pre">	</span> * bytes[0] = value &gt;&gt; &nbsp;0</div>
<div><span class="Apple-tab-span" style="white-space:pre">	</span> * @param value</div>
<div><span class="Apple-tab-span" style="white-space:pre">	</span> * @return</div>
<div><span class="Apple-tab-span" style="white-space:pre">	</span> */</div>
<div><span class="Apple-tab-span" style="white-space:pre">	</span>public static byte[] intToBytes(int value){</div>
<div><span class="Apple-tab-span" style="white-space:pre">		</span>int length = 4;</div>
<div><span class="Apple-tab-span" style="white-space:pre">		</span>byte[] bytes = new byte[length];</div>
<div><span class="Apple-tab-span" style="white-space:pre">		</span>for (int i = length - 1; i &gt;= 0; i--) {</div>
<div><span class="Apple-tab-span" style="white-space:pre">			</span>int offset = i * 8; // 24, 16, 8</div>
<div><span class="Apple-tab-span" style="white-space:pre">			</span>bytes[i] = (byte) (value &gt;&gt; offset);</div>
<div><span class="Apple-tab-span" style="white-space:pre">		</span>}</div>
<div><span class="Apple-tab-span" style="white-space:pre">		</span>return bytes;</div>
<div><span class="Apple-tab-span" style="white-space:pre">	</span>}</div>
<div><span class="Apple-tab-span" style="white-space:pre">	</span></div>
<div><span class="Apple-tab-span" style="white-space:pre">	</span>/**</div>
<div><span class="Apple-tab-span" style="white-space:pre">	</span> * bytes[7] = value &gt;&gt; 56</div>
<div><span class="Apple-tab-span" style="white-space:pre">	</span> * bytes[6] = value &gt;&gt; 48</div>
<div><span class="Apple-tab-span" style="white-space:pre">	</span> * bytes[5] = value &gt;&gt; 40</div>
<div><span class="Apple-tab-span" style="white-space:pre">	</span> * bytes[4] = value &gt;&gt; 32</div>
<div><span class="Apple-tab-span" style="white-space:pre">	</span> * bytes[3] = value &gt;&gt; 24</div>
<div><span class="Apple-tab-span" style="white-space:pre">	</span> * bytes[2] = value &gt;&gt; 16</div>
<div><span class="Apple-tab-span" style="white-space:pre">	</span> * bytes[1] = value &gt;&gt; &nbsp;8</div>
<div><span class="Apple-tab-span" style="white-space:pre">	</span> * bytes[0] = value &gt;&gt; &nbsp;0</div>
<div><span class="Apple-tab-span" style="white-space:pre">	</span> * @param value</div>
<div><span class="Apple-tab-span" style="white-space:pre">	</span> * @return</div>
<div><span class="Apple-tab-span" style="white-space:pre">	</span> */</div>
<div><span class="Apple-tab-span" style="white-space:pre">	</span>public static byte[] longToBytes(long value){</div>
<div><span class="Apple-tab-span" style="white-space:pre">		</span>int length = 8;</div>
<div><span class="Apple-tab-span" style="white-space:pre">		</span>byte[] bytes = new byte[length];</div>
<div><span class="Apple-tab-span" style="white-space:pre">		</span>for (int i = length - 1; i &gt;= 0; i--) {</div>
<div><span class="Apple-tab-span" style="white-space:pre">			</span>int offset = i * 8; //56, 48, 40, 32, 24, 16, 8</div>
<div><span class="Apple-tab-span" style="white-space:pre">			</span>bytes[i] = (byte) (value &gt;&gt; offset);</div>
<div><span class="Apple-tab-span" style="white-space:pre">		</span>}</div>
<div><span class="Apple-tab-span" style="white-space:pre">		</span>return bytes;</div>
<div><span class="Apple-tab-span" style="white-space:pre">	</span>}</div>
<div><span class="Apple-tab-span" style="white-space:pre">	</span></div>
<div><br />
</div>
<div><span class="Apple-tab-span" style="white-space:pre">	</span>/**</div>
<div><span class="Apple-tab-span" style="white-space:pre">	</span> * 操作符 &lt;&lt; 的优先级比 &amp; 高</div>
<div><span class="Apple-tab-span" style="white-space:pre">	</span> * intValue = (bytes[3] &amp; 0xFF) &lt;&lt; 24</div>
<div><span class="Apple-tab-span" style="white-space:pre">		</span> &nbsp; &nbsp; &nbsp; &nbsp;| (bytes[2] &amp; 0xFF) &lt;&lt; 16</div>
<div><span class="Apple-tab-span" style="white-space:pre">		</span> &nbsp; &nbsp; &nbsp; &nbsp;| (bytes[1] &amp; 0xFF) &lt;&lt; &nbsp;8</div>
<div><span class="Apple-tab-span" style="white-space:pre">		</span> &nbsp; &nbsp; &nbsp; &nbsp;| (bytes[0] &amp; 0xFF) &lt;&lt; &nbsp;0</div>
<div><span class="Apple-tab-span" style="white-space:pre">	</span> * @param bytes</div>
<div><span class="Apple-tab-span" style="white-space:pre">	</span> * @return</div>
<div><span class="Apple-tab-span" style="white-space:pre">	</span> */</div>
<div><span class="Apple-tab-span" style="white-space:pre">	</span>public static int bytesToInt (byte[] bytes){</div>
<div><span class="Apple-tab-span" style="white-space:pre">		</span>int length = 4;</div>
<div><span class="Apple-tab-span" style="white-space:pre">		</span>int intValue = 0;</div>
<div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;for (int i = length - 1; i &gt;= 0; i--) {</div>
<div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;<span class="Apple-tab-span" style="white-space:pre">	</span>int offset = i * 8; //24, 16, 8</div>
<div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;<span class="Apple-tab-span" style="white-space:pre">	</span>intValue |= (bytes[i] &amp; 0xFF) &lt;&lt; offset;</div>
<div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;}</div>
<div>&nbsp;&nbsp; &nbsp; &nbsp; return intValue;</div>
<div><span class="Apple-tab-span" style="white-space:pre">	</span>}</div>
<div><span class="Apple-tab-span" style="white-space:pre">	</span></div>
<div><span class="Apple-tab-span" style="white-space:pre">	</span>/**</div>
<div><span class="Apple-tab-span" style="white-space:pre">	</span> * 操作符 &lt;&lt; 的优先级比 &amp; 高</div>
<div><span class="Apple-tab-span" style="white-space:pre">	</span> * longValue = (long)(bytes[7] &amp; 0xFF) &lt;&lt; 56</div>
<div><span class="Apple-tab-span" style="white-space:pre">		</span> &nbsp; &nbsp; &nbsp; &nbsp; | (long)(bytes[6] &amp; 0xFF) &lt;&lt; 48</div>
<div><span class="Apple-tab-span" style="white-space:pre">		</span> &nbsp; &nbsp; &nbsp; &nbsp; | (long)(bytes[5] &amp; 0xFF) &lt;&lt; 40</div>
<div><span class="Apple-tab-span" style="white-space:pre">		</span> &nbsp; &nbsp; &nbsp; &nbsp; | (long)(bytes[4] &amp; 0xFF) &lt;&lt; 32</div>
<div><span class="Apple-tab-span" style="white-space:pre">		</span> &nbsp; &nbsp; &nbsp; &nbsp; | (long)(bytes[3] &amp; 0xFF) &lt;&lt; 24</div>
<div><span class="Apple-tab-span" style="white-space:pre">		</span> &nbsp; &nbsp; &nbsp; &nbsp; | (long)(bytes[2] &amp; 0xFF) &lt;&lt; 16</div>
<div><span class="Apple-tab-span" style="white-space:pre">		</span> &nbsp; &nbsp; &nbsp; &nbsp; | (long)(bytes[1] &amp; 0xFF) &lt;&lt; &nbsp;8</div>
<div><span class="Apple-tab-span" style="white-space:pre">		</span> &nbsp; &nbsp; &nbsp; &nbsp; | (long)(bytes[0] &amp; 0xFF) &lt;&lt; &nbsp;0</div>
<div><span class="Apple-tab-span" style="white-space:pre">	</span> * @param bytes</div>
<div><span class="Apple-tab-span" style="white-space:pre">	</span> * @return</div>
<div><span class="Apple-tab-span" style="white-space:pre">	</span> */</div>
<div><span class="Apple-tab-span" style="white-space:pre">	</span>public static long bytesToLong (byte[] bytes){</div>
<div><span class="Apple-tab-span" style="white-space:pre">		</span>int length = 8;</div>
<div><span class="Apple-tab-span" style="white-space:pre">		</span>long longValue = 0;</div>
<div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;for (int i = length - 1; i &gt;= 0; i--) {</div>
<div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;<span class="Apple-tab-span" style="white-space:pre">	</span>int offset = i * 8; //56, 48, 40, 32, 24, 16, 8</div>
<div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;<span class="Apple-tab-span" style="white-space:pre">	</span>longValue |= (long)(bytes[i] &amp; 0xFF) &lt;&lt; offset; //一定要先强制转换成long型再移位, 因为0xFF为int型</div>
<div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;}</div>
<div>&nbsp;&nbsp; &nbsp; &nbsp; return longValue;</div>
<div><span class="Apple-tab-span" style="white-space:pre">	</span>}</div>
<div><span class="Apple-tab-span" style="white-space:pre">	</span></div>
<div><span class="Apple-tab-span" style="white-space:pre">	</span>public static float bytesToFloat(byte[] bytes) {</div>
<div><span class="Apple-tab-span" style="white-space:pre">		</span>return Float.intBitsToFloat(bytesToInt(bytes));</div>
<div><span class="Apple-tab-span" style="white-space:pre">	</span>}</div>
<div><span class="Apple-tab-span" style="white-space:pre">	</span></div>
<div><span class="Apple-tab-span" style="white-space:pre">	</span>public static double bytesToDouble(byte[] bytes) {</div>
<div><span class="Apple-tab-span" style="white-space:pre">		</span>return Double.longBitsToDouble(bytesToLong(bytes));</div>
<div><span class="Apple-tab-span" style="white-space:pre">	</span>}</div>
<div><span class="Apple-tab-span" style="white-space:pre">	</span></div>
<div><span class="Apple-tab-span" style="white-space:pre">	</span>public static byte[] floatToBytes(float value){</div>
<div><span class="Apple-tab-span" style="white-space:pre">		</span>return intToBytes(Float.floatToIntBits(value));</div>
<div><span class="Apple-tab-span" style="white-space:pre">	</span>}</div>
<div><br />
</div>
<div><span class="Apple-tab-span" style="white-space:pre">	</span>public static byte[] doubleToBytes(double value){</div>
<div><span class="Apple-tab-span" style="white-space:pre">		</span>return longToBytes(Double.doubleToLongBits(value));</div>
<div><span class="Apple-tab-span" style="white-space:pre">	</span>}</div>
<div>}</div>
<div>====================================================================================</div>
</div>
<div>/**</div>
<div>
<div>public static String bytes2HexString(byte[] b) {&nbsp;</div>
<div><span class="Apple-tab-span" style="white-space:pre">	</span>String ret = "";&nbsp;</div>
<div><span class="Apple-tab-span" style="white-space:pre">	</span>for (int i = 0; i &lt; b.length; i++) {&nbsp;</div>
<div><span class="Apple-tab-span" style="white-space:pre">		</span>String hex = Integer.toHexString(b[ i ] 0xFF);&nbsp;</div>
<div><span class="Apple-tab-span" style="white-space:pre">		</span>if (hex.length() == 1) {</div>
<div>&nbsp;<span class="Apple-tab-span" style="white-space:pre">			</span>hex = '0' + hex;&nbsp;</div>
<div>&nbsp;<span class="Apple-tab-span" style="white-space:pre">		</span>}&nbsp;</div>
<div>&nbsp;<span class="Apple-tab-span" style="white-space:pre">		</span>ret += hex.toUpperCase();&nbsp;</div>
<div>&nbsp;<span class="Apple-tab-span" style="white-space:pre">	</span>}&nbsp;</div>
<div>&nbsp;<span class="Apple-tab-span" style="white-space:pre">	</span>return ret;&nbsp;</div>
<div>}&nbsp;</div>
<div>&nbsp;</div>
<div>&nbsp;上面是将byte[]转化十六进制的字符串,注意这里b[ i ] &amp; 0xFF将一个byte和 0xFF进行了与运算,</div>
<div>&nbsp;然后使用Integer.toHexString取得了十六进制字符串,可以看出 b[ i ] &amp; 0xFF运算后得出的仍然</div>
<div>&nbsp;是个int,那么为何要和 0xFF进行与运算呢?</div>
<div>&nbsp;直接 Integer.toHexString(b[ i ]);,将byte强转为int不行吗?答案是不行的.&nbsp;</div>
<div>&nbsp;其原因在于:&nbsp;</div>
<div>&nbsp;1.byte的大小为8bits而int的大小为32bits&nbsp;</div>
<div>&nbsp;2.java的二进制采用的是补码形式&nbsp;</div>
<div>&nbsp;</div>
<div>&nbsp;在这里先温习下计算机基础理论 byte是一个字节保存的，有8个位，即8个0、1。&nbsp;</div>
<div>&nbsp;8位的第一个位是符号位， 也就是说</div>
<div>&nbsp;0000 0001代表的是数字1&nbsp;</div>
<div>&nbsp;1000 0000代表的就是-1&nbsp;</div>
<div>&nbsp;所以</div>
<div>&nbsp;正数最大为0111 1111，也就是数字127&nbsp;</div>
<div>&nbsp;负数最大为1111 1111，也就是数字-128&nbsp;</div>
<div>&nbsp;上面说的是二进制原码，</div>
<div>&nbsp;但是在java中采用的是补码的形式，而不是原码，</div>
<div>&nbsp;</div>
<div>&nbsp;下面介绍下什么是补码&nbsp;</div>
<div>&nbsp;1、反码：一个数如果是正，则它的反码与原码相同； 一个数如果是负，则符号位为1，其余各位是对原码取反；&nbsp;</div>
<div>&nbsp;2、补码：利用溢出，我们可以将减法变成加法，</div>
<div>&nbsp;<span class="Apple-tab-span" style="white-space:pre">		</span>对于十进制数，从9得到5可用减法： 9－4＝5。</div>
<div>&nbsp;<span class="Apple-tab-span" style="white-space:pre">			</span>因为4+6＝10，我们可以将6作为4的补数，改写为加法： 9+6＝15（去掉高位1，也就是减10）得到5.&nbsp;</div>
<div>&nbsp;<span class="Apple-tab-span" style="white-space:pre">		</span>对于十六进制数，从c到5可用减法： c－7＝5。</div>
<div>&nbsp;<span class="Apple-tab-span" style="white-space:pre">			</span>因为7+9＝16 将9作为7的补数，改写为加法： c+9＝15（去掉高位1，也就是减16）得到5.&nbsp;</div>
<div>&nbsp;<span class="Apple-tab-span" style="white-space:pre">			</span></div>
<div>&nbsp;在计算机中，如果我们用1个字节表示一个数，一个字节有8位，超过8位就进1，在内存中情况为（1 0000 0000），进位1被丢弃。</div>
<div>&nbsp;所以补码是这样运算的：</div>
<div>&nbsp;&nbsp;⑴一个数为正，则它的原码、反码、补码相同</div>
<div>&nbsp;&nbsp;⑵一个数为负，高符号位为1，其余各位是对原码取反，然后整个数加1，(符号位不变，其余为取反再加1)</div>
<div>&nbsp;&nbsp;-1的原码为 10000001&nbsp;</div>
<div>&nbsp;&nbsp;-1的反码为 11111110 再 + 1&nbsp;</div>
<div>&nbsp;&nbsp;-1的补码为 11111111&nbsp;</div>
<div>&nbsp;&nbsp;</div>
<div>&nbsp;&nbsp;0的原码为 &nbsp;00000000&nbsp;</div>
<div>&nbsp;&nbsp;0的反码为 &nbsp;11111111 (正零和负零的反码相同) 再 + 1</div>
<div>&nbsp;&nbsp;0的补码为 100000000（舍掉打头的1，正零和负零的补码相同）&nbsp;</div>
<div>&nbsp;&nbsp;</div>
<div>&nbsp;&nbsp;Integer.toHexString的参数是int，如果不进行&amp;0xff，那么当一个byte转换成int时，</div>
<div>&nbsp;&nbsp;由于int是32位，而byte只有8位，这时会进行补位，补位补的是1</div>
<div>&nbsp;&nbsp;例如：补码11111111的十进制数为-1，转换为int时变为11111111 11111111 11111111 11111111</div>
<div>&nbsp;&nbsp;好多1啊，呵呵！即 0xff ff ff ff&nbsp;，但是这个数是不对的，</div>
<div>&nbsp;&nbsp;这种补位就会造成误差。和0xff相与后，高24bits就会被清0了，结果就对了。(任是补码11111111)</div>
</div>
<div>*/</div>
<img src ="http://www.blogjava.net/AstroQi/aggbug/337898.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/AstroQi/" target="_blank">Astro.Qi</a> 2010-11-12 12:09 <a href="http://www.blogjava.net/AstroQi/archive/2010/11/12/337898.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Java 中 byte与char、String互转原理</title><link>http://www.blogjava.net/AstroQi/archive/2010/11/12/337897.html</link><dc:creator>Astro.Qi</dc:creator><author>Astro.Qi</author><pubDate>Fri, 12 Nov 2010 04:08:00 GMT</pubDate><guid>http://www.blogjava.net/AstroQi/archive/2010/11/12/337897.html</guid><wfw:comment>http://www.blogjava.net/AstroQi/comments/337897.html</wfw:comment><comments>http://www.blogjava.net/AstroQi/archive/2010/11/12/337897.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/AstroQi/comments/commentRss/337897.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/AstroQi/services/trackbacks/337897.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 一、字节和unicode&nbsp;Java内核是unicode的，就连class文件也是，但是很多媒体，包括文件/流的保存方式是使用字节流的。因此Java要对这些字节流经行转化。 char是unicode的，而byte是字节。Java中&nbsp;byte/char互转的函数在sun.io的包中间有。其中ByteToCharConverter类是中调度，可以用来告诉你，你用的 conve...&nbsp;&nbsp;<a href='http://www.blogjava.net/AstroQi/archive/2010/11/12/337897.html'>阅读全文</a><img src ="http://www.blogjava.net/AstroQi/aggbug/337897.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/AstroQi/" target="_blank">Astro.Qi</a> 2010-11-12 12:08 <a href="http://www.blogjava.net/AstroQi/archive/2010/11/12/337897.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Hibernate自定义表单完全解决方案(无需重置SessionFactory)</title><link>http://www.blogjava.net/AstroQi/archive/2009/10/26/299764.html</link><dc:creator>Astro.Qi</dc:creator><author>Astro.Qi</author><pubDate>Mon, 26 Oct 2009 05:16:00 GMT</pubDate><guid>http://www.blogjava.net/AstroQi/archive/2009/10/26/299764.html</guid><wfw:comment>http://www.blogjava.net/AstroQi/comments/299764.html</wfw:comment><comments>http://www.blogjava.net/AstroQi/archive/2009/10/26/299764.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.blogjava.net/AstroQi/comments/commentRss/299764.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/AstroQi/services/trackbacks/299764.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 最近开发的一个系统，需要在不更改代码和重启系统的情况下提供对用户自动建表的支持，由于系统应用了hibernate，所以在建表同时也要建立持久化对象以及对这些对象注册，人渣我首先想倒的是 baidu和google，哪知一番搜索下来，发现都不尽入人意，于是乎，造轮子之路开始了&nbsp;数据库我是采用的oracle9i,目前在比如数据库类型支持，还有对象关系支持上都很简单，不过在现有基础上进行扩展...&nbsp;&nbsp;<a href='http://www.blogjava.net/AstroQi/archive/2009/10/26/299764.html'>阅读全文</a><img src ="http://www.blogjava.net/AstroQi/aggbug/299764.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/AstroQi/" target="_blank">Astro.Qi</a> 2009-10-26 13:16 <a href="http://www.blogjava.net/AstroQi/archive/2009/10/26/299764.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>