`
deepfuture
  • 浏览: 4335744 次
  • 性别: Icon_minigender_1
  • 来自: 湛江
博客专栏
073ec2a9-85b7-3ebf-a3bb-c6361e6c6f64
SQLite源码剖析
浏览量:79448
1591c4b8-62f1-3d3e-9551-25c77465da96
WIN32汇编语言学习应用...
浏览量:68428
F5390db6-59dd-338f-ba18-4e93943ff06a
神奇的perl
浏览量:101577
Dac44363-8a80-3836-99aa-f7b7780fa6e2
lucene等搜索引擎解析...
浏览量:281358
Ec49a563-4109-3c69-9c83-8f6d068ba113
深入lucene3.5源码...
浏览量:14625
9b99bfc2-19c2-3346-9100-7f8879c731ce
VB.NET并行与分布式编...
浏览量:65628
B1db2af3-06b3-35bb-ac08-59ff2d1324b4
silverlight 5...
浏览量:31356
4a56b548-ab3d-35af-a984-e0781d142c23
算法下午茶系列
浏览量:45243
社区版块
存档分类
最新评论

lucene3.5之StringInterner

 
阅读更多
首先讲解关于java的intern
public String intern()返回字符串对象的规范化表示形式。
一个初始时为空的字符串池,它由类 String 私有地维护。

当调用 intern 方法时,如果池已经包含一个等于此 String 对象的字符串(该对象由 equals(Object) 方法确定),则返回池中的字符串。否则,将此 String 对象添加到池中,并且返回此 String 对象的引用。

它遵循对于任何两个字符串 s 和 t,当且仅当 s.equals(t) 为 true 时,s.intern() == t.intern() 才为 true。
注意,equals()表示2个变量是否引用了同一个对象实例,而==仅表示2个变量的值是否一致。




package org.apache.lucene.util;
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License.  You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* Subclasses of StringInterner are required to
* return the same single String object for all equal strings.
* Depending on the implementation, this may not be
* the same object returned as String.intern().
*
* This StringInterner base class simply delegates to String.intern().
*/
public class StringInterner {
  /** Returns a single object instance for each equal string. */
 
  public String intern(String s) {
    return s.intern();
  }

  /** Returns a single object instance for each equal string. */
  public String intern(char[] arr, int offset, int len) {
    return intern(new String(arr, offset, len));
  }
}


上述代码中
1)intern(String s)的机制是调用s.intern(),对于所有相同的字符串只返回一个对象的实例,是一个节约内存的好方法
2) public String intern(char[] arr, int offset, int len)完成一个返回某个
字符串从开始位置截取指定长度生成的新字符串对象的实例
其中用到了string的以下构造函数
public String(byte[] bytes,
              int offset,
              int length)构造一个新的 String,方法是使用指定的字符集解码字节的指定子数组。新的 String 的长度是一个字符集函数,因此不能等于该子数组的长度。
当给定字节在给定字符集中无效的情况下,该构造方法无指定的行为。当需要进一步控制解码过程时,应使用 CharsetDecoder 类。


参数:
bytes - 要解码为字符的字节
offset - 要解码的首字节的索引
length - 要解码的字节数
抛出:
IndexOutOfBoundsException - 如果 offset 和 length 参数索引字符超出 bytes 数组的范围
分享到:
评论
1 楼 BuN_Ny 2012-04-28  
然后呢?这些不是javaAPI的介绍么?用intern跟 String s = "abc"有什么区别?这样如果常量池里有这个对象,不也是共用的么?还有lucene为啥封装这个啊?感觉比原生的api也不方便。

相关推荐

Global site tag (gtag.js) - Google Analytics