Java常用类:String
一、介绍
String:不可变的Unicode字符序列
Java没有内置的字符串类型,而是在标准的Java类库中提供了一个预定义的类String.每个用双引号括起来的字符串就是String类的一个实例.
当使用+,实际是产生新的对象
(使用循环拼接字符串时,一定要用StringBuilder或者StringBuffer其中的一个)
二、查看String的一小部分源码
1
2
3
4
5
6
 
The <code>String</code> class represents character strings. All
 * string literals in Java programs, such as <code>"abc"</code>, are
 * implemented as instances of this class.
 * <p>
 * Strings are constant; their values cannot be changed after they
 * are created.
 
 
String类存在于java.lang包中。上述大概意思:String是一个字符串,String的字面量也是String的一个实例。String它的值在创建以后是不能变的.....
1
2
3
4
5
 
public final class String
    implements java.io.Serializable, Comparable<String>, CharSequence
{
    /** The value is used for character storage. */
    private final char value[];
 
 
可以看出外部类是不能直接访问到这个value属性的,这个char value[]不可变
以下是String的部分构造器:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 
//默认构造器,当写下了String str =new String();的时候,其实内部是给了一个长度为0的char数组给value
    public String() {
      ...省略部分....
      this.value = new char[0];
    }
    //可以传递一个字符串,则直接把传递的字符串赋给了value
    public String(String original) {
      ...省略部分....
      this.value = v;
    }
    /**
    *也可以传递一个字符数组,例如:可以像下面这样
    *char[] c = {'a','b','c'};
    *String str = new String(c);
    */
    public String(char value[]) {
    int size = value.length;
    this.offset = 0;
    this.count = size;
    this.value = Arrays.copyOf(value, size);
    }
 
 

| 1 2 3 4 5 6 | The <code>String</code> classrepresents character strings. All * string literals in Java programs, such as <code>"abc"</code>, are * implemented as instances of thisclass. * <p> * Strings are constant; their values cannot be changed after they * are created. | 
String类存在于java.lang包中。上述大概意思:String是一个字符串,String的字面量也是String的一个实例。String它的值在创建以后是不能变的.....
| 1 2 3 4 5 | publicfinalclassString    implementsjava.io.Serializable, Comparable<String>, CharSequence{    /** The value is used for character storage. */    privatefinalcharvalue[]; | 
可以看出外部类是不能直接访问到这个value属性的,这个char value[]不可变
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | //默认构造器,当写下了String str =new String();的时候,其实内部是给了一个长度为0的char数组给value    publicString() {      ...省略部分....      this.value = newchar[0];    }    //可以传递一个字符串,则直接把传递的字符串赋给了value    publicString(String original) {      ...省略部分....      this.value = v;    }    /**    *也可以传递一个字符数组,例如:可以像下面这样    *char[] c = {'a','b','c'};    *String str = new String(c);    */    publicString(charvalue[]) {    intsize = value.length;    this.offset = 0;    this.count = size;    this.value = Arrays.copyOf(value, size);    } | 

String重载了很多构造器,不一一查看了。
String的一些常用方法源码:还有很多其他的,不一一介绍了,使用起来都很简单。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | //length是直接返回的countpublicintlength() {   returncount;}//isEmpty是直接判断count是否等于0publicbooleanisEmpty() {   returncount == 0;}//charAt是取char[] value中相应索引的值,count不在范围抛出数组索引越界异常publiccharcharAt(intindex) {   if((index < 0) || (index >= count)) {       thrownewStringIndexOutOfBoundsException(index);   }   returnvalue[index + offset];}//substringpublicString substring(intbeginIndex) {    returnsubstring(beginIndex, count);}//开始索引小于0,大于当前对象总长度,或者结束索引大于总长度,都抛出越界异常//否则,开始等于0,结束等于总长度,则重新new一个string对象publicString substring(intbeginIndex, intendIndex) {    if(beginIndex < 0) {        thrownewStringIndexOutOfBoundsException(beginIndex);    }    if(endIndex > count) {        thrownewStringIndexOutOfBoundsException(endIndex);    }    if(beginIndex > endIndex) {        thrownewStringIndexOutOfBoundsException(endIndex - beginIndex);    }    return((beginIndex == 0) && (endIndex == count)) ? this:        newString(offset + beginIndex, endIndex - beginIndex, value);}    //常用的equals方法(重写了父类的equals方法),是先比较对象是否为同一个对象。然后比较其内容是否都相等。publicbooleanequals(Object anObject) {if(this== anObject) {//首先比较当前对象和传来的对象是不是同一个对象   returntrue;}if(anObject instanceofString) {//如果传来的对象是String的一个实例   String anotherString = (String)anObject;   intn = count;   if(n == anotherString.count) {   charv1[] = value;   charv2[] = anotherString.value;   inti = offset;   intj = anotherString.offset;   while(n-- != 0) {//数组每个元素对应比较       if(v1[i++] != v2[j++])//如果有一个元素不相等,返回false       returnfalse;   }   returntrue;   }}returnfalse;} | 
length、isEmpty、charAt、substring等等的就不做测试了,主要测试equals方法
| 1 2 3 | String str = newString("abcd");String str2 = str;System.out.println(str.equals(str2));//返回结果:true,因为str和str2都指向了同一个new String("abcd");对象 | 
| 1 2 3 4 5 | String str = newString("abcd");String str2 = newString("abcd");System.out.println(str.equals(str2));//返回结果:true,此时str和str2指向的不是同一个对象,但是内容相等//但是如果直接==的话就是falseSystem.out.println(str==str2);//因为不是同一个对象 | 
三、总结
1、String是由final修饰的class,不可修改。内部的属性都是private的,不给外部访问。
2、内部的主要操作就是操作的char value[]数组,这个value其实是可以修改的,但是jdk中没有提供接口供你修改。
3、字符串比较需要使用equals方法,两个对象比较使用==
4、部分常用方法,还有一些concat、replace、replaceFirst、replaceAll、split、toLowerCase、toUpperCase、trim、valueOf等等的方法
| 1 2 3 4 5 6 7 8 9 10 11 12 | //长度:直接返回count属性publicintlength()//是否空:直接return count==0publicbooleanisEmpty()//根据指定索引获取字符:如果index<0或者index>count都抛出*StringIndexOutOfBoundsException,否则直接返回char数组中对应的值publiccharcharAt(intindex)//如果传来的和调用的是同一个对象,返回true//然后遍历比较,如果传来对象的其中一个值跟调用的对象不等,就返回falsepublicbooleanequals(Object anObject)//substring先判断beginIndex < 0,beginIndex < 0,beginIndex >endIndex都会抛出异常//否则(beginIndex == 0) && (endIndex == count)成立的话直接返回this,不成立则new StringpublicString substring(intbeginIndex, intendIndex) | 
5、值得一提的是,String的toString方法是直接返回的this
6、String对象个数问题:
| 1 2 3 4 5 6 7 8 | String str="a";//总共一个对象aString str2=newString("a");//总共两个对象:一个new String,一个aString str="a";//这里一个对象for(inti=0;i<10;i++){   str = str + i;//这里每循环一次都会创建一个对象}//以上执行完会出现11个对象 | 
Java常用类:String的更多相关文章
- Java 常用类String类、StringBuffer类
		常用类 String类.StringBuffer类 String代表不可变的字符序列 "xxxxxxx"为该类的对象 举例(1) public class Test { publi ... 
- 深入理解Java常用类----String
		Java中字符串的操作可谓是最常见的操作了,String这个类它封装了有关字符串操作的大部分方法,从构建一个字符串对象到对字符串的各种操作都封装在该类中,本篇我们通过阅读String类的源码 ... 
- 深入理解Java常用类----String(二)
		上篇介绍了String类的构造器,获取内部属性等方法,最后留下了最常用的局部操作函数没有介绍,本篇将接着上篇内容,从这些最常见的函数的操作说起,看看我们日常经常使用的这些方法的内部是怎么实现的.第一个 ... 
- java常用类-String类
		* 字符串:就是由多个字符组成的一串数据.也可以看成是一个字符数组. * 通过查看API,我们可以知道 * A:字符串字面值"abc"也可以看成是一个字符串对象. * B:字符串是 ... 
- java常用类String
		String: String类: 代表字符串 是一个final类,代表不可变的字符序列 字符串是常量,用双引号引起来表示.值在创建后不可更改 String对象的字符内容是存储在一个字符数组Value[ ... 
- Java常用类String的面试题汇总
		比较两个字符串时使用"=="还是equals()方法? 当然是equals方法."=="测试的是两个对象的引用是否相同,而equals()比较的是两个字符串的值 ... 
- Java常用API(String类)
		Java常用API(String类) 概述: java.lang.String 类代表字符串.Java程序中所有的字符串文字(例如 "abc" )都可以被看作是实现此类的实例 1. ... 
- Java常用类:包装类,String,日期类,Math,File,枚举类
		Java常用类:包装类,String,日期类,Math,File,枚举类 
- Java基础 —— Java常用类
		Java常用类: java.lang包: java.lang.Object类: hashcode()方法:返回一段整型的哈希码,代表地址. toString()方法:返回父类名+"@&quo ... 
- Java常用类学习笔记总结
		Java常用类 java.lang.String类的使用 1.概述 String:字符串,使用一对""引起来表示. 1.String声明为final的,不可被继承 2.String ... 
随机推荐
- Windows 代码实现关机(直接黑屏)
			整理资料的时候发现的以前的代码,本机Win7 x64 Sp1 运行直接关机,黑屏.就是利用RtlAdjustPrivilege函数提权,代码中的注释写的很详细了.用的VS2010写的,直接编译成x64 ... 
- sql server 2008 评估期已过期解决办法
			开始-->所有程序-->Microsoft SQL Server 2008-->配置工具-->SQL Server 安装中心-->维护-->版本升级,接着按照提示一 ... 
- http://www.cnblogs.com/flyoung2008/archive/2013/08/11/3251148.html
			http://www.cnblogs.com/flyoung2008/archive/2013/08/11/3251148.html 
- 关于playmaker play animation出现警告 The AnimationClip 'xxx' used by the Animati ...
			转载自网络: 出现这个提示: The AnimationClip 'xxx' used by the Animation component 'xxx' must be marked as Legac ... 
- android学习笔记二:Intent
			1.Intent作用 协助完成各个组建间的通信.如activity间.启动service.Broadcast. 2.Intent构成 1.Componet name:要启动的目的组建. 2.Actio ... 
- SQL Server 数据库初始化准备脚本
			通常我们在项目部署前都会写一份数据库初始化脚本.由于数据库外键的限制,我们需要按照数据引用顺序添加初始记录,这个整理过程相当麻烦. 因此写了以下脚本,原理是先去掉所有外键,然后执行一次清空,然后添加数 ... 
- JSON格式化 JSON美化 输出到html
			{"promotion_details":{"promotion_detail":[{"discount_fee":"22.20& ... 
- Codeforces Round #215 (Div. 1) B
			出来冒个泡 由于数比较大 开了map计数 然后边走边删边加 勉强可过 #include <iostream> #include<cstdio> #include<cs ... 
- 读取Properties文件工具类
			import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java ... 
- JSAPI微信支付返回错误:fail_no permission to execute
			问题描述 fail_no permission to execute 一定是授权目录出问题了,因为没有权限. 开发环境及可能造成的原因 这次的微信开发是用的Mvc4,支付的封装代码不会有问题(用过很多 ... 
