下列程序的输出是什么?

class A { public static void main(String[] a) {
    String v = “base”;      v.concat(“ball”);
    v.substring(1,5);       System.out.println(v);  }
}

分析:由于String是不可变的,当执行v.concat("ball")时,v本身还是指向“base”,当再执行v.substring(1,5)时,会报出界异常

如下为concat的源码:可以看到,最后返回的是新的字符串

public String concat(String str) {
int otherLen = str.length();
if (otherLen == 0) {
return this;
}
int len = value.length;
char buf[] = Arrays.copyOf(value, len + otherLen);
str.getChars(buf, len);
return new String(buf, true);
}

如下是substring源码:

public String substring(int beginIndex) {
if (beginIndex < 0) {
throw new StringIndexOutOfBoundsException(beginIndex);
}
int subLen = value.length - beginIndex;
if (subLen < 0) {
throw new StringIndexOutOfBoundsException(subLen);
}
return (beginIndex == 0) ? this : new String(value, beginIndex, subLen);
} 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);
}

String类的substring方法的更多相关文章

  1. 【转载】C#中string类使用Substring方法截取字符串

    在C#的字符串操作过程中,截取字符串是一种常见的字符串操作,可使用string类的Substring方法来完成字符串的截取操作,该方法支持设定截取的开始位置以及截取的字符串长度等参数,Substrin ...

  2. String类的substring()方法

    截取字符串,在java语言中的用法 1.  public String substring(int beginIndex) 返回一个新字符串,它是此字符串的一个子字符串.该子字符串始于指定索引处的字符 ...

  3. 关于JAVA的String类的一些方法

    一.得到字符串对象的有关信息 1.通过调用length()方法得到String的长度. String str=”This is a String”; int len =str.length(); 2. ...

  4. C++ string 类的 find 方法实例详解

    1.C++ 中 string 类的 find 方法列表 size_type std::basic_string::find(const basic_string &__str, size_ty ...

  5. java.lang.String 类的所有方法

    java.lang.String 类的所有方法 方法摘要 char charAt(int index) 返回指定索引处的 char 值. int codePointAt(int index) 返回指定 ...

  6. String类的indexOf方法的用法和举例

    2017年3月3号博主第一次去郑州互联网公司面试,背景是这样的我先前去了农大龙子湖校园招聘投简历,然后第二天去面试了那经历可以说是很失败的一次面试,当然这跟自己的水平有关了接下来重点讲一下面试的题目: ...

  7. String类的常见方法的使用案例

    String类的常见方法的使用案例 //使用指定的字符串替换当前字符串中指定的内容 //将helloworld中的o替换为a String s="HelloWorld"; Stri ...

  8. Java中String类的format方法使用总结

    可参考: http://www.cnblogs.com/fsjohnhuang/p/4094777.html http://kgd1120.iteye.com/blog/1293633 String类 ...

  9. String类的split方法以及StringTokenizer

    split方法可以根据指定的表达式regex将一个字符串分割成一个子字符串数组. 它的参数有两种形式,也即:split(String regex)和split(String regex, int li ...

随机推荐

  1. Android-SqliteSQL语句大全

    SqliteSQL语句大全 创表语句: create table student_table(_id integer primary key autoincrement, name text, age ...

  2. css选择器与DOM'匹配的关系

    一道面试题 css 选择器匹配时,只考察是否包含有对应的class,而与class的顺序无关 而css的定义是后面的覆盖前面的定义 原理:http://www.w3.org/html/ig/zh/wi ...

  3. TSQL--INT转换成指定长度字符串

    -- ================================================ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ...

  4. 学习React前端框架,报错 'React' must be in scope when using JSX react/react-in-jsx-scope

    问题 import react from 'react'  改成  import React from 'react'   小写 react  改成 React

  5. C# PowerPoint操作的基本用法。

    代码using System;using System.Collections.Generic;using System.Linq;using System.Text;using OFFICECORE ...

  6. GO学习笔记 - 用defer来实现try{}finally{}

    在Delphi中,try{}finally{}语句非常有用,对于一定要最终执行的语句,我们放到finally,从而保证程序顺利执行!在GO语言中没有try{}finally{}语句,但是GO语言用另外 ...

  7. G - 確率(水题)

    原文链接 G - 確率 Time Limit:1000MS     Memory Limit:131072KB     64bit IO Format:%lld & %llu Submit S ...

  8. docker-ce的安装以及卸载

    注意:以下命令无特殊说明外均在root用户下执行 一.Docker CE的安装 1. 首先,卸载老旧的docker. yum remove docker \ docker-client \ docke ...

  9. webpack 中文文档

    webpack 最强最详细中文文档 https://doc.webpack-china.org/guides/getting-started/#- webpack多页应用架构系列 http://web ...

  10. mxonline实战7,模板继承和模板标签

        对应github地址:https://github.com/pshyms/django/tree/master/mxonline/7_day     一. 定制不同页面中样式相同,内容不同的模 ...