原来String是这样的(下)
来源:互联网 发布:欠淘宝贷款200万 编辑:程序博客网 时间:2024/04/27 02:24
前言
回顾
我们讲到了String的equals和==的区别:
equals根据你编写的方法体来进行比较,而==是根据比较的引用地址是否相同来比较的。
主题
今天我们来讲讲String类的其他.方法在源码中的解读.
例子
12345678910111213
public class Test {public static void main(String[] args){String a1=new String("abc");String a2=new String("abc");System.out.println(a1.isEmpty());System.out.println(a1.length());System.out.println(a1.charAt(1);System.out.println(a1.substring(2,3));}}
String的几个方法
这里我们介绍了String的几个简单的方法。
1234
.isEmpty();.length();.charAt();.subString();
我们查看源码就可以知道:String方法的几个构造器
.isEmpty();
1234567891011
/*** Returns {@code true} if, and only if, {@link #length()} is {@code 0}.** @return {@code true} if {@link #length()} is {@code 0}, otherwise* {@code false}** @since 1.6*/public boolean isEmpty() {return value.length == 0;}
isEmpty直接判断传进来的值长度是否为0
.length();
1234567891011
/*** Returns the length of this string.* The length is equal to the number of <a href="Character.html#unicode">Unicode* code units</a> in the string.** @return the length of the sequence of characters represented by this* object.*/public int length() {return value.length;}
返回字符串的长度
.charAt();
123456789101112131415161718192021222324
/*** Returns the {@code char} value at the* specified index. An index ranges from {@code 0} to* {@code length() - 1}. The first {@code char} value of the sequence* is at index {@code 0}, the next at index {@code 1},* and so on, as for array indexing.** <p>If the {@code char} value specified by the index is a* <a href="Character.html#unicode">surrogate</a>, the surrogate* value is returned.** @param index the index of the {@code char} value.* @return the {@code char} value at the specified index of this string.* The first {@code char} value is at index {@code 0}.* @exception IndexOutOfBoundsException if the {@code index}* argument is negative or not less than the length of this* string.*/public char charAt(int index) {if ((index < 0) || (index >= value.length)) {throw new StringIndexOutOfBoundsException(index);}return value[index];}
charAt这里我们可以看到,类型为char,定义了一个下标
如果下标index<0或者>=字符的长度抛出StringIndexOutOfBoundsException
否则返回value[index] 这里我们返回的就是vlaue[1] 答案为b
.subString();
123456789101112131415161718192021222324252627282930313233343536
*** Returns a string that is a substring of this string. The* substring begins at the specified {@code beginIndex} and* extends to the character at index {@code endIndex - 1}.* Thus the length of the substring is {@code endIndex-beginIndex}.* <p>* Examples:* <blockquote><pre>* "hamburger".substring(4, 8) returns "urge"* "smiles".substring(1, 5) returns "mile"* </pre></blockquote>** @param beginIndex the beginning index, inclusive.* @param endIndex the ending index, exclusive.* @return the specified substring.* @exception IndexOutOfBoundsException if the* {@code beginIndex} is negative, or* {@code endIndex} is larger than the length of* this {@code String} object, or* {@code beginIndex} is larger than* {@code endIndex}.*/public String substring(int beginIndex, int endIndex) {if (beginIndex < 0) {throw new StringIndexOutOfBoundsException(beginIndex);}if (endIndex > value.length) {throw new StringIndexOutOfBoundsException(endIndex);}int subLen = endIndex - beginIndex;if (subLen < 0) {throw new StringIndexOutOfBoundsException(subLen);}return ((beginIndex == 0) && (endIndex == value.length)) ? this: new String(value, beginIndex, subLen);}
subString方法有两个参数,一个起始指标,一个结束指标
判断beginIndex < 0
抛出StringIndexOutOfBoundsException
,同样的结束index大于长度抛出StringIndexOutOfBoundsException
然后定义一个sbuLen
,为endIndex - beginIndex
,判断subLen小于0抛出StringIndexOutOfBoundsException
返回如果(beginIndex == 0) && (endIndex == value.length)
返回对象本身,否则该数组和beginIndex、
subLen`构成新的对象返回
这里讲的就是String基本的几个方法。这里还有一个小彩蛋就是&和&&的区别
大家可以回忆一下.
&:方法无论前值是否为true,都要判断后面的逻辑表达式
&&:会形成短路,前面为false的时候就不执行后面逻辑
结尾附上String的基本方法:
1234567891011121314151617181920212223
/*** 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.*/public String() {this.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.** @param original* A {@code String}*/public String(String original) {this.value = original.value;this.hash = original.hash;}
这就是String构成其实是一个数组,有value和hash两个属性,大家可以多多理解和想象一下.
- 原来String是这样的(下)
- 原来是这样的
- 原来是这样的
- 震惊!原来String类是这样的(上)
- 伤心原来是这样的!!!!!!
- 原来AJAX是这样的
- 原来新手是这样的
- TTTTTTTTTTDDDDDDDD原来是这样。。。。。。。。。。的
- 回文原来是这样的
- IT原来是这样的
- 世界原来是这样的
- 原来cvMinMaxLoc的roi是这样的
- 原来Smart Client 是这样的
- 网页病毒原来是这样产生的
- 原来define是这样用的
- 美丽心情原来是这样的:)
- 晕,原来指针是这样赋值的!
- 日本人,原来是这样来的
- 【Docker安装】- 卸掉docker参考
- sklearn.metrics.confusion_matrix
- Android直播间_001
- JAVA面向对象4:类的继承
- 关于“熵”的那些事
- 原来String是这样的(下)
- 数据库(第一,第二,第三范式)
- 优先考虑泛型
- 主机没连网线怎么连接虚拟机
- Java短信接口之验证码
- 从输入url到看到页面,发生了什么
- 微软的新一代Windows套件包管理器(OneGet)
- 互信息——事件相关性度量
- linux常用命令