分享

Java面试中关于String的问题总结 (太全面了)

 hh3755 2014-07-16

 Java面试中总喜欢出几个关于String对象的题,比如:

1.String s = new String("abc");创建了几个String对象。

2.String s1 = "abc";
   String s2 = "a";
   String s3 = s2 + "bc";
   String s4 = "a" + "bc";
   String s5 = s3.intern();
请问s1==s3是true还是false,s1==s4是false还是true。s1==s5呢?

这样的题碰到很多了,今天来全面的总结一下,之所以敢说全面,是因为我参考的是最权威的资料……Java语言规范和Java API。

API中说明:

字符串是常量;它们的值在创建之后不能更改。因为 String 对象是不可变的,所以可以共享。(http://gceclub./Java_Docs/jdk6/html/zh_CN/api/java/lang/String.html)

那么,是如何共享的勒,String类的intern方法有如下说明:

字符串池,初始为空,它由类 String 私有地维护。(http://gceclub./Java_Docs/jdk6/html/zh_CN/api/java/lang/String.html#intern())

也就是说,String类自己维护了一个池,把程序中的String对象放到那个池里面,于是我们代码中只要用到值相同的String,就是同一个String对象,节省了空间。

但是,并不是任何时候任何String对象都在这个字符串池中,不然也就不会有那些面试题了。那么,到底哪些String对象在池中,哪些在堆中?请看Java语言规范。

Java语言规范第三版,第3.10.5小节,String Literals的最后一段如下:

 

Literal strings within the same class (§8) in the same package (§7) represent references to the same String object (§4.3.1).

Literal strings within different classes in the same package represent references to the same String object.

Literal strings within different classes in different packages likewise represent references to the same String object.

Strings computed by constant expressions (§15.28) are computed at compile time and then treated as if they were literals.

Strings computed by concatenation at run time are newly created and therefore distinct.

The result of explicitly interning a computed string is the same string as any pre-existing literal string with the same contents.

以上6句话就是权威且全面的解释了。我依次解释下。

首先解释下什么是字符串字面常数(String Literals),字面常数(Literals)就是你写在源代码里面的值,比如说int i = 6; 6就是一个整数形字面常数。String s = "abc"; “abc”就是一个字符串字面常数。Java中,所有的字符串字面常数都放在上文提到的字符串池里面,是可以共享的,就是说,String s1 = "abc"; String s2 = "abc"; s1,s2都引用的同一个字符串对象,而且这个对象在字符串池里面,因此s1==s2。另外,字符串字面常数是什么时候实例化并放到字符串池里面去的呢?答案是Load Class的时候(Java Spec 12.5)。

下面看那6句话。

前面三句基本废话,想绕口令一样,意思就是说任何类任何包,值相同的字符串字面常数(String Literals)都引用同一个对象。

第四句是说,通过常量表达式(constant expressions)计算出来的字符串,也算字符串字面常数,就是说他们也在字符串池中。什么是常量表达式(constant expressions)待会说,这个很重要。

注意第五句话,在程序运行时通过连接(+)计算出来的字符串对象,是新创建的,他们不是字面常数,就算他们值相同,他们也不在字符串池里面,他们在堆内存空间里,因此引用的对象各不相同。

最后一句话也很重要,String类的intern方法,返回一个值相同的String对象,但是这个对象就像一个字符串字面常数一样,意思就是,他也到字符串池里面去了。

现在我们来看开头的两个题目。

 

1.String s = new String("abc");创建了几个String对象。

答案是2个,一个是字符串字面常数,在字符串池中。一个是new出来的字符串对象,在堆中。

2.String s1 = "abc";
   String s2 = "a";
   String s3 = s2 + "bc";
   String s4 = "a" + "bc";
   String s5 = s3.intern();
请问s1==s3是true还是false,s1==s4是false还是true。s1==s5呢?

此题注意两点,因为s2是一个变量,所以s3是运行时才能计算出来的字符串,是new的,在堆中不在字符串池中。s4是通过常量表达式计算出来的,他等同于字符串字面常数,在字符串池中。所以,s1!=s3,s1==s4。再看s5,s5是s3放到字符串池里面返回的对像,所以s1==s5。这里新手要注意的是,s3.intern()方法,是返回字符串在池中的引用,并不会改变s3这个变量的引用,就是s3还是指向堆中的那个"abc",并没有因调用了intern()方法而改变,实际上也不可能改变。

好,现在我们回到前文没有说清楚的一个问题,到底什么算常量表达式(constant expressions)。上面提到了两种非常量表达式,new 和变量相加。至于什么算常量表达式(constant expressions),请看Java语言规范。

 

15.28 Constant Expression

 ConstantExpression:         Expression

A compile-time constant expression is an expression denoting a value of primitive type or a String that does not complete abruptly and is composed using only the following:

  • Literals of primitive type and literals of type String 
  • Casts to primitive types and casts to type String
  • The unary operators +-~, and (but not ++ or --)
  • The multiplicative operators */, and %
  • The additive operators + and -
  • The shift operators <<>>, and >>>
  • The relational operators <<=>, and >= (but not instanceof)
  • The equality operators == and !=
  • The bitwise and logical operators &^, and |
  • The conditional-and operator && and the conditional-or operator ||
  • The ternary conditional operator ? :
  • Parenthesized expressions whose contained expression is a constant expression.
  • Simple names that refer to constant variables
  • Qualified names of the form TypeName . Identifier that refer to constant variables

Compile-time constant expressions are used in case labels in switch statements and have a special significance for assignment conversion. Compile-time constants of type String are always "interned" so as to share unique instances, using the method String.intern.

A compile-time constant expression is always treated as FP-strict, even if it occurs in a context where a non-constant expression would not be considered to be FP-strict.

看着是不是有点晕,这情况也太多了,面试应该不会那么变态的。其实看着复杂,记住一个原则就好了,那就是编译时能确定的,就算,运行时才能确定的,就不算。以下为例子

String s = 1 + "23";//算,符合第二条Casts to primitive types and casts to type String
String s = (2 > 1) + "" ;//算,意味着s=="true",且这个“true”已经放到字符串池里面去了。
String s = (o instanceof Object) + "";//不算,instanceof这个操作符决定了不算。s=="true",但这个"true"对象在堆中。

留意下面的情况。

   final String s2 = "a";
   String s3 = s2 + "bc";//算

注意现在的s2+"bc"也算一个常量表达式,理由是那个列表里面的最后两条,s2是一个常量变量(constant variables),问题又来了,什么是常量变量?规范里也说了,被final修饰,并且通过常量表达式初始化的变量,就是常量变量。变量s2被final修饰,他通过常量表达式"a"初始化,所以s2是一个常量变量,所以s3引用的"abc",也在字符串池里面咯,明白了吧。

再举个反例:

final String s2 = getA();//s2不是常量变量,但是s2引用的"a"其实还是在常量池中,这两点不矛盾
public String getA(){return "a"}

String s3 = s2 + "bc";//此时s3不算常量表达式,因为s2不是常量变量

这是时候的s2,就不是常量变量了哦,因为getA()不是一个常量表达式

 

好,啰啰嗦嗦说了这么多,大概应该说清楚了。有错误欢迎指证,希望对您有用~  

 

 2012/9/15 根据网友lenbias34的回复补充了一点内容

 

 

 

本文出自 “讨厌萝卜的试验田” 博客,请务必保留此出处http://cymoft.blog.51cto.com/324099/473220

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多