String若干问题

原创 2007年09月19日 22:10:00
主要就是string literals1 及constant pool问题了,看以下代码。
public class StringExample {
    
public static void main(String[] args) {
        String s0 
= "Programming";
        String s1 
= "Program"+"ming";
        String s2 
= new String("Programming");
        String s3 
= new String("Programming").intern();
        System.out.println(
"s0 == s1 :"+(s0==s1));
        System.out.println(
"s0 == s2 :"+(s0==s2));
        System.out.println(
"s0 == s3 :"+(s0==s3));
    }

}


运行结果:
s0 
== s1 :true
s0 
== s2 :false
s0 
== s3 :true

 

public class StringTest{
    String s1;
    
public StringTest(){
        s1 
= "Programming";
    }

}


public class StringExample {
    String s0;
    
public StringExample(){
        s0 
= "Programming";
    }

    
public static void main(String[] args) {
        System.out.println(
new StringExample().s0 == new StringTest().s1);
    }

}


运行结果:
ture

 

 


1From the Java Language Specification: “A string literal consists of zero or more characters enclosed in double quotes. Each character may be represented by an escape sequence.”

 

The “constant pool” is something that is computed at compile time and stored with the compiled .class file. It contains information on methods, classes, interfaces, …, and string literals. When the JVM loads the .class file and the s0 and s2 variables are resolved, the JVM does something called constant pool resolution. The process of constant pool resolution for string follows these steps, as taken from the Java Virtual Machine Specification (5.4):

  If another constant pool entry tagged CONSTANT_String2 and representing the identical sequence of Unicode characters has already been resolved, then the result of resolution is a reference to the instance of class String created for that earlier constant pool entry.

2This is used internal to the .class file to identify literals.
  Otherwise, if the method intern has previously been called on an instance of class String containing a sequence of Unicode characters identical to that represented by the constant pool entry, then the result of resolution is a reference to that same instance of class String.
  Otherwise, a new instance of class String is created containing the sequence of Unicode characters represented by the CONSTANT_String entry; that class instance is the result of resolution.

 

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

TCHAR 与 STD::string 之间的若干问题

TCHAR 与 STD::string 之间的若干问题 我经常在 C++ 程序中使用标准模板库(STL)的 std::string 类,但在 使用 Unicode 时...

Java String中若干问题

关于String的若干问题: String中的“==”与equals() 方法: (1)对于String对象使用“==”判定时,其实是判定两个String对象是否为同一String对象的引用; St...

网格Cache若干问题分析.pdf

  • 2014-09-20 14:39
  • 314KB
  • 下载

视频监控的若干问题

  • 2014-07-30 20:32
  • 2.34MB
  • 下载

Linux下配置vsftp的若干问题

# 530 Login incorrect. FTP中有一个名叫ftpusers的文件,该文件未在配置文件vsftpd.conf中指明。也就是说只要ftpusers这个文件存在,它就起作用,里面写的都...

51单片机若干问题

  • 2015-05-07 10:50
  • 117KB
  • 下载

用iframe进行表单提交时的若干问题总结

这篇文章主要用来探讨在使用iframe进行表单提交时一系列浏览器兼容性以及iframe本身的一些限制。 iframe的主要用途一般是运用在无刷新提交数据并做出响应的场景,可以作为ajax方式的有...

关于word的若干问题回答

  • 2012-05-19 21:51
  • 52KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)