posts - 4,  comments - 2,  trackbacks - 0
            2006年12月22日
          String s = new String("abc");創建了幾個String對象?
          ?
          引用變量與對象的區別;
          字符串文字"abc"是一個String對象;
          文字池(pool of literal strings)和堆(heap)中的字符串對象。

          一、引用變量與對象:除了一些早期的Java書籍和現在的垃圾書籍,人們都可以從中比較清楚地學習到兩者的區別。
          A aa;
          這個語句聲明一個類A的引用變量aa[我們常常稱之為句柄],而對象一般通過new創建。所以題目中s僅僅是一個引用變量,它不是對象。

          二、Java中所有的字符串文字[字符串常量]都是一個String的對象。有人[特別是C程序員]在一些場合喜歡把字符串"當作/看成"字符數組,這也沒有辦法,因為字符串與字符數組存在一些內在的聯系。事實上,它與字符數組是兩種完全不同的對象。

          System.out.println("Hello".length());
          char[] cc={'H','i'};
          System.out.println(cc.length);


          三、字符串對象的創建:
          由于字符串對象的大量使用(它是一個對象,一般而言對象總是在heap分配內存),Java中為了節省內存空間和運行時間(如比較字符串時,==比equals()快),在編譯階段就把所有的字符串文字放到一個文字池(pool of literal strings)中,而運行時文字池成為常量池的一部分。文字池的好處,就是該池中所有相同的字符串常量被合并,只占用一個空間。
          我們知道,對兩個引用變量,使用==判斷它們的值(引用)是否相等,即指向同一個對象:

          String s1 = "abc" ;
          String s2 = "abc" ;
          if( s1 == s2 ) System.out.println("s1,s2 refer to the same object");
          else System.out.println("trouble");



          這里的輸出顯示,兩個字符串文字保存為一個對象。就是說,上面的代碼只在pool中創建了一個String對象。

          現在看String s = new String("abc");語句,這里"abc"本身就是pool中的一個對象,而在運行時執行new String()時,
          將pool中的對象復制一份放到heap中,并且把heap中的這個對象的引用交給s持有。ok,這條語句就創建了2個String對象。


          String s1 = new String("abc") ;
          String s2 = new String("abc") ;
          if( s1 == s2 ){ //不會執行的語句}



          這時用==判斷就可知,雖然兩個對象的"內容"相同(equals()判斷),但兩個引用變量所持有的引用不同,

          BTW:上面的代碼創建了幾個String Object? (三個,pool中一個,heap中2個。)
          posted @ 2007-01-23 09:16 ziwolf 閱讀(3081) | 評論 (2)編輯 收藏
          說來真是奇怪,MSN上不了了。竟然是因為地震。我暈。
          電線為啥放在臺灣邊上呢。
          posted @ 2006-12-30 14:06 ziwolf 閱讀(187) | 評論 (0)編輯 收藏
          初始化web配置
          如果你使用的是Servlet2.4 及以上的web容器,那么僅需要在web.xml 中增加ContextListener即可

          <web-app>
          ...
          <listener>
          ?? ?<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
          </listener>
          ...
          </web-app>


          如果你使用的是早期版本web容器 Servlet 2.4 以前,那么需要使用一個javax.servlet.Filter 的實現

          <web-app>
          ..
          <filter>
          ?? ?<filter-name>requestContextFilter</filter-name>
          ?? ?<filter-class>org.springframework.web.filter.RequestContextFilter</filter-class>
          </filter>
          <filter-mapping>
          ?? ?<filter-name>requestContextFilter</filter-name>
          ?? ?<url-pattern>/*</url-pattern>
          </filter-mapping>
          ...
          </web-app>

          ----------------------------------------------------------------------
          RequestContextListener and RequestContextFilter 兩個類做的都是同樣的工作;將HTTP request 對象綁定到為該請求提供服務的Thread。這使具有request or session 作用域的bean能夠在后面的調用鏈中被訪問到。


          作用域bean 與依賴
          如果打算將一個Http request 范圍的bean 注入到別一個bean 中,那么需要注入一個AOP代理來替代被注入的作用域bean,也就是說需要注入一個代理對象。
          注意
          <aop:scoped-proxy/>不能和作用域為singleton 或 prototype 的bean一起使用。

          <?xml version="1.0" encoding="UTF-8"?>
          <beans xmlns="http://www.springframework.org/schema/beans"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xmlns:aop="http://www.springframework.org/schema/aop"
          xsi:schemaLocation="
          http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
          http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
          <!-- a HTTP Session-scoped bean exposed as a proxy -->
          <bean id="userPreferences" class="com.foo.UserPreferences" scope="session">
          <!-- this next element effects the proxying of the surrounding bean -->
          <aop:scoped-proxy/>
          </bean>
          <!-- a singleton-scoped bean injected with a proxy to the above bean -->
          <bean id="userService" class="com.foo.SimpleUserService">
          <!-- a reference to the proxied 'userPreferences' bean -->
          <property name="userPreferences" ref="userPreferences"/>
          </bean>




          初始化回調

          可以在Bean 定義中指定一個普通的初始化方法,即在XML配置文件中通過指定init-method 屬性來完成。

          <bean id="exampleInitBean" class="examples.ExampleBean" init-method="init" />

          public class ExampleBean {
          ?? ?public void init(){
          ?? ??? ?//do some initialization work
          ?? ?}
          }


          析構回調
          <bean id="exampleInitBean" class="examples.ExampleBean" destory-method="cleanup"/>

          public class ExampleBean {
          ?? ?public void cleanup(){
          ?? ??? ?//do some destruction work..
          ?? ?}
          }


          posted @ 2006-12-26 13:35 ziwolf 閱讀(367) | 評論 (0)編輯 收藏
          private byte[] md5(String strSrc){
          ??byte[] returnByte = null;
          ??try{
          ??????MessageDigest md5 = MessageDigest.getInstance("MD5");
          ??????returnByte = md5.digest(strSrc.getBytes("UTF8"));
          ???}catch(Exception e){
          ??????e.printStackTrace();
          ??}
          ??return returnByte;
          ?}
          ?
          ?public String getBase64Encode(byte[] src){
          ????String requestValue="";
          ????try{
          ???BASE64Encoder base64en =? new BASE64Encoder();
          ???requestValue = base64en.encode(src);
          ??}catch(Exception e){
          ???e.printStackTrace();
          ??}
          ????return requestValue;
          ?}
          posted @ 2006-12-22 11:50 ziwolf| 編輯 收藏
          主站蜘蛛池模板: 丰都县| 辽中县| 武邑县| 阿拉善右旗| 灵台县| 赤城县| 邹平县| 文化| 云安县| 化德县| 黄平县| 襄垣县| 剑川县| 洞头县| 淮北市| 建德市| 马鞍山市| 临澧县| 勐海县| 达日县| 民和| 贞丰县| 泰宁县| 普洱| 神木县| 南岸区| 普兰店市| 宁安市| 望都县| 小金县| 沾化县| 德令哈市| 来凤县| 永济市| 高要市| 裕民县| 怀远县| 耒阳市| 射洪县| 余庆县| 迭部县|