String对象是不可变得
1.字符串一旦创建便不可再改变。“abc”字符串对象一旦创建,不可再改变为其他的如"abcd"。

public class Test{public static void main(String[] args){//创建一个“abc”对象,该对象内存地址,让s1变量保存//s1是一个引用,s1指向"abc"对象.String s1 = "abc";//可以让s1重新指向吗?s1是局部变量,s1前边没有final,所以s1可以重新指向//但是"def"字符串本身不可变s1 = "def";}}

2.提升字符串的访问效率:在程序中使用了"缓存"技术。所以在java中所有使用"双引号"括起来的字符串都会在"字符串常量池"中创建一份。字符串常量池在方法去中被存储
如果创建一个新的字符串没有出现过,第一次创建,如果字符串常量池中没有该字符串,则将创建的新的字符串放入该字符串常量池中,当下次如果再使用时,直接在该常量池中取即可,(字符串常量池是一个缓存技术,为了提高访问字符串效率)

public class Test{public static void main(String[] args){String s1 = "abc";//s1的指向是可以改变的,但是字符串"abc"是不可以改变的String s2 = "abc";String s3 = "abc";//syso的"=="里面比较的是两个变量指向的内存地址,输出结果为true可以看出来s2和s3指向的是同一个内存地址,印证了字符串常量池的。System.out.println(s1==s2);//输出结果为true//比较两个字符串内容,不能用"=="String s4 = new String("abc");String s5 = new String("abc");//这里创建的字符串放在的是创建的String对象里的,所s4和s5指向的是不同对象,指向的内存地址是不一样的System.out.println(s4 == s5);//false//想要比较字符串的内容,必须得通过String里的equals方法来比较System.out.println(s4.equals(s5));//输出结果为true//以下程序执行结束之后,会在字符串常量池中创建3个字符串对象//分别为"abc","def","abcdef"//所以一般不建议使用'+'来连接两个字符串,会在缓存中消耗大量的内存String s6 = "abc";String s7 = "def";String S8 = s6+s7;}
}

分析以下程序创建字符串对象的区别

public class Test{public static void main(String[] args){//只会在字符串池常量中出创建一个"abc"字符串对象String s1 = "abc";//会在字符串常量池中创建一个"hello"对象,并且会在堆中再创建一个字符串对象//这种方式比较浪费内存String s2 = new String("hello");}}

以下为一个String面试题:

public class StringTest{public static void main(String[] args){//判断以下程序创建了几个对象?String s1 = new String("abc");String s2 = new String("abc");//会创建三个对象,方法区的字符串常量池中有一个"hello"//另外两个在堆中(new了两个对象)}
}

注意:使用String的时候我们应该注意的问题:进来不要做字符串频繁的拼接操作。
因为字符串一旦创建不可改变,只要频繁拼接,就会在字符串常量池中创建大量的字符串对象,给垃圾回收带来问题。

以下代码会创建6个对象,非常的浪费内存,频繁的拼接导致

public class Test{public static void main(String[] args){String[] ins = {"i","love","u"};String s;//将字符串拼接,如果使用'+'来拼接for(int i = 0;i<ins.length;i++){if(){s += ins[i]; }else{s += ins[i]+",";}}
}

关于字符串的常用方法
1.关于字符串常用构造方法

public class StringTest{public static void main(String[] args){String s1 = "abc";String s2 = new String("abc");byte[] bytes = {97,98,99,100};String s3 = new String(bytes);//这是将byte数组转换为字符串System.out.println(s3);//这里输出的是abcd,abcd的ACSLL码值为97,98,99,100.可以看出String已经重写过了toString方法,因为syso里加对象名输出的是对象的toString方法  //String​(byte[] bytes, int offset, int length),将byte数组指定位置指定长度,转换为字符串String s4 =new String(bytes,1,2);//将bytes数字1位置,长度为2的数组转化为字符串。System.out.println(s4);//输出结果为bc//String​(char[] value)  将char数组转换为字符串//用法和byte数组相似,将char型转换为字符串char[] c1 ={'一','二','三','四','五','六'};//String​(char[] value, int offset, int count) 将char数组指定位置的指定长度转化为字符串}

2.字符串的常用方法
(1).char charAt​(int index) Returns the char value at the specified index(返回指定位置的char值).

public class Test{public static void main(String[] args){String s1 = "sdsdsd"char c1 = s1.charAt(2);//输出为s       }}

(2).boolean endsWith​(String suffix) Tests if this string ends with the specified suffix(此字符串是否以指定后缀结束).

public class Test{public static void main(String[] args){String s1 = "new year";boolean s2 = s1.endsWith("ar");System.out.println(s2);  //输出结果为true
}
}

(3).boolean equalsIgnoreCase​(String anotherString) Compares this String to another String, ignoring case considerations(在忽略大小写的情况下,比较两个字符串是否相等).
(4).byte[] getBytes() Encodes this String into a sequence of bytes using the platform’s default charset, storing the result into a new byte array(将字符串转换为byte数组).

public class Test{public static void main(String[] args){String s1 = "abc";byte[] b1 = s1.getBytes();for(int i=0;i<b1.length;i++){System.out.println(b1[i]);}
}
}

(5).int indexOf​(int ch) Returns the index within this string of the first occurrence of the specified character(返回指定字符串在此字符串中第一次出现的索引).

 System.out.println("httpsdsdsasd//:asdasd".indexOf("sd"));//输出为4即在位置4找到指定字符串的一次出现System.out.println("http".indexOf("sd"));//输出-1,当此字符串中没有该字字符串的时候,返回值为-1

(6).int indexOf​(int ch, int fromIndex) Returns the index within this string of the first occurrence of the specified cha substring, starting the search at the specified index(从指定索引开始,返回指定字符串在此字符串中第一次出现的索引).

System.out.println("httpsdsdsasd//:asdasd".indexOf("sd",2));//输出4,但是本次寻找是从位置2开始

(7).int lastIndexOf​(String str) Returns the index within this string of the last occurrence of the specified substring(返回指定字符串在字符串中最右边出现处的索引).
(8).int lastIndexOf​(String str, int fromIndex) Returns the index within this string of the last occurrence of the specified substring, searching backward starting at the specified index(从指定位置,在此字符串中反向搜索该指定字符串).

(9).int length() Returns the length of this string.(返回字符串的长度)
这里注意length在字符串是方法,在数组中是属性

(10).String replaceAll​(String regex, String replacement) Replaces each substring of this string that matches the given regular expression with the given replacement.

System.out.println("zuoyou"replaceAll("zuo","you"));//输出youyou
//是把此字符串中的部分字符串用子指定字符串来进行替换
//总共会创建4个对象,在字符串常量池中,再次印证了字符串对象一经创建,不能被改变

(11).String[] split​(String regex) Splits this string around matches of the given regular expression(根据给定的正则表达式的匹配拆分此字符串).

具体用法如下

String n1 = "2018,20,5";
String[] n2 = n1.split(","); //代表以","分割,并将其存入数组中
for(int i=0;i<n2.length;i++)
{System.out.println(n2[i]);
}
//输出结果:
2018
20
5
/

12.boolean startsWith​(String prefix) Tests if this string starts with the specified prefix.(查看此字符串是否以指定字符串开始)

System.out.println("absd".startsWith("a"));//返回结果为true

13.String substring​(int beginIndex) Returns a string that is a substring of this string.(在指定位置处分割此字符串,把指定位置字符串后面的包括该指定位置内容返回)

System.out.println("1234567".substring(5));//返回值为为67

14.String substring​(int beginIndex, int endIndex) Returns a string that is a substring of this string.(对比13,加上了一个区间,分割该区间内的内容)

System.out.println("1234567".substring(1,3));//返回值为为23
//通过返回的值为23可以知道为一个左闭右开的区间

15.String toUpperCase() Converts all of the characters in this String to upper case using the rules of the default locale.(将字符串里的字符转换为大写)

System.out.println("abc".toUpperCase());//输出为ABC

16.String toLowerCase() Converts all of the characters in this String to lower case using the rules of the default locale(将字符串里的字符转换为小写).

 System.out.println("ABC".toLowerCase());

17.String trim() Returns a string whose value is this string, with all leading and trailing space removed, where space is defined as any character whose codepoint is less than or equal to ‘U+0020’ (the space character).(去除字符串前后的空白)

System.out.println("    sds    dsds  sds  dssd".trim()); //输出结果,sds    dsds  sds  dssd,前后空白被去除了

18.还有String一系列的静态vauleOf方法

其中一个例子,在System.out.println()中有引用String    valueOf​(Object obj)   //该方法内部会判断,obj为空时,返回null.不为空时,返回obj.toString();Object o1 = new Object();
o1 = null;System.out.println(o1);//不会有异常,会输出null,相当于System.out.println(String.vauleOf(o1));System.out.println(o1.toString());//会报出空指针异常

(常用类_String类)相关推荐

  1. Java 常用对象-Date类和Calender类

    2017-11-02 22:29:34 Date类:类 Date 表示特定的瞬间,精确到毫秒. 在 JDK 1.1 之前,类 Date 有两个其他的函数.它允许把日期解释为年.月.日.小时.分钟和秒值 ...

  2. Java常用实体类--System类

    字符串.日期.数字是Java程序中最常使用的数据对象,对这些数据的创建.修改.格式化和转换等操作融入在Java程序的每个角落,必须熟练掌握.本节将通过实例演示以下常用实体类 Java系统级类:系统类S ...

  3. java.util类,GitHub - yutaolian/JavaUtils: 总结的一些Java常用的util类

    JavaUtils 总结的一些Java常用的util类 ###1.格式化时间 SimpleDateFormat(DateFormat)实现线程安全的使用 众所周知SimpleDateFormat(Da ...

  4. JDBC—01—JDBC简介;JDBC常用接口与类;

    一. JDBC 简介 1 什么是 JDBC JDBC(Java DataBase Connectivity)java 数据库连接 是 JavaEE 平台下的技术规范 定义了在 Java 语言中连接数据 ...

  5. 几种常用的JS类定义方法

    几种常用的JS类定义方法 // 方法1 对象直接量 var obj1 = {     v1 : "",     get_v1 : function() {         retu ...

  6. Java教程之JDBC中的常用接口和类

    JDBC定义了一系列操作数据库的接口和类,这些接口和类位于java.sql包中.接下来,本节将详细介绍JDBC的常用API. Driver接口 Driver接口是所有JDBC驱动程序必须要实现的接口, ...

  7. Android常用的工具类

    2019独角兽企业重金招聘Python工程师标准>>> 最新最准确内容建议直接访问原文:Android常用的工具类 主要介绍总结的Android开发中常用的工具类,大部分同样适用于J ...

  8. 常用并发工具类(锁和线程间通信工具类)

    常用并发工具类总结 JUC 下的常用并发工具类(锁和线程间通信工具类),主要包括 ReentrantLock.ReentrantReadWriteLock.CountDownLatch.CyclicB ...

  9. 日期的包装 java,Java基础之Java常用类--Object类,字符串相关类,包装类,日期相关类,数字相关类...

    Java是一种面向对象的语言,也就是将万事万物可以描述为对象,特点如下: 1.面向对象是常见的一种思考习惯,符合人们的思考习惯. 2.面向对象的出现,将复杂的事情简单化. 3.面向对象的出现,将之前过 ...

最新文章

  1. Android ListView (多个adapter 说明)
  2. 小米五android o卡吗,【图片】小米5优化教程,吃鸡卡的进来!!!_小米5吧_百度贴吧...
  3. 企业级java springcloud b2bc商城系统开源源码二次开发-hystrix 请求缓存
  4. 数据结构--二叉查找树 Binary Search Tree
  5. 【LeetCode】剑指 Offer 40. 最小的k个数
  6. 初中生学python教材推荐_推荐中学生看的几本书
  7. 【Oracle】11g外部表指定oracle_datapump引擎,不能使用preprocessor预处理子句。
  8. 错误代码741 因为文件名产生符号链接,所以需由对象管理器重新运行分析操作。
  9. 论文翻译:2021_Towards model compression for deep learning based speech enhancement
  10. 罗永浩、戴威的C位消亡史
  11. vs+cmake完美编译RTS游戏,类似魔兽争霸源码
  12. element-ui dialog组件添加可拖拽位置 可拖拽宽高
  13. 高通平台GPIO模拟PWM控制背光
  14. PingCAP 与 DSG 达成战略合作,共同开启数据智能管理新篇章
  15. Windows 10和Linux脚本启动jar包服务器,并设置开机启动
  16. 计算机可以不需要显卡吗,显卡有什么用 电脑不装显卡影响大吗
  17. CodeBlocks编译环境配置及调试问题
  18. 钢铁侠是怎样炼成的:一段给人启示的英雄传奇
  19. OpenWrt下SSR与XWare迅雷远程冲突问题
  20. 斐讯T1/N1 Linux 更换中文系统环境和界面

热门文章

  1. 什么时候会发生FullGC
  2. 将分区表类型改为GUID格式后 开不了机的问题
  3. Timus 1295. Crazy Notions
  4. canvas核心内容
  5. 在vue中使用nicescroll
  6. Intervention/image处理ios图片是发生旋转的处理
  7. 日系插画培训网课怎么选
  8. boost:filesystem
  9. 关于单片机检测高电平
  10. 或许是时候说一声再见