String对象是JAVA语言中重要的数据类型,但是不是基本数据类型,属于引用数据类型

任何一Project中,无疑字符串的操作是最多的了

String的内部结构:char数组,offset偏移量,count长度

public final class Stringimplements java.io.Serializable, Comparable<String>, CharSequence
{/** The value is used for character storage. */private final char value[];/** The offset is the first index of the storage that is used. */private final int offset;/** The count is the number of characters in the String. */private final int count;
<span style="white-space:pre">  </span>。。。
 public String() {this.offset = 0;this.count = 0;this.value = new char[0];}
public String(String original) {int size = original.count;char[] originalValue = original.value;char[] v;if (originalValue.length > size) {// The array representing the String is bigger than the new// String itself.  Perhaps this constructor is being called// in order to trim the baggage, so make a copy of the array.int off = original.offset;v = Arrays.copyOfRange(originalValue, off, off+size);} else {// The array representing the String is the same// size as the String, so no point in making a copy.v = originalValue;}this.offset = 0;this.count = size;this.value = v;}
 public String(char value[]) {int size = value.length;this.offset = 0;this.count = size;this.value = Arrays.copyOf(value, size);}
 public String(char value[], int offset, int count) {if (offset < 0) {throw new StringIndexOutOfBoundsException(offset);}if (count < 0) {throw new StringIndexOutOfBoundsException(count);}// Note: offset or count might be near -1>>>1.if (offset > value.length - count) {throw new StringIndexOutOfBoundsException(offset + count);}this.offset = 0;this.count = count;this.value = Arrays.copyOfRange(value, offset, offset+count);}
....

以上是String源码中的内容,可以看到有很多的构造方法

String不变性,是指String对象一旦生成,就不能对它进行修改,即这个对象的状态在对象创建时就固定不变了,相当于不变模式(当一个对象被多线程共享时,并且频繁访问,可以省略同步和锁等待时间)。

String对常量池(字符串池)的优化

String s = "1";
s = s+"2";
s = s+"3";
以上这段代码,编译后,我们可以反编译看看编译后的代码是什么样子的
public static void main(String args[])
{
String s = "1";
s = (new StringBuilder()).append(s).append("2").toString();
s = (new StringBuilder()).append(s).append("3").toString();
}
以上因为编译期,不能得出最终的s是什么样子的,所以内部使用了StringBuilder优化
String s = "1"+"2"+"3";
-->
String s = "123";
在编译期知道s是什么样子的,所以直接优化
在常量池中,当两个String对象拥有相同的值时,它们引用常量池中的同一个拷贝,当同一个字符串反复出现时,可以节省大量内存空间
String str1="123";
String str2="123";
String str3=new String("123");
str1和str2是指向同一块内存地址空间,而str3则是另一块内存地址空间,但是最终都指向常量池中的同一个“123”字符串

使用indexOf()和substring()代替split(),效率更高

使用charAt()代替startsWith(),endsWith(),效率更高

public static Object[] mySplit(String source,String s) {List<String> list = new ArrayList<String>();int loc=source.indexOf(s),length=source.length();String temp = "";while(loc!=-1) {temp = source.substring(0, loc);list.add(temp);source=source.substring(loc+1, length);loc = source.indexOf(s);length=source.length();}return list.toArray();}
public static void main(String args[]) {String str = "KJDKGF-sdfgdssdfb-5241354651";int count = 10000000;String temp = "";long begin = System.currentTimeMillis();for(int i=0;i<count;i++) {temp = str.split("-")[0];}System.out.println(System.currentTimeMillis()-begin);begin = System.currentTimeMillis();for(int i=0;i<count;i++) {temp = str.substring(0,str.indexOf("-"));}System.out.println(System.currentTimeMillis()-begin);}

10000000次操作,从以下结果可以很明显的看出来的性能的差异,在计算机的世界里1ns都应该珍惜啊

1365
129

相差10倍的性能

String,无所不在的数据类型相关推荐

  1. java 字符串是对象吗_解析Java中的String对象的数据类型

    解析Java中的String对象的数据类型 2007-06-06 eNet&Ciweek 1. 首先String不属于8种基本数据类型,String是一个对象. 因为对象的默认值是null,所 ...

  2. String类以及String与基本数据类型/char[]/byte[]之间的转换

    String类: 1.String 声明为final的,不可被继承 2.String实现了Serializable接口:表示字符串是支持序列化的. 实现了Comparable接口:表示String可以 ...

  3. boolean类型默认值_【Java基础】还在问String属于什么数据类型

    1.前言 首先提出一个问题在 Java 中 String 属于那种数据类型? 我的一个朋友是这么说的,「what?是不是有很多疑惑,int.double.String 不都是经常一起使用的么,应该都是 ...

  4. java基本数据类型自动转包装类,Java String和基本数据类型之间的转换(包装类)

    一.String 转化成 基本数据类型 利用基本数据类型对应的包装类的parseXxx() 或 valueOf() 方法 注意 : String 对象的字面量 是 数字类型.否则会报异常(Number ...

  5. string替换_GEE数据类型—String,Number

    String和Number 是GEE中最基本的数据类型 1 String 注意在给变量赋值的时候,不仅关注值是什么,还要关注值是什么格式 //创建一个String变量 ee.String( )告诉GE ...

  6. Redis string和hash数据类型

    文章目录 一.redis 数据存储格式 二.string 1. string基本操作 2. string扩展操作 3. 数据库中热点数据key命名规范 4. string类型注意事项 三.hash 1 ...

  7. String是基本数据类型吗?

    1. 本期答案 String不是基本数据类型,是引用数据类型. 2. 分析 2.1 Java中有哪些数据类型 Java语言是强类型语言(所有变量必须先定义后使用),对于每一种数据都给出了明确的数据类型 ...

  8. 自动拆箱自动装箱以及String 和基本数据类型封装类生成的对象是否相等

    自动拆箱(unboxing)&自动装箱(boxing) @author 李东秀|| qq:1028659927 本文主要为自己理解所做的学习笔记,如有不对的地方, 望各位看官不吝指出,代码运行 ...

  9. string是什么数据类型?

    string属于Java中的字符串类型,也是一个引用类型,并不属于基本的数据类型. Java中基本的数据类型只有八个,分别是数值型:byte.short.int.long:浮点型:float.doub ...

最新文章

  1. 黄聪:Python+NLTK自然语言处理学习(三):计算机自动学习机制
  2. python3.6 在 windows10 下使用pycrypto
  3. 项目交付为什么失败?-记我在某个项目中的迷思
  4. CTFshow 反序列化 web256
  5. Fiddler本机调试的方法
  6. 人工智能实现a*算法解决八数码_小白带你学回溯算法
  7. 飞桨领航团邀你出战!黑客松线下48H Coding Party等你来
  8. Emscripten教程之入门指导
  9. 发现不错的文章,推!
  10. sp_xml_preparedocument 处理xml文档
  11. 计算2的幂(信息学奥赛一本通-T1037)
  12. 计算机任务类别 搜索服务,服务删除后仍要求启动 计划任务里找不到 该怎么办...
  13. 使用 matlab 进行正太拟合
  14. tar.xz如何解压:linux和windows下tar.xz解压命令介绍
  15. vs中四点画矩形的算法_实战基于图割算法的木材表面缺陷图像分析
  16. 如何快速设计《数字电路》的JK触发器、T触发器描述的驱动方程对应的次态K图——异或卡诺图法
  17. 51单片机最小系统板制作
  18. var,let const,const 变量提升
  19. 若依开发文档手册[持续更新:拥抱初次使用若依的开发者]
  20. 震撼心灵、洗礼灵魂--【经典的大师参禅的禅语】

热门文章

  1. 计算机工程研究生美国专业排名,美国计算机研究生专业排名
  2. 云计算运营—01华为云计算解决方案介绍
  3. LeetCode/LintCode 题解丨一周爆刷双指针:神奇字符串
  4. 软考一般什么时候出成绩呢?
  5. AA Master考试认证笔记
  6. 【全网详解】从0到1搭建双十一实时交易数据展示平台——Spark+Kafka构建实时分析系统
  7. 三角函数积分的换元法
  8. 常说的“四层”和“七层”是什么
  9. python 扯线木偶_年轻的50个细节
  10. php++ui设计课程,UI设计主要学什么?