数据结构---Java---String
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);
}
}
数据结构---Java---String的更多相关文章
- Java String学习笔记
参照:https://www.jianshu.com/p/2f209af80f84 常量池: Java代码被编译成class文件时,会生成一个常量池(Constant pool)的数据结构,用以保存字 ...
- 程序兵法:Java String 源码的排序算法(一)
摘要: 原创出处 https://www.bysocket.com 「公众号:泥瓦匠BYSocket 」欢迎关注和转载,保留摘要,谢谢! 这是泥瓦匠的第103篇原创 <程序兵法:Java Str ...
- 纯数据结构Java实现(5/11)(Set&Map)
纯数据结构Java实现(5/11)(Set&Map) Set 和 Map 都是抽象或者高级数据结构,至于底层是采用树还是散列则根据需要而定. 可以细想一下 TreeMap/HashMap, T ...
- 面试话痨(二)C:JAVA String,别以为你穿个马甲我就不认识你了
面试话痨系列是从技术广度的角度去回答面试官提的问题,适合萌新观看! 面试官,别再问我火箭怎么造了,我知道螺丝的四种拧法,你想听吗? String相关的题目,是面试中经常考察的点,当面试中遇到了St ...
- Redis 5 种基本数据结构(String、List、Hash、Set、Sorted Set)详解 | JavaGuide
首发于:Redis 5 种基本数据结构详解 - JavaGuide 相关文章:Redis常见面试题总结(上) . Redis 5 种基本数据结构(String.List.Hash.Set.Sorted ...
- 从Java String实例来理解ANSI、Unicode、BMP、UTF等编码概念
转(http://www.codeceo.com/article/java-string-ansi-unicode-bmp-utf.html#0-tsina-1-10971-397232819ff9a ...
- Java String.split()小点
java String.split(); 别的不说,单说其中一个问题,这个函数去切分空字符串时,得到的结果: public static void main(String[] args) {// St ...
- Java总结篇系列:Java String
String作为Java中最常用的引用类型,相对来说基本上都比较熟悉,无论在平时的编码过程中还是在笔试面试中,String都很受到青睐,然而,在使用String过程中,又有较多需要注意的细节之处. 1 ...
- java String.split()函数的用法分析
java String.split()函数的用法分析 栏目:Java基础 作者:admin 日期:2015-04-06 评论:0 点击: 3,195 次 在java.lang包中有String.spl ...
- java string类型的初始化
以下基本上是java string类型最常用的三种方法 new string()就不介绍了 基本等同于第三种 String a; 申明一个string类型的 a,即没有在申请内存地址,更没有在内存 ...
随机推荐
- python处理文件某行的固定位置
1.打开文件 2.按行循环 3.处理固定行 with open('file/Aa.txt') as f: for line in f: print(line[2:12]) 可以这样处理的原因是,lin ...
- SQL的各种连接--自联结,内连接,外连接,交叉连接
1.准备两个表:Student,Course,其中student.C_S_Id=Course.C_Id(即Student 表中的 C_S_Id 字段为外键列,关联的是 Course 表的 C_Id 主 ...
- 3.1.2 Socket网络通信开发
Socket语法 Python中,我们用Socket()函数来创建套接字,语法如下: socket.socket([family[, type[, proto]]]) 参数 family:套接字家族可 ...
- CDH6.3 Centos7
按照官方文档安装即可 CentOS7 上搭建 CDH(6.3.0) 官方文档:https://docs.cloudera.com/documentation/enterprise/6/6.3/topi ...
- JAVA代码覆盖率采集与分析方案
原文地址-> http://m.blog.csdn.net/article/details?id=48688763
- gensim word2vec |来自渣渣硕的学习笔记
最近写论文跑模型,要用到word2vec,但是发现自己怎么也看不懂网上的帖子,还是自己笨吧,所以就有了我的第一篇博客!!! 关于word2vec工具打算写一个系列的,当然今天这篇文章只打算写: 如何 ...
- boost Shared Memory
Shared Memory Shared memory is typically the fastest form of interprocess communicatioin. It provide ...
- 如何在pycharm上创建分支,并且把它推送到远端仓库
注意创建的分支名 ,如果远端仓库没有pycharm中创建的分支名时 此时远端仓库会创建一个分支出来 这是就方便了代码的管理 具体步骤如下图操作步骤 推送上去搞定
- springboot实战(汪云飞)学习-1-1
java EE开发的颠覆者 spring boot 实战 随书学习-1 1.学习案例都是maven项目,首先要在eclipse 中配置 maven,主要修改maven的配置文件:配置文件下载链接: h ...
- project 计划添加编号或 任务分解时为任务添加编号
[工具]-[选项]-[视图]-选择[显示大纲数字]-[确定]