java的String学习加JDK源码的学习
1.1.——;源码;就是说的JDK源码,就是JDK开发人员写到代码,写了这个版本的JDK中该类是怎么来的,其中包括什么方法,什么接口,以及它具体实现的方法。我这是JDK7.6;
1.2——;学会看源码,看源码时就是看自己熟悉的,不然你是看不下去的,不然效率会低得吓人的。
1.3;——怎么打开源码。按住Ctrl 然后点击你要看的类,就会进入这个类的源码。
注意这里的outline表示大纲,这个类有什么方法的一个目录,可以直接点击然后就会进入这个方法介绍那个地方。
2;关于String类,不可变字符序列;
1
2
3
4
5
6
7
8
9
|
看着部分源码,String类最开头部分,关于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类代表字符类,
*
//literls字面量,也就是跟“abs”,双引号括起来的部分,也可以叫直接量。
*
//implement
实现,instance 实列,
*
</code></code>
|
<code><code>
public
final
class
String
implements
java.io.Serializable, Comparable<string>, CharSequence
//String是被final修饰的,因此它是不可变的,并且不能被继承,
并且它实现了许多接口。(具体什么不用管,(JDK里提供的接口)也可以按住Ctrl点击即可。
接口中就定义了许多,方法。并且只有方法的什么没有方法的实例;(接口的概念)</string></code></code>
<code><code>
public
String() {
this
.value
=
new
char
[
0
];
}
//这个就是String类的构造器。
当String
str1 =
new
String();
//就是声请长度为0的字符数组;
—————————————————————————————————————————————————————————————
public
String(String original) {
this
.value
= original.value;
this
.hash
= original.hash;
}
//这个构造方法就是可以传进字符串进来。
String
str2 =
new
String(
"sssss"
);
//可以传入字符串进来。
-------------------------------------------------------------
public
String(
char
value[]) {
this
.value
= Arrays.copyOf(value, value.length);
}
//构造方法,传递一个数组,但是这里有数组的copy。需要把这个数组Arrany.copyOf到我们这个类的数组里面。
char
[]
c = {
'a'
,
'v'
};
String
str3 =
new
String(c);
//传递一个数组进来,在这个构造方法里面调用了Array.copyOF(),
//Array类中public
static char[] copyOf(char[] original, int newLength)进行赋值
}</code></code>
<code><code>
public
int
length() {
return
value.length;
}
//length()这个方法就是把这个数组的长度返回来了。</code></code>
|
1
2
3
4
|
<code><code>
public
boolean
isEmpty() {
return
value.length == 0 ;
}
//如果长度为0那么就是空嘛。</code></code>
|
1
2
3
4
5
6
7
8
9
10
|
<code><code> public
char
charAt( int
index) {
if
((index < 0 )
|| (index >= value.length)) {
throw
new
StringIndexOutOfBoundsException(index);
}
return
value[index];
}
//
charAt,就是返回索引指向的字符,从0开始;
//注意中间还穿插了一个如果索引不在范围之内就会抛出一个异常,
String
strr = "abcder" ;
System.out.println(strr.charAt( 2 )); //返回的是c</code></code>
|
关于public boolean equals(Object anObject) 方法
String
strr1 = new String("sjjdsf"); String strr2 = strr1; System.out.println(strr2.equals(strr2)); //返回tur,因为指向同一个对象。直接在第一部分就是返回ture了。 -------------------------------------------------------- String strr1 = new String("sjjdsf"); String strr2 = new String ("sjjdsf");
System.out.println(strr1.equals(strr2));//比较的是内容是否相等。 System.out.println(strr1==strr2);//比较的是他们引用是否相等 //顺便回忆一下,equal方法默认方法是与==一致的,因此一般都是需要重写的。 String str = "absd"; String str1 = "absd"; System.out.println(str.equals(str1));//肯定是ture //这个应该也是true;”abcd“双引号括起来的字符串是表示在常量池中。
来个内存图的分析;
1
2
3
4
|
<code><code>String
str = new
String( "abcd" );
String
str2 = new
String( "abcd" );
String
str3 = "def" ;
String
str4 = "def" ;</code></code>
|
<code><code>indexOf()方法
Returns
the index within this
string of the first occurrence of
*
the specified character.
返回索引,在这个字符串中第一次出现的这个字符。
public
int
indexOf( int
ch) { //调用重载方法
return
indexOf(ch, 0 ); //0;表示这个字符串,从最开头开始找。
}
重载方法;
public
int
indexOf( int
ch, int
fromIndex) { //formIndex开始找,
String
str = new
String( "abcd" );
System.out.println(str.indexOf( 'b' )); //返回索引位置,并且注意索引都是从0开始的;
System.out.println(str.indexOf( 'e' )); //如果没有找到那么就返回-1;
lastIndextOF() //从后面开始找。
System.out.println( "Abcdb" .indexOf( 'd' )); //3
System.out.println( "Abcdb" .lastIndexOf( 'b' )); //4
</code></code>
|
1
2
3
4
|
<code><code> public
String substring( int
beginIndex) //截取字符串的方法。返回时字符串对象;
*
Returns a new
string that is a substring of this
string. The
*
substring begins with the character at the specified index and
*
extends
to the end of this
string. </code></code>
|
//返回一个子字符串对象,并且子字符串的对象从传人的索引这个值所值的字符开始截取,一直到字符串结尾。 //如果传人的值为负数,则会抛出异常。 public String substring(int beginIndex, int endIndex) //一个重载方法; //表示的是从beginInder到endIndex的截取。 String str = new String("abcd"); String str1 = str.substring(2); System.out.println(str1);//返回cd
1
2
3
4
5
6
7
8
9
10
11
|
<code><code>
* Returns a new
string resulting from replacing all occurrences of
*
<code>oldChar in this
string with <code>newChar.
//返回一个新的字符串对象,从把字符串中的newchar用oldchar代替的新字符串
If
the character <code>oldChar does not occur in the
*
character sequence represented by this
<code>String object,
*
then a reference to this
<code>String object is returned.
//如果这个老的字符没有出现在字符串里面,那么就会返回原字符串对象的引用。、return
this。
String
str = new
String( "abcd" );
String
str1 = str.replace( 'a' ,
'*' );
System.out.println(str1); //返回*bcd
</code></code></code></code></code></code></code>
|
<code><code><code><code><code>
public
String[] split(String regex)
//对字符串进行切割,返回一个数组。
String
str =
"abcd,abc,dmhjkl,"
;
String[]
strArray = str.split(
","
);
//按逗号进行切割。
for
(
int
i=
0
;i<strarray.length;i++){
pre=
""
><pre
class
=
"brush:java;"
>
public
String trim()
//去除首尾空格。
String
str =
"
assdf aw da f "
;
String
str1 = str.trim();
//去除首尾空格;
System.out.println(str1);
//返回assdf
aw da f</pre>
<pre
class
=
"brush:java;"
>
public
char
[]
toCharArray()
//就是将String对象中的数组返回,返回的是一个新的数组,这个数组就可以修改了。</pre>
<pre
class
=
"brush:java;"
>
public
boolean
equalsIgnoreCase(String anotherString)
//忽略大小写进行比较
Compares
this
{
@code
String} to another {
@code
String}, ignoring
case
*
considerations.
System.out.println(
"ASs"
.equalsIgnoreCase(
"asS"
));
//返回true</pre>
<pre
class
=
"brush:java;"
>
public
boolean
startsWith(String prefix)
//表示字符串是否以这个开始。
public
boolean
endsWith(String suffix)
//表示字符串是否以这个结尾。
System.out.println(
"Abcdb"
.startsWith(
"Ab"
));
//ture
System.out.println(
"Abcdb"
.endsWith(
"db"
));
//true;</pre>
<pre
class
=
"brush:java;"
>
public
String toLowerCase()
//把字符串中的字母全变成小写
public
String toUpperCase()
//把字符串的字母全变为大写;
System.out.println(
"ADs"
.toLowerCase());
//ads
System.out.println(
"ADS"
.toUpperCase());
//ADS</pre>
<p><strong>常见的拼字符串代码,但是并不好,很浪费空间。因为要声明很多对象**</strong></p>
<pre
class
=
"brush:java;"
>String
gh =
"g"
;
//这句话就是有两个对象,“g”也单独算一个对象/
for
(
int
i =
0
;
i <
10
;
i++){
//这里会创建9个对象。一起就是11个对象(常做面试题)
gh
+= i;
//相当于,gh
= gh + i;
}
System.out.println(gh);
//g0123456789
//要注意,这里的gh是变化的,为什么;因为String不可变,是指的”g“里面的,
//如果改为final
String gh = "g"; 那就的确是不可变的但是String里面的因此gh是可变的。</pre>
<p><img
alt=
"这里写图片描述"
src=
"/uploadfile/Collfiles/20161101/201611010923291052.png"
title=
"\"
style="
width:
630px; height:
476
.907px;"></p>
<p>再强调一下,
private
final
char
value[];是引用的值不可变,但是value里面的元素是可以变的,但是String类是
private
并且也没有暴露可以修改的接口,因此value里面的值还是不能修改。(封装)</p>
<p>对于源码的学习,要适当,看自己知道的就ok,了解这个类大概有什么方法。</p>
<p><strong>附上上方各种方法的代码</strong></p>
<pre
class
=
"brush:java;"
>
package
textString;
import
javax.swing.plaf.synth.SynthSeparatorUI;
public
class
Stringtext {
public
static
void
main(String[] args) {
//
String str = "abcd";//按住Ctrl点击String,进入String类的源码;
//
String str1 = new String();//就是声请长度为1的字符数组;
//
String str2 = new String("sssss");//可以传入字符串进来。
//
char[] c = {'a', 'v'};
//
String str3 = new String(c);//传递一个数组进来,在这个构造方法里面调用了Array.copyOF();
//
//public static char[] copyOf(char[] original, int newLength)进行赋值/
//返回这个索引位置的字符
//
String strr = "abcder";
//
System.out.println(strr.charAt(2));//返回的是c
//
String strr1 = new String("sjjdsf");
//
String strr2 = strr1;
//
System.out.println(strr2.equals(strr2));//返回tur,因为指向同一个对象。直接在第一部分就是返回ture了。
//
String strr1 = new String("sjjdsf");
//
String strr2 = new String ("sjjdsf");
//
System.out.println(strr1.equals(strr2));//比较的是内容是否相等。
//
System.out.println(strr1==strr2);//比较的是他们引用是否相等
//equals()判断
//
String str = "absd";
//
String str1 = "absd";
//
System.out.println(str.equals(str1));//肯定是ture
//
System.out.println(str==str1);//这个应该也是true;”abcd“双引号括起来的字符串是表示在常量池中。
//
//关于不同声明的内存图
//
String str = new String("abcd");
//
String str2 = new String("abcd");
//
String str3 = "def";
//
String str4 = "def";
//从字符串前面开始找。
//
String str = new String("abcd");
//
System.out.println(str.indexOf('b'));//返回索引位置,并且注意索引都是从0开始的;
//
System.out.println(str.indexOf('e'));//如果没有找到那么就返回-1;
//字符串截取
//
String str = new String("abcd");
//
String str1 = str.substring(2);
//
System.out.println(str1);//返回cd
//进行替换。
//
String str = new String("abcd");
//
String str1 = str.replace('a', '*');
//
System.out.println(str1);//返回*bcd
//按照某个东西对字符串进行切割返回字符串数组
//
String str = "abcd,abc,dmhjkl,";
//
String[] strArray = str.split(",");//按逗号进行切割。
//
for(int i=0;i<strarray.length;i++){ 3="" 4="" string="" str=" assdf aw da f " str1="str.trim();//去除首尾空格;" assdf="" aw="" da="" f="" true="" ture="" ads="" gh="g" int="" i="0;" g0123456789="" final="" pre=""></pre>
</strarray.length;i++){></code></code></code></code></code>