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