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> 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[]不可变
|
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); } |

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是直接返回的countpublic int length() { return count;}//isEmpty是直接判断count是否等于0public boolean isEmpty() { return count == 0;}//charAt是取char[] value中相应索引的值,count不在范围抛出数组索引越界异常public char charAt(int index) { if ((index < 0) || (index >= count)) { throw new StringIndexOutOfBoundsException(index); } return value[index + offset];}//substringpublic String substring(int beginIndex) { return substring(beginIndex, count);}//开始索引小于0,大于当前对象总长度,或者结束索引大于总长度,都抛出越界异常//否则,开始等于0,结束等于总长度,则重新new一个string对象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);} //常用的equals方法(重写了父类的equals方法),是先比较对象是否为同一个对象。然后比较其内容是否都相等。public boolean equals(Object anObject) {if (this == anObject) {//首先比较当前对象和传来的对象是不是同一个对象 return true;}if (anObject instanceof String) {//如果传来的对象是String的一个实例 String anotherString = (String)anObject; int n = count; if (n == anotherString.count) { char v1[] = value; char v2[] = anotherString.value; int i = offset; int j = anotherString.offset; while (n-- != 0) {//数组每个元素对应比较 if (v1[i++] != v2[j++])//如果有一个元素不相等,返回false return false; } return true; }}return false;} |
length、isEmpty、charAt、substring等等的就不做测试了,主要测试equals方法
|
1
2
3
|
String str = new String("abcd");String str2 = str;System.out.println(str.equals(str2));//返回结果:true,因为str和str2都指向了同一个new String("abcd");对象 |
|
1
2
3
4
5
|
String str = new String("abcd");String str2 = new String("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属性public int length()//是否空:直接return count==0public boolean isEmpty()//根据指定索引获取字符:如果index<0或者index>count都抛出*StringIndexOutOfBoundsException,否则直接返回char数组中对应的值public char charAt(int index)//如果传来的和调用的是同一个对象,返回true//然后遍历比较,如果传来对象的其中一个值跟调用的对象不等,就返回falsepublic boolean equals(Object anObject)//substring先判断beginIndex < 0,beginIndex < 0,beginIndex >endIndex都会抛出异常//否则(beginIndex == 0) && (endIndex == count)成立的话直接返回this,不成立则new Stringpublic String substring(int beginIndex, int endIndex) |
5、值得一提的是,String的toString方法是直接返回的this
6、String对象个数问题:
|
1
2
3
4
5
6
7
8
|
String str="a";//总共一个对象aString str2=new String("a");//总共两个对象:一个new String,一个aString str="a";//这里一个对象for(int i=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 ...
随机推荐
- How to say "no"?
How to say "no"?7招教你如何拒绝别人 Do you have a hard time saying no to others? Do you say “y ...
- Android核心分析之二十一Android应用框架之AndroidApplication
Android Application Android提供给开发程序员的概念空间中Application只是一个松散的表征概念,没有多少实质上的表征.在Android实际空间中看不到实际意义上的应用程 ...
- eclipse:failed to create the java virtual machine
今天eclipse出现failed to create the java virtual machine无法启动,在网上找了解决办法如下: 找到eclipse目录下的eclipse.ini,可以看到如 ...
- TestDirector域或工程用户的管理
一.添加用户 单击界面上的"Users"按钮,进入如下图: 我们可以添加新用户,删除用户,导入用户,修改用户密码,用户的详细信息. 1.单击"New"按钮为域或 ...
- Android:Intel Atom x86模拟器的安装与使用
1.下载硬件加速执行管理器haxm-windows_r05.zip,找到系统对应版本,并安装 2.下载安卓x86系统映象sysimg_x86-19_r01.zip,不区分系统环境,解压得到x86文件夹 ...
- C语言:几种字符输入函数的区别
几种字符输入函数的区别: 1.getche()函数:用于从键盘读入一个字符并显示,然后直接执行下一条语 句. 2.getch()函数:用于从键盘中读入一个字符,但不显示在屏幕上, 然后 ...
- Data Flow ->> CDC Control Task, CDC Source, CDC Splitter
CDC Control Task可以从控制CDC数据同步,比如初始化加载.LSN范围的管理.它可以代替另一种做法,就是通过调用一批CDC函数来完成同样的事情.从SSIS的角度来完成,事情编程简单,和另 ...
- Zn离子参数
Generating Topology and Coordinates Files Using xLeap (AmberTools V1.5) Parameter and example files: ...
- libevent系列文章
Libevent 2 提供了 bufferevent 接口,简化了编程的难度,bufferevent 实际上是对底层事件核心的封装,因此学习 bufferevent 的实现是研究 Libevent 底 ...
- 事务回滚后,自增ID仍然增加
回滚后,自增ID仍然增加. 比如当前ID是7,插入一条数据后,又回滚了.然后你再插入一条数据,此时插入成功,这时候你的ID不是8,而是9.因为虽然你之前插入回滚,但是ID还是自增了. 如果你认为自增I ...