JAVA中的string---(转载自 ---- 平凡希)
来源:互联网 发布:淘宝宝贝标题敏感 编辑:程序博客网 时间:2024/05/16 11:03
String 类
1
public final class String implements 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; /** Cache the hash code for the string */ private int hash; // Default to 0 /** use serialVersionUID from JDK 1.0.2 for interoperability */ private static final long serialVersionUID = -6849794470754667710L; ........1)string类是final类,final类不能被继承,并且他的成员方法都默认为final方法在java中,被final修饰的类是不允许被继承的,并且该类中的其他成员都是默认为final方法
2)Stringl类是通过char数组来保存自字符串的。
public String substring(int beginIndex, int endIndex) { if (beginIndex < 0) { throw new StringIndexOutOfBoundsException(beginIndex); } if (endIndex > count) { throw new StringIndexOutOfBoundsException(endIndex); } if (beginIndex > endIndex) { throw new StringIndexOutOfBoundsException(endIndex - beginIndex); } return ((beginIndex == 0) && (endIndex == count)) ? this : new String(offset + beginIndex, endIndex - beginIndex, value);}public String concat(String str) { int otherLen = str.length(); if (otherLen == 0) { return this; } char buf[] = new char[count + otherLen]; getChars(0, count, buf, 0); str.getChars(0, otherLen, buf, count); return new String(0, count + otherLen, buf);}public String replace(char oldChar, char newChar) { if (oldChar != newChar) { int len = count; int i = -1; char[] val = value; /* avoid getfield opcode */ int off = offset; /* avoid getfield opcode */ while (++i < len) { if (val[off + i] == oldChar) { break; } } if (i < len) { char buf[] = new char[len]; for (int j = 0 ; j < i ; j++) { buf[j] = val[off+j]; } while (i < len) { char c = val[off + i]; buf[i] = (c == oldChar) ? newChar : c; i++; } return new String(0, len, buf); } } return this;}
sub() contact() replace()操作都不是在原有的字符串上进行的,而是重新生成了一个新的字符串对象,进行这些操作后,原始的字符串并没有被改变,。String对象一旦被创建就是固定不变了,。对String对象的任何改变都不会影响到原对象,相关的任何操都会生成新的对象
二字符串常量池
当创建字符串常量的时候,jvm会首先检查字符串常量池,如果该字符串已经存在常量池中,那么就直接返回常量池的实例引用,如果字符串不存在常量池中,就会实例化该字符串并将其放到常量池中,由于String字符串的不可变性 我们可以十分的肯定常量池中一定不存在两个相同的字符串
java中的常量池分为两种形态:静态常量池和运行时常量池l
静态常量池就是: *.class中的常量池,class文件中的常量池包括了字符串字面量,类,方法的信息,占用了class文件的绝大部分空间,
运行时常量池: jvm虚拟机在完成类装载操作后,将class文件中的常量载入到内存中,并保存在方法区中,经常说的常量池指的就是运行时常量池
String a=“abc”;
String b=“abc”;
a和b指向的是同一对象
String c=new String("abc");
在这里new 会产生一个新的对象“abc”这个对象是放在堆中的。 此时产生了两个新的对象,一个放在栈中的c
堆中的“abc”
例子:
/** * 采用字面值的方式赋值 */public void test1(){ String str1="aaa"; String str2="aaa"; System.out.println("===========test1============"); System.out.println(str1==str2);//true 可以看出str1跟str2是指向同一个对象 }
/** * 采用new关键字方式赋值 */public void test1(){ String str3=new String("aaa"); String str4=new String("aaa"); System.out.println("===========test2============"); System.out.println(str3==str4);//false 可以看出str3跟str4分别创建了一个新的对象 采用new关键字新建一个字符串对象时,JVM首先在字符串池中查找有没有"aaa"这个字符串对象,如果有,则不在池中再去创建"aaa"这个对象了,直接在堆中创建一个"aaa"字符串对象,然后将堆中的这个"aaa"对象的地址返回赋给引用str3,这样,str3就指向了堆中创建的这个"aaa"字符串对象;如果没有,则首先在字符串池中创建一个"aaa"字符串对象,然后再在堆中创建一个"aaa"字符串对象,然后将堆中这个"aaa"字符串对象的地址返回赋给str3引用,这样,str3指向了堆中创建的这个"aaa"字符串对象。当执行String str4=new String("aaa")时, 因为采用new关键字创建对象时,每次new出来的都是一个新的对象,也即是说引用str3和str4指向的是两个不同的对象,因此语句System.out.println(str3 == str4)输出:false。}
分析:
jvm现在字符串中查找有没有aaa对象,如果有不在池中再创建新的,直接在堆中创建一个aaa字符串对象,然后将这个aaa对象的地址赋值给引用str3,如果没有则首先在字符串池中创建一个aaa对象,再在重复上述操作,,最后将这个新创建的aaa对象的地址赋值引用str4, 由于在使用关键字new对象时,每次new的对象都是一个新的对象,每个对象都自己的唯一地址,所以str3==str4 false
public void test3(){ String s0="helloworld"; String s1="helloworld"; String s2="hello"+"world"; System.out.println("===========test3============"); System.out.println(s0==s1); //true 可以看出s0跟s1是指向同一个对象 System.out.println(s0==s2); //true 可以看出s0跟s2是指向同一个对象 }
/** * 编译期无法确定 */public void test4(){ String s0="helloworld"; String s1=new String("helloworld"); String s2="hello" + new String("world"); System.out.println("===========test4============"); System.out.println( s0==s1 ); //false System.out.println( s0==s2 ); //false System.out.println( s1==s2 ); //false}
0 0
- JAVA中的string---(转载自 ---- 平凡希)
- JAVA中的String(引用自51CTO文章)
- Java中的String、StringBuilder以及StringBuffer(转载)
- [转载]java.lang.String中的…
- [转载]java.lang.String中的…
- JAVA面试题解惑系列(六)——字符串(String)杂谈 转载自javaeye
- 转载c++中的string
- Java中的字符串常量池详解--转载自技术小黑屋
- java中String的比较(转载)
- JAVA String (转载)
- java中的输入输出(转载)
- java中的国际化(转载)
- java中的输入输出(转载)
- java中的注解(转载)
- 平凡中的哲理
- omap3 linux中的中断(转载自阿炳哥)
- (十六)java中的String
- Java中的set去重复(基础类型,与对象)转载自http://blog.csdn.net/miqi770/article/details/8998517
- 列表——更改的是谁
- CrateDB记录_2
- ComBatTelRunner别作怪好吗?!
- eval_api_类中类_map
- SQLServer 扩展事件(Extended Events)
- JAVA中的string---(转载自 ---- 平凡希)
- 关于C++ Builder中TChart的一些用法[转载]
- 8位数码管动态扫描显示
- springmvc-学习总结-非注解式处理器和映射器
- Gazebo 详细介绍
- leetcode_middle_65_289. Game of Life
- Error: (unix time) try if you are using GNU date
- C++中的string
- POJ 3617 (贪心)