getChars
1 | /*** |
valueOf
该方法有以下几种形式,分别对应不同的输入类型1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47/** 返回 Object 参数的字符串表示形式 */
public static String valueOf(Object obj) {
return (obj == null) ? "null" : obj.toString();
}
/**
* 返回 boolean 参数的字符串表示形式
* 说明:如果参数为 true,则返回一个等于 "true" 的字符串;
* 否则,返回一个等于 "false" 的字符串。
*/
public static String valueOf(boolean b) {
return b ? "true" : "false";
}
/** 返回 char 参数的字符串表示形式 */
public static String valueOf(char c) {
char data[] = {c};
return new String(data, true);
}
/** 返回 int 参数的字符串表示形式 */
public static String valueOf(int i) {
return Integer.toString(i);
}
/** 返回 long 参数的字符串表示形式 */
public static String valueOf(long l) {
return Long.toString(l);
}
/** 返回 float 参数的字符串表示形式 */
public static String valueOf(float f) {
return Float.toString(f);
}
/** 返回 double 参数的字符串表示形式 */
public static String valueOf(double d) {
return Double.toString(d);
}
/**
* 返回 char 数组参数的特定子数组的字符串表示形式
* 说明:字符数组的内容已被复制,后续修改不会影响新创建的字符串
*/
public static String valueOf(char data[], int offset, int count) {
return new String(data, offset, count);
}
intern
源码:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21/*
Returns a canonical representation for the string object.
A pool of strings, initially empty, is maintained privately by the class String.
When the intern method is invoked, if the pool already contains a string equal to
this String object as determined by the equals(Object) method, then the string
from the pool is returned. Otherwise, this String object is added to the pool
and a reference to this String object is returned.
It follows that for any two strings s and t, s.intern() == t.intern() is true
if and only if s.equals(t) is true.
All literal strings and string-valued constant expressions are interned.
String literals are defined in section 3.10.5 of the The Java™ Language Specification.
Returns:
a string that has the same contents as this string,
but is guaranteed to be from a pool of unique strings.
*/
public native String intern();
由源码可知,当调用intern
方法时,如果字符串资源池中已经包含一个等于该String对象的字符串(通过equals(object)方法确定),则返回资源池中相等字符串的引用;否则,将该String对象添加到资源池中,并返回该String对象的引用。
例1:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17public class StringDemo {
public static void main(String[] args) {
String s1 = new String("1");
s1.intern();
String s2 = "1";
System.out.println(s1 == s2);
String s3 = new StringBuilder("1").append("1").toString();
s3.intern();
String s4 = "11";
System.out.println(s3 == s4);
}
}
/* output
false
true
*/
例2:1
2
3
4
5
6
7
8
9
10
11
12public class StringDemo {
public static void main(String[] args) {
String str1 = new StringBuilder("hello").append("string").toString();
System.out.println(str1.intern() == str1);
String str2 = new StringBuilder("ja").append("va").toString();
System.out.println(str2.intern() == str2);
}
}
/* output
true
false
*/
问题待解决。。。