您好,欢迎访问一九零五行业门户网

2w字 详解 String,yyds

前言
大家好,今天给大家分享java基础知识之string。
string类的重要性就不必说了,可以说是我们后端开发用的最多的类,所以,很有必要好好来聊聊它。
本文主要内容如下:
string简介 我们先来说说,java中八大数据类型,然后在说string。
八大基本数据类型byte:8位,最大存储数据量是255,存放的数据范围是-128~127之间。
short:16位,最大数据存储量是65536,数据范围是-32768~32767之间。
int:32位,最大数据存储容量是2的32次方减1,数据范围是负的2的31次方到正的2的31次方减1。
long:64位,最大数据存储容量是2的64次方减1,数据范围为负的2的63次方到正的2的63次方减1。
float:32位,数据范围在3.4e-45~1.4e38,直接赋值时必须在数字后加上f或f。
double:64位,数据范围在4.9e-324~1.8e308,赋值时可以加d或d也可以不加。
boolean:只有true和false两个取值。
char:16位,存储unicode码,用单引号赋值。
除了这八大数据类型以外(八大数据类型也有与之对应的封装类型,我相信你是知道的),java中还有一种比较特殊的类型:string,字面意义就是字符串。
string官方介绍英文版
地址:https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/string.html
看不懂吗?没事,我们可以借用翻译工具,浏览器自带的,更希望的是你能看懂原版英文。
string 存在于咱们安装的jdk目录下rt.ar包中,全路径名为:java.lang.string。我们java代码中string用来表示字符串,比如:
string str = "中国梦,我的梦";string name = "zhangsan";
暂时先知道这些就可以了。
string使用 定义类型在日常开发中,使用string的地方太多了,尤其是用来定义变量、常量的类型,基本上只要你码代码,总是能见到它。
比如:用户信息,用实体类user来表示。
public class user{ private long id; private string username; private string address; private string password; ....}
常用方法演示string类有20多个方法,下面给出一个使用示例(这里演示大部分方法,剩下的可以自行去试试)。
//案例代码,来源于网络public class stringdemo { public static void main(string[] args) throws exception { string str1 = "hello world"; string str2 = "hello world"; string str3 = "hello world"; string str4 = " hello world "; //返回字符串的长度 system.out.println("r1: " + str1.length()); //比较两个字符串的大小compareto(返回的是int),0相等,复数小于,正数大于 system.out.println("r2 : " + str1.compareto(str2)); //比较两个字符串的大小compareto(返回的是int),0相等,复数小于,正数大于 system.out.println("r3 : " + str1.compareto(str3)); //字符串比较comparetoignorecase,忽略大小写。0相等,复数小于,正数大于 system.out.println("r4 : " + str1.comparetoignorecase(str3)); //字符串查找indexof,返回的是找到的第一个的位置,没找到返回-1。从0开始 system.out.println("r5 : " + str1.indexof("o")); //查找字符串最后一次出现的位置lastindexof system.out.println("r6 : " + str1.lastindexof("o")); //删除字符串中的一个字符,字符串从0开始的 substring(a, b) //返回指定起始位置(含)到结束位置(不含)之间的字符串 system.out.println("r7 : " + str1.substring(0, 5) + str1.substring(6)); //字符串替换,替换所有 system.out.println("r8 : " + str1.replace("o", "h")); //字符串替换,替换所有 system.out.println("r9 : " + str1.replaceall("o", "h")); //字符串替换,替换第一个 system.out.println("r10 : " + str1.replacefirst("o", "h")); //字符串反转 system.out.println("r11 : " + new stringbuffer(str1).reverse()); //字符串反转 system.out.println("r11’: " + new stringbuilder(str1).reverse()); //字符串分割 string[] temp = str1.split("\\ "); for (string str : temp) { system.out.println("r12 : " + str); } //字符串转大写 system.out.println("r13 : " + str1.touppercase()); //字符串转小写 system.out.println("r14 : " + str1.tolowercase()); //去掉首尾空格 system.out.println("r15 : " + str4.trim()); //是否包含,大小写区分 system.out.println("r16 : " + str1.contains("world")); //返回指定位置字符 system.out.println("r17 : " + str1.charat(4)); //测试此字符串是否以指定的后缀结束 system.out.println("r18 : " + str1.endswith("d")); //测试此字符串是否以指定的前缀开始 system.out.println("r19 : " + str1.startswith("h")); //测试此字符串从指定索引开始的子字符串是否以指定前缀开始 system.out.println("r20 : " + str1.startswith("ll", 2)); //将指定字符串连接到此字符串的结尾。等价于用“+” system.out.println("r21 : " + str1.concat("haha")); //比较字符串的内容是否相同 system.out.println("r22 : " + str1.equals(str2)); //与equals方法类似,忽略大小写 system.out.println("r23 : " + str1.equalsignorecase(str2)); //判断是否是空字符串 system.out.println("r24: " + str1.isempty()); }}
我们开发中差不多也就是这么使用了,但是如果你仅仅是使用很牛了,貌似遇到面试照样会挂。所以,学知识,不能停留在使用层面,需要更深层次的学习。
下面我们就来深层次的学习string,希望大家带着一颗平常的心学习,不要害怕什么,灯笼是张纸,捅破不值钱。
string核心部分源码分析 备注:jdk版本为1.8+,因为jdk9版本中和旧版本有细微差别。
string类源码注释/** * the {@code string} class represents character strings. all * string literals in java programs, such as {@code "abc"}, are * implemented as instances of this class. * 这个string类代表字符串。java编程中的所有字符串常量。 * 比如说:"abc"就是这个string类的实例 * <p> * strings are constant; their values cannot be changed after they * are created. * 字符串是常量,他们一旦被创建后,他们的值是不能被修改。(重点) * string buffers support mutable strings. * string缓存池支持可变的字符串, * because string objects are immutable they can be shared. for example: * 因为string字符串不可变,但他们可以被共享。比如: * <blockquote><pre> * string str = "abc"; * </pre></blockquote><p> * is equivalent to: * <blockquote><pre> * char data[] = {'a', 'b', 'c'}; * string str = new string(data); * </pre></blockquote><p> * here are some more examples of how strings can be used: * string使用案例 * system.out.println("abc"); * string cde = "cde"; * system.out.println("abc" + cde); * string c = "abc".substring(2,3); * string d = cde.substring(1, 2); * <p> * the class {@code string} includes methods for examining * individual characters of the sequence, for comparing strings, for * searching strings, for extracting substrings, and for creating a * copy of a string with all characters translated to uppercase or to * lowercase. case mapping is based on the unicode standard version * specified by the {@link java.lang.character character} class. * 这个string类包含了一些测评单个字符序列的方法,比如字符串比较,查找字符串, * 提取字符串,和拷贝一个字符串的大小写副本。 * 大小写映射的是基于character类支持的unicode的字符集标准版本。 * <p> * the java language provides special support for the string * concatenation operator (&nbsp;+&nbsp;), and for conversion of * other objects to strings. * java语言提供了对字符串的特殊支持,如:可以通过"+"号来进行字符串的拼接操作, * 为其他类提供了与字符串转换的操作 * string concatenation is implemented * through the {@code stringbuilder}(or {@code stringbuffer}) * class and its {@code append} method. * 字符串的+号拼接操作是通过stringbuilder或者stringbuffer类的append()方法 * 来实现的 * string conversions are implemented through the method * {@code tostring}, defined by {@code object} and * inherited by all classes in java. * 对象与字符串的转换操作是通过所有类的父类object中定义的tostring()方法来实现的 * for additional information on * string concatenation and conversion, see gosling, joy, and steele, * <i>the java language specification</i>. * * <p> unless otherwise noted, passing a <tt>null</tt> argument to a constructor * or method in this class will cause a {@link nullpointerexception} to be * thrown. * 除非有特殊说明,否则传一个null给string的构造方法或者put方法,会报空指针异常的 * <p>a {@code string} represents a string in the utf-16 format * in which <em>supplementary characters</em> are represented by <em>surrogate * pairs</em> (see the section <a href="character.html#unicode">unicode * character representations</a> in the {@code character} class for * more information). * 一个string 对象代表了一个utf-16编码语法组成的字符串 * index values refer to {@code char} code units, so a supplementary * character uses two positions in a {@code string}. * <p>the {@code string} class provides methods for dealing with * unicode code points (i.e., characters), in addition to those for * dealing with unicode code units (i.e., {@code char} values). * 索引值指向字符码单元,所以一个字符在一个字符串中使用两个位置, * string 类提供了一些方法区处理单个unicode编码,除了那些处理unicode代码单元。 * @since jdk1.0 */
以上便是string类注释的整个片段,后面剩下的就是作者、相关类、相关方法以及从jdk哪个版本开始有的。
string类定义public final class string implements java.io.serializable, comparable<string>, charsequence { .... }
类图
string类被final修饰,表示string不可以被继承。下面我们来说说string实现三个接口有什么用处:
实现serializable,可以被序列化实现comparable,可以用于比较大小(按顺序比较单个字符的ascii码)实现charsequence,表示是一个有序字符的序列,(因为string的本质是一个char类型数组)简单介绍final
修饰类:类不可被继承,也就是说,string类不可被继承了
修饰方法:把方法锁定,以访任何继承类修改它的涵义
修饰遍历:初始化后不可更改
重要成员 /** the value is used for character storage. */// 来用存储string内容的private final char value[];// 存储字符串哈希值,默认值为0private int hash; // default to 0// 实现序列化的标识private static final long serialversionuid = -6849794470754667710l;
char value[]被final修饰,说明value[]数组是不可变的。
构造方法/** * initializes a newly created {@code string} object so that it represents * an empty character sequence. note that use of this constructor is * unnecessary since strings are immutable. * 初始化新创建的string对象,时期表示空字符串序列。 * 注意:这个构造方法的用法是没必要的,因为字符串是不可变的 */public string() { this.value = "".value;}
无参构造方法中是将一个空字符串的value值赋给当前value。
/** * initializes a newly created {@code string} object so that it represents * the same sequence of characters as the argument; in other words, the * newly created string is a copy of the argument string. unless an * explicit copy of {@code original} is needed, use of this constructor is * unnecessary since strings are immutable. * 初始化创建的string对象,时期表示与参数相同的字符串序列。 * 换句话说:新创建的字符串是参数自粗糙的副本。 * 除非,如果需要original的显示副本,否则也是没有必要使用此构造方法的 * 因为字符串是不可变的 * @param original * a {@code string} */ public string(string original) { this.value = original.value; this.hash = original.hash; }//案例: string str=new string("abc");
把original的value赋给当前的value,并把original的hash赋给当前的hash。
/** * allocates a new {@code string} so that it represents the sequence of * characters currently contained in the character array argument. the * contents of the character array are copied; subsequent modification of * the character array does not affect the newly created string. * 分配一个新的{@code string},以便它表示字符数组参数中当前包含的字符。这个 * 复制字符数组的内容;随后修改字符数组不影响新创建的字符串。 * @param value * the initial value of the string */public string(char value[]) { //注:将传过来的char数组copy到value数组里 this.value = arrays.copyof(value, value.length);}//arrays类中的copyof方法public static char[] copyof(char[] original, int newlength) { //创建一个新的char数组 char[] copy = new char[newlength]; //把original数组中内容拷贝到新建的char数组中 system.arraycopy(original, 0, copy, 0, math.min(original.length, newlength)); //返回新建的char数组 return copy;}
使用arrays类的copyof方法,新建一个char数组,将original的内容放到新建的char数组中。
然后,把新建的char数组赋给当前的vlaue。
public string(stringbuffer buffer) { synchronized(buffer) { this.value = arrays.copyof(buffer.getvalue(), buffer.length()); }}
因为stringbuffer是线程安全类,所以,这里加了同步锁,保证线程安全。
public string(stringbuilder builder) { this.value = arrays.copyof(builder.getvalue(), builder.length());}
stringbuilder是非线程安全的,这里也就没有做线程安全处理,其他内容和前面一样。
注:很多时候我们不会这么去构造,因为stringbuilder跟stringbuffer有tostring方法如果不考虑线程安全,优先选择stringbuilder
这里就讲这么多构造方法,其他很复杂,也基本不用,所以,了解这些就够了。如果对其他感兴趣的,可以自行去研究研究。
常用方法分析前面的使用案例中,我们已经对string的大部分方法进行演示一波,这里我们就挑几个相对重要的方法进行深度解析。
hashcode方法hashcode()方法是在object类中定义的,string对其进行了重写。
public int hashcode() { int h = hash; if (h == 0 && value.length > 0) { char val[] = value; //hash算法,s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1] //使用{@codeint}算法,其中{@codes[i]}是<i> i</i>字符串的第个字符, //{@code n}是字符串,{@code^}表示指数运算。 for (int i = 0; i < value.length; i++) { h = 31 * h + val[i]; } hash = h; } return h;}
hashcode的一个具体实现,由于java体系中每个对象都可以转换成string,因此他们应该都是通过这个hash来实现的
接着,我们看看equals()方法;
equals()方法equals()方法也是object类中定义的,string类对其进行了重写。
public boolean equals(object anobject) { //首先会判断是否是同一个对象 if (this == anobject) { return true; } //判断是否为string类型 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) { //不相等,直接返回false if (v1[i] != v2[i]) return false; i++; } return true; } } return false;}
补充:==比较
==比较基本数据类型,比较的是值==比较引用数据类型,比较的是地址值
substring()方法substring方法在工作使用的也是相当的多,作用就是截取一段字符串。
public string substring(int beginindex) { if (beginindex < 0) { throw new stringindexoutofboundsexception(beginindex); } int sublen = value.length - beginindex; if (sublen < 0) { throw new stringindexoutofboundsexception(sublen); } //如果beginindex==0,返回的是当前对象, //否则这里是new的一个新对象,其实string中的很多函数都是这样的操作 return (beginindex == 0) ? this : new string(value, beginindex, sublen);}
intern()方法intern()方法是native修饰的方法,表示该方法为本地方法。
/* * when the intern method is invoked, if the pool already contains a * string equal to this {@code string} object as determined by * the {@link #equals(object)} method, then the string from the pool is * returned. otherwise, this {@code string} object is added to the * pool and a reference to this {@code string} object is returned. */public native string intern();
方法注释会有写到,意思就是调用方法时,如果常量池有当前string的值,就返回这个值,没有就加进去,返回这个值的引用。
案例如下
public class stringdemo { public static void main(string[] args) throws exception { string str1 = "a"; string str2 = "b"; string str3 = "ab"; string str4 = str1 + str2; string str5 = new string("ab"); system.out.println(str5 == str3);//堆内存比较字符串池 //intern如果常量池有当前string的值,就返回这个值,没有就加进去,返回这个值的引用 system.out.println(str5.intern() == str3);//引用的是同一个字符串池里的 system.out.println(str5.intern() == str4);//变量相加给一个新值,所以str4引用的是个新的 system.out.println(str4 == str3);//变量相加给一个新值,所以str4引用的是个新的 }}
运行结果
falsetruefalsefalse
length()方法获取字符串长度,实际上是获取字符数组长度 ,源码就非常简单了,没什么好说的。
public int length() { return value.length;}
isempty() 方法判断字符串是否为空,实际上是盼复字符数组长度是否为0 ,源码也是非常简单,没什么好说的。
public boolean isempty() { return value.length == 0;}
charat(int index) 方法根据索引参数获取字符 。
public char charat(int index) { //索引小于0或者索引大于字符数组长度,则抛出越界异常 if ((index < 0) || (index >= value.length)) { throw new stringindexoutofboundsexception(index); } //返回字符数组指定位置字符 return value[index];}
getbytes()方法获取字符串的字节数组,按照系统默认字符编码将字符串解码为字节数组 。
public byte[] getbytes() { return stringcoding.encode(value, 0, value.length);}
compareto()方法这个方法写的很巧妙,先从0开始判断字符大小。如果两个对象能比较字符的地方比较完了还相等,就直接返回自身长度减被比较对象长度,如果两个字符串长度相等,则返回的是0,巧妙地判断了三种情况。
public int compareto(string anotherstring) { //自身对象字符串长度len1 int len1 = value.length; //被比较对象字符串长度len2 int len2 = anotherstring.value.length; //取两个字符串长度的最小值lim int lim = math.min(len1, len2); char v1[] = value; char v2[] = anotherstring.value; int k = 0; //从value的第一个字符开始到最小长度lim处为止,如果字符不相等, //返回自身(对象不相等处字符-被比较对象不相等字符) while (k < lim) { char c1 = v1[k]; char c2 = v2[k]; if (c1 != c2) { return c1 - c2; } k++; } //如果前面都相等,则返回(自身长度-被比较对象长度) return len1 - len2;}
startswith()方法public boolean startswith(string prefix, int toffset) { char ta[] = value; int to = toffset; char pa[] = prefix.value; int po = 0; int pc = prefix.value.length; // note: toffset might be near -1>>>1. //如果起始地址小于0或者(起始地址+所比较对象长度)大于自身对象长度,返回假 if ((toffset < 0) || (toffset > value.length - pc)) { return false; } //从所比较对象的末尾开始比较 while (--pc >= 0) { if (ta[to++] != pa[po++]) { return false; } } return true;} public boolean startswith(string prefix) { return startswith(prefix, 0);} public boolean endswith(string suffix) { return startswith(suffix, value.length - suffix.value.length);}
起始比较和末尾比较都是比较经常用得到的方法,例如:在判断一个字符串是不是http协议的,或者初步判断一个文件是不是mp3文件,都可以采用这个方法进行比较。
concat()方法public string concat(string str) { int otherlen = str.length(); //如果被添加的字符串为空,返回对象本身 if (otherlen == 0) { return this; } int len = value.length; char buf[] = arrays.copyof(value, len + otherlen); str.getchars(buf, len); return new string(buf, true);}
concat方法也是经常用的方法之一,它先判断被添加字符串是否为空来决定要不要创建新的对象。
replace()方法public string replace(char oldchar, char newchar) { //新旧值先对比 if (oldchar != newchar) { int len = value.length; int i = -1; char[] val = value; //找到旧值最开始出现的位置 while (++i < len) { if (val[i] == oldchar) { break; } } //从那个位置开始,直到末尾,用新值代替出现的旧值 if (i < len) { char buf[] = new char[len]; for (int j = 0; j < i; j++) { buf[j] = val[j]; } while (i < len) { char c = val[i]; buf[i] = (c == oldchar) ? newchar : c; i++; } return new string(buf, true); } } return this;}
这个方法也有讨巧的地方,例如最开始先找出旧值出现的位置,这样节省了一部分对比的时间。replace(string oldstr,string newstr)方法通过正则表达式来判断。
trim()方法public string trim() { int len = value.length; int st = 0; char[] val = value; /* avoid getfield opcode */ //找到字符串前段没有空格的位置 while ((st < len) && (val[st] <= ' ')) { st++; } //找到字符串末尾没有空格的位置 while ((st < len) && (val[len - 1] <= ' ')) { len--; } //如果前后都没有出现空格,返回字符串本身 return ((st > 0) || (len < value.length)) ? substring(st, len) : this;}
trim方法就是将字符串中的空白字符串删掉。
valueof()方法public static string valueof(boolean b) { //如果b为true就返回"true"否则返回"false" return b ? "true" : "false";}public static string valueof(char c) { //创建data[]数组 并把c添加进去 char data[] = {c}; //创建一个新的string对象并进行返回 return new string(data, true); }public static string valueof(int i) { //调用integer对象的tostring()方法并进行返回 return integer.tostring(i); }//integer类中的tostring(i)方法public static string tostring(int i) { //是否为integer最小数,是直接返回 if (i == integer.min_value) return "-2147483648"; //这个i有多少位 int size = (i < 0) ? stringsize(-i) + 1 : stringsize(i); //创建一个char数组 char[] buf = new char[size]; //把i内容方法char数组中区 getchars(i, size, buf); //返回一个string对象 return new string(buf, true);}
split() 方法public string[] split(string regex) { return split(regex, 0);}//使用到了正则表达式public string[] split(string regex, int limit) { //.... //源码有点多了,反正就是里面使用到了正则表达式,进行切分 }
split() 方法用于把一个字符串分割成字符串数组,返回一个字符串数组返回的数组中的字串不包括 regex自身。可选的“limit”是一个整数,第一个方法中默认是0,允许各位指定要返回的最大数组的元素个数。
常见方法源码分析就这么多了,下面我们再回顾到使用场景中来,尤其是面试中。
string在面试中常见问题 如何比较字符串相同?在java中比较对象是否相同,通常有两种方法:
==equals方法注意==用于基本数据类型的比较和用于引用类型的比较的区别。
==比较基本数据类型,比较的是值
==比较引用数据类型,比较的是地址值
另外,string对equals方法进行了重写,所以比较字符串咱们还是要使用equals方法来比较。主要是string的equals方法里包含了==的判断(请看前面源码分析部分)。
案例
public class stringdemo { public static void main(string[] args) { string st1 = "abc"; string st2 = "abc"; system.out.println(st1 == st2); system.out.println(st1.equals(st2)); }}
输出
truetrue
string str=new string("abc");这行代码创建了几个对象?看下面这段代码:
string str1 = "abc"; // 在常量池中string str2 = new string("abc"); // 在堆上
关于这段代码,创建了几个对象,网上答案有多重,1个,2个还有3个的。下面我们就来聊聊到底是几个?
首先,我们需要明确的是;不管是str1还是str2,他们都是string类型的变量,不是对象,平时,可能我们会叫str2对象,那只是为了便于理解,本质上来说str2、str1都不是对象。
其次,string str="abc";的时候,字符串“abc”会被存储在字符串常量池中,只有1份,此时的赋值操作等于是创建0个或1个对象。如果常量池中已经存在了“abc”,那么不会再创建对象,直接将引用赋值给str1;如果常量池中没有“abc”,那么创建一个对象,并将引用赋值给str1。
那么,通过new string("abc");的形式又是如何呢?
答案是1个或2个。
当jvm遇到上述代码时,会先检索常量池中是否存在“abc”,如果不存在“abc”这个字符串,则会先在常量池中创建这个一个字符串。然后再执行new操作,会在堆内存中创建一个存储“abc”的string对象,对象的引用赋值给str2。此过程创建了2个对象。
当然,如果检索常量池时发现已经存在了对应的字符串,那么只会在堆内创建一个新的string对象,此过程只创建了1个对象。
最后,如果单独问string str=new string("abc");创建了几个对象,切记:常量池中是否存在"abc",存在,创建一个对象;不存在创建两个对象。
string 和 stringbuilder、stringbuffer 的区别线程安全性
string 中的对象是不可变的,也就可以理解为常量,线程安全。abstractstringbuilder 是 stringbuilder 与 stringbuffer 的公共父类,定义了一些字符串的基本操作,如 expandcapacity、append、insert、indexof 等公共方法。stringbuffer 对方法加了同步锁或者对调用的方法加了同步锁,所以是线程安全的。stringbuilder 并没有对方法进行加同步锁,所以是非线程安全的。
性能
每次对 string 类型进行改变的时候,都会生成一个新的 string 对象,然后将 指针指向新的 string 对象。stringbuffer 每次都会对 stringbuffer 对象本身进行操作,而不是生成新的对象并改变对象引用。相同情况下使用 stringbuilder 相比使用 stringbuffer 仅能获得 10%~15% 左右的性能提升,但却要冒多线程不安全的风险。
对于三者使用的总结:
操作少量的数据 ,推荐使用string单线程操作字符串缓冲区下操作大量数据,推荐使用 stringbuilder多线程操作字符串缓冲区下操作大量数据 ,推荐使用 stringbufferstring 和 jvm有什么关系?string 常见的创建方式有两种,new string() 的方式和直接赋值的方式,直接赋值的方式会先去字符串常量池中查找是否已经有此值,如果有则把引用地址直接指向此值,否则会先在常量池中创建,然后再把引用指向此值;而 new string() 的方式一定会先在堆上创建一个字符串对象,然后再去常量池中查询此字符串的值是否已经存在,如果不存在会先在常量池中创建此字符串,然后把引用的值指向此字符串。
jvm中的常量池
字面量—文本字符串,也就是我们举例中的 public string s = " abc "; 中的 "abc"。
用 final 修饰的成员变量,包括静态变量、实例变量和局部变量。
请看下面这段代码:
string s1 = new string("java");string s2 = s1.intern();string s3 = "java";system.out.println(s1 == s2); // falsesystem.out.println(s2 == s3); // true
它们在 jvm 存储的位置,如下图所示:
注意:jdk 1.7 之后把永生代换成的元空间,把字符串常量池从方法区移到了 java 堆上。
除此之外编译器还会对 string 字符串做一些优化,例如以下代码:
string s1 = "ja" + "va";string s2 = "java";system.out.println(s1 == s2);
虽然 s1 拼接了多个字符串,但对比的结果却是 true,我们使用反编译工具,看到的结果如下:
compiled from "stringexample.java"public class com.lagou.interview.stringexample { public com.lagou.interview.stringexample(); code: 0: aload_0 1: invokespecial #1 // method java/lang/object."<init>":()v 4: return linenumbertable: line 3: 0 public static void main(java.lang.string[]); code: 0: ldc #2 // string java 2: astore_1 3: ldc #2 // string java 5: astore_2 6: getstatic #3 // field java/lang/system.out:ljava/io/printstream; 9: aload_1 10: aload_2 11: if_acmpne 18 14: iconst_1 15: goto 19 18: iconst_0 19: invokevirtual #4 // method java/io/printstream.println:(z)v 22: return linenumbertable: line 5: 0 line 6: 3 line 7: 6 line 8: 22}
从编译代码 #2 可以看出,代码 "ja"+"va" 被直接编译成了 "java" ,因此 s1==s2 的结果才是 true,这就是编译器对字符串优化的结果。
如何判断两个字符串中含有几个相同字符将字符串转化成数组hashmap 方法字符串直接进行比较正则表达式hashset 方法string有没有长度限制?是多少?为什么?下面先看看length方法源码:
private final char value[];public int length() { return value.length;}
length()方法返回的是int类型,那可以得知string类型的长度肯定不能超过integer.max_value的。
答:首先字符串的内容是由一个字符数组 char[] 来存储的,由于数组的长度及索引是整数,且string类中返回字符串长度的方法length() 的返回值也是int ,所以通过查看java源码中的类integer我们可以看到integer的最大范围是2^31 -1,由于数组是从0开始的,所以**数组的最大长度可以使【0~2^31】**通过计算是大概4gb。
但是通过翻阅java虚拟机手册对class文件格式的定义以及常量池中对string类型的结构体定义我们可以知道对于索引定义了u2,就是无符号占2个字节,2个字节可以表示的最大范围是2^16 -1 = 65535。
但是由于jvm需要1个字节表示结束指令,所以这个范围就为65534了。超出这个范围在编译时期是会报错的,但是运行时拼接或者赋值的话范围是在整形的最大范围。
字符串对象能否用在switch表达式中?从jdk7开始的话,我们就可以在switch条件表达式中使用字符串了,也就是说7之前的版本是不可以的。
switch (str.tolowercase()) { case "tian": value = 1; break; case "jiang": value = 2; break;}
说说string中intern方法在jdk7之前的版本,调用这个方法的时候,会去常量池中查看是否已经存在这个常量了,如果已经存在,那么直接返回这个常量在常量池中的地址值,如果不存在,则在常量池中创建一个,并返回其地址值。
但是在jdk7以及之后的版本中,常量池从perm区搬到了heap区。intern检测到这个常量在常量池中不存在的时候,不会直接在常量池中创建该对象了,而是将堆中的这个对象的引用直接存到常量池中,减少内存开销。
下面的案例
public class interntest { public static void main(string[] args) { string str1 = new string("hello") + new string("world"); str1.intern(); string str2 = "helloworld"; system.out.println(str1 == str2);//true system.out.println(str1.intern() == str2);//true }}
好了,关于stirng类的分享就到此,欢迎找我探讨更多技术问题。
以上就是2w字 详解 string,yyds的详细内容。
其它类似信息

推荐信息