Java的引用数据类型——类(String类)

Java的引用数据类型可分为三种:类(class)、接口(interface)、数组。

1、“==”和“equals()”方法的区别
String str1 = "Hello”;

   String str2 = new String("Hello");

   String str3 = str2;

   System.out.println(str1==str2);  //false                System.out.println(str1.equals(str2));  //true

   System.out.println(str1==str3);  //false                System.out.println(str1.equals(str3));  //true

   System.out.println(str2==str3);  //true                 System.out.println(str2.equals(str3));  //true

可知,“==”比较的是两个字符串内存地址的数值是否相等,属于数值比较;

     “equals()”比较的是两个字符串的内容,属于内容比较。

2、匿名对象
匿名对象:如果一个对象没有引用,则称其为匿名对象。匿名对象只能使用一次,之后就会被GC回收释放。

使用字符串调用方法:

    String str = "Hello";

    System.out.println("Hello".equals(str));

3、String类的两种实例化方式及其区别
直接赋值:String str = “Hello”;

构造方法实例化:String str = new String("Hello");

区别:

1、直接赋值方法会将字符串的内容放入对象池(Object Pool),以供其他继续使用直接赋值的方式的String对象使用,即相同内容、都使用直接赋值法实例化的对象在堆中共享同一块保存对象内容的内存。

2、构造方法会开辟两块堆内存空间,其中一块将成为垃圾,且该对象不能入对象池(直接法实例化的对象不与其共享同一内存单元),但可用intern()方法入池。

例:“String s=new String(“mldn”);”创建了几个实例化对象?

    答:2个。

4、String类的常用方法
String与char转换

1、public String(char[] value)    //构造方法

2、public String(char[] value,int offset,int count)//构造方法

3、public char charAt(int index)

4、public char[] toCharArray()

String与byte转换

1、public String(byte[] bytes)

2、public String(byte[] bytes,int offset,int length)

3、public byte[] getBytes()

4、public byte[] getBytes()(String charsetName)throws UnsupportedEncodingException

String比较

1、public boolean equals(String anObject)    //区分大小写比较是否相等

2、public boolean equalsIgnoreCase(String anotherString)    //不区分大小写比较是否相等

3、public int compareTo(String anotherString)    //比较两字符串大小(大于(返回值大于0),小于(返回值小于0),等于(返回值等于0))

字符串查找

1、public boolean contains(String s)    //查找是否含有字符串

2、public int indexOf(String str)    //查找子串的起始位置,找不到返回-1

3、public int indexOf(String str,int fromIndex)    //从指定位置查找子串起始位置,找不到返回-1

4、public int lastIndexOf(String str)    //从后向前查找子串位置,找不到返回-1

5、public int lastIndexOf(String str,int fromIndex)    //从指定位置从后向前找

6、public boolean startsWith(String prefix)    //判断是否以指定字符串开头

7、public boolean startsWith(String prefix,int toffset)    //从指定位置判断是否以指定字符串开头

8、public boolean endsWith(String suffix)    //判断是否以指定字符串结尾

字符串替换

1、public String replaceAll(String regex,String replacement)    //全部替换字母

2、public String replaceFirst(String regex,String replacement)    //替换首个字母

字符串截取

1、public String substring(int beginIndex)    //从指定位置截取到最后

2、public String substring(int beginIndex,int endIndex)    //截取指定范围内的内容

字符串拆分

1、public String[] split(String regex)    //按照指定的字符全拆分为字符串数组

2、public String[] split(String regex,int limit)    //拆分为指定的长度

其他方法

1、public boolean isEmpty()    //判断是否为空字符串

2、public int length()    //    取得字符串长度

3、public String trim()    //去掉左右空格

4、public String toLowerCase    //将全部字符串转为小写

5、public String toUpperCase    //将全部字符串转为大写

6、public String intern()    //入池

7、public String concat(String str)    //字符串连接
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值