1、概述

    1.1  源码(JDK1.8)

public final class String implements java.io.Serializable, Comparable<String>, CharSequence {

     //==========属性
private final char value[];
private int hash; //==========构造器
public String() {
this.value = "".value;
} public String(String original) {
this.value = original.value;
this.hash = original.hash;
} //**************char[]
public String(char value[]) {
this.value = Arrays.copyOf(value, value.length);
} //****************char[],charset
public String(byte bytes[], Charset charset) {
this(bytes, 0, bytes.length, charset);
} public String(byte bytes[], int offset, int length, Charset charset) {
if (charset == null)
throw new NullPointerException("charset");
checkBounds(bytes, offset, length);
this.value = StringCoding.decode(charset, bytes, offset, length);
} //***********bytes[]
public String(byte bytes[]) {
this(bytes, 0, bytes.length);
} //**********StringBuffer
public String(StringBuffer buffer) {
synchronized(buffer) {
this.value = Arrays.copyOf(buffer.getValue(), buffer.length());
}
} //**************StringBuilder
public String(StringBuilder builder) {
this.value = Arrays.copyOf(builder.getValue(), builder.length());
} //============方法
public char charAt(int index) {
if ((index < 0) || (index >= value.length)) {
throw new StringIndexOutOfBoundsException(index);
}
return value[index];
} public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = value.length;
if (n == anotherString.value.length) {
char v1[] = value;
char v2[] = anotherString.value;
int i = 0;
while (n-- != 0) {
if (v1[i] != v2[i])
return false;
i++;
}
return true;
}
}
return false;
} public boolean startsWith(String prefix) {
return startsWith(prefix, 0);
} public boolean endsWith(String suffix) {
return startsWith(suffix, value.length - suffix.value.length);
} public int hashCode() {
int h = hash;
if (h == 0 && value.length > 0) {
char val[] = value; for (int i = 0; i < value.length; i++) {
h = 31 * h + val[i];
}
hash = h;
}
return h;
} public int indexOf(String str) {
return indexOf(str, 0);
} public String substring(int beginIndex, int endIndex) {
if (beginIndex < 0) {
throw new StringIndexOutOfBoundsException(beginIndex);
}
if (endIndex > value.length) {
throw new StringIndexOutOfBoundsException(endIndex);
}
int subLen = endIndex - beginIndex;
if (subLen < 0) {
throw new StringIndexOutOfBoundsException(subLen);
}
return ((beginIndex == 0) && (endIndex == value.length)) ? this
: new String(value, beginIndex, subLen);
} public boolean contains(CharSequence s) {
return indexOf(s.toString()) > -1;
} public String[] split(String regex) {
return split(regex, 0);
} public native String intern();
}

    1.2  String的存储位置

package com.an.string;

public class Test {
public static void main(String[] args){
test1();
test2();
test3();
test4();
test5();
} /**
* java存储于 常量池中
* s1、s2存储于 堆内存中
* 结果:false、true
*/
public static void test1(){
String s1=new String("java");
String s2=new String("java");
System.out.println(s1==s2);
System.out.println(s1.equals(s2));
} /**
* java 存储于 常量池中
* 结果:true、true
*/
public static void test2(){
String s1="java";
String s2="java";
System.out.println(s1==s2);
System.out.println(s1.equals(s2));
} /**
* s1、s2 、s4存放于常量池中
* s3 在运行期才会进行计算赋值
* 结果:false
*/
public static void test3(){
String s1="java ";
String s2="hi";
String s3=s1+s2;
String s4="java hi";
System.out.println(s3==s4);
} /**
* final修饰的,在编译期涉及该对象的会直接进行计算赋值
* s1、s2 、s4存放于常量池中
* s3 在编译期进行计算赋值
* 结果:true
*/
public static void test4(){
final String s1="java ";
final String s2="hi";
String s3=s1+s2;
String s4="java hi";
System.out.println(s3==s4);
} /**
* s1、hi、java存储于常量池中
* 结果:true
*/
public static void test5(){
String s1="hi java";
String s2="hi "+"java";
System.out.println(s1==s2);
}
}

  

最新文章

  1. [LeetCode] Battleships in a Board 平板上的战船
  2. DirectX游戏编程(一):创建一个Direct3D程序
  3. C# GDI+ 处理文本的两个小技巧
  4. px 与 dp, sp换算公式?
  5. Windows7中IIS简单安装与配置(详细图解)
  6. EBS应用服务器启动指南
  7. IOS开发UI基础UITextView相关属性
  8. 卷积相关公式的matlab代码
  9. Cent OS 常用 命令
  10. markdown 简明语法
  11. 关于Union和Union All的区别以及用法
  12. 利用JAVA API远程进行HDFS的相关操作
  13. Handler学习小结
  14. 性能测试工具 wrk 使用教程
  15. C博客作业01--分支,顺序结构
  16. Hibernate若干知识点
  17. scanf,fscanf,sscanf的区别
  18. 利用可道云kodexplorer在树莓派raspbian上搭建私有云网盘
  19. PowerDesigner 生成C#实体模版代码
  20. elasticsearch5使用snapshot接口备份索引

热门文章

  1. tuple unpacking
  2. 透明的UITableView
  3. BZOJ 4238 电压 解题报告
  4. 9.26-mkdir,tree,touch命令
  5. Windows Server服务器之用户界面,任务管理器等
  6. vue 中使用scss
  7. 微信网页开发调用微信jssdk接口遇到的坑以及最终解决方法 (持续更新)
  8. 北风设计模式课程---外观模式(Facade)总结
  9. Windows 08 R2_组策略
  10. Centos7从零开始】Centos 下硬盘分区的最佳方案