黑马程序员 Java基础 String类

------ Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------

一 String字符串

1字符串:就是由多个字符组成的一串数据。也可以看成是一个字符数组。

字符串是常量,一旦被赋值,就不能被改变。存放在方法区中的常量池中。

 

2 String s = new String("hello");String s = "hello"的区别?

 

 

看程序写结果

==equals()

String s1 = new String("hello");

String s2 = new String("hello");

System.out.println(s1 == s2);// false

System.out.println(s1.equals(s2));// true

 

String s3 = new String("hello");

String s4 = "hello";

System.out.println(s3 == s4);// false

System.out.println(s3.equals(s4));// true

 

String s5 = "hello";

String s6 = "hello";

System.out.println(s5 == s6);// true

System.out.println(s5.equals(s6));// true



 

关于字符串的拼接

String s1 = "hello";

String s2 = "world";

String s3 = "helloworld";

System.out.println(s3 == s1 + s2);// false

System.out.println(s3.equals((s1 + s2)));// true

 

System.out.println(s3 == "hello" + "world");// false 这个易错,应该是true

System.out.println(s3.equals("hello" + "world"));// true



笔记:

字符串如果是变量相加,先开空间,在拼接。

字符串如果是常量相加,是先加,然后在常量池找,如果有就直接返回,否则,就创建。

 

方法

A:判断功能

boolean equals(Object obj)

boolean equalsIgnoreCase(String str)

boolean contains(String str)

boolean startsWith(String str)

boolean endsWith(String str)

boolean isEmpty()

B:获取功能

char charAt(int index)

int indexOf(int ch)

int indexOf(String str)

int indexOf(int ch,int fromIndex)

int indexOf(String str,int fromIndex)

String substring(int start)

String substring(int start,int end)

C:转换功能

byte[] getBytes()

char[] toCharArray()

static String valueOf(char[] chs)

static String valueOf(int i)

String toLowerCase()

String toUpperCase()

String concat(String str)

D:其他功能

a:替换功能 

String replace(char old,char new)

String replace(String old,String new)(可以结合正则表达式理解)

b:去空格功能

String trim()

c:按字典比较功能

int compareTo(String str)  

//如果参数字符串等于此字符串,则返回值 0;如果此字符串按字典顺 序小于字符串参数,则返回一个小于 的值;如果此字符串按字典顺序 大于字符串参数,则返回一个大于 的值。

 

补充:

NullPointerException 空指针异常。对象都不存在,所以此时没法调方法。

这是一个常见的错误。

当应用程序试图在需要对象的地方使用 null 时,抛出该异常。这种情况包括:

 

    调用 null 对象的实例方法。

    访问或修改 null 对象的字段。

    如果一个数组为null,试图用属性length获得其长度时。

    如果一个数组为null,试图访问或修改其中某个元素时。

    在需要抛出一个异常对象,而该对象为 null 时。

 

应用程序将会抛出NullPointerException类的实例,表明其他对 null 对象的非法使用。

 

二、StringBuffer

1、作用

用字符串做拼接,比较耗时并且也耗内存,而这种拼接操作又是比较常见的,为了解决这个问题,Java就提供了一个字符串缓冲区类。StringBuffer供我们使用。

而且是线程安全的可变字符串。

 

2、特点

1)长度、内容均可变

2)缓冲区中可以存储任意类型的数据,但最终需要变成字符串。

 

3、常用方法

1)添加(说明StringBuffer 长度和内容均可变

append(data)://在缓冲区中追加数据。追加到尾部

insert(index,data)://在指定位置插入数据。

2)删除

delete(start,end);//删除从startend-1范围的元素

deleteCharAt(index);//删除指定位置的元素

3)修改。

void setCharAt(index,char);//替换指定位置的字符

4)查找。(查不到返回-1

int indexOf(string); //返回指定子字符串在此字符串中第一次出现处的索引

5)获取子串

string substring(start); //返回start到结尾的子串

string substring(start,end); //返回startend-1的子串

6)反转。

StringBuffer reverse();//字符串反转

  

案例

代码1:  StringStringBuffer的相互转换

public static void main(String[] args) {

String s = "hello";

 

// 注意:不能把字符串的值直接赋值给StringBuffer

// 方式1:通过构造方法

StringBuffer sb = new StringBuffer(s);

// 方式2:通过append()方法

StringBuffer sb2 = new StringBuffer();

sb2.append(s);

System.out.println("sb:" + sb + "\nsb2:" + sb2);

 

StringBuffer buffer = new StringBuffer("java");

// 方式1:通过构造方法

String str = new String(buffer);

// 方式2:通过toString()方法

String str2 = buffer.toString();

System.out.println("str:" + str + "\nstr2:" + str2);

}



 

代码2:把数组拼接成一个字符串

public static void main(String[] args) {

int[] arr = {44, 33, 55, 11, 22};

 

System.out.println("s1: " + arrayToString(arr));

 

System.out.println("s2: " + arrayToString2(arr));

}

 

public static String arrayToString(int[] arr){

String s = "";

 

s += "[";

for(int i = 0; i < arr.length; i++){

if(i != arr.length - 1){

s += arr[i] + ", ";

}else{

s += arr[i];

}

}

s += "]";

 

return s;

}

 

public static String arrayToString2(int[] arr){

StringBuffer sb = new StringBuffer();

 

sb.append("[");

for(int i = 0; i < arr.length; i++){

if(i != arr.length - 1){

sb.append(arr[i]).append(", ");

}else{

sb.append(arr[i]);

}

}

sb.append("]");

 

return sb.toString();

}



肯定是用StringBuffer更好。因为不浪费空间,不会生成很多字符串常量

 

代码3:把字符串反转 

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("请输入数据:");

String s = sc.nextLine();

 

System.out.println("s1:" + myReverse(s));

 

System.out.println("s1:" + myReverse2(s));

}

 

public static String myReverse(String s){

String result = "";

char[] chs = s.toCharArray();

 

for(int i = chs.length - 1; i >= 0 ; i--){

result += chs[i];

}

 

return result;

}

 

public static String myReverse2(String s){

return new StringBuffer(s).reverse().toString();

}



 

明显,StringBuffer 中有相应函数可以调用,所以用它更方便

 

代码4:判断一个字符串是否是对称字符串

例如"abc"不是对称字符串,"aba""abba""aaa""mnanm"是对称字符串

public static void main(String[] args) {
String s = "mnanam"; 

System.out.println(new StringBuffer(s).reverse().toString().equals(s));

}

 

代码5:形式参数问题(StringStringBuffer分别作为参数传递)

public static void main(String[] args) {

String s1 = "hello";

String s2 = "world";

System.out.println(s1 + "   " + s2);

change(s1, s2);

System.out.println(s1 + "   " + s2);

 

StringBuffer sb1 = new StringBuffer("hello");

StringBuffer sb2 = new StringBuffer("world");

System.out.println(sb1 + "   " + sb2);

change(sb1, sb2);

System.out.println(sb1 + "   " + sb2);

}

 

public static void change(String s1, String s2){

s1 = s2;

s2 = s1 + s2;

}

 

public static void change(StringBuffer sb1, StringBuffer sb2){

sb1 = sb2;

sb2.append(sb1);

}


hello   world

hello   world

hello   world

hello   worldworld

 

StringBuffer作为参数传递,调方法是会改变的,而赋值不会改变

String作为参数传递,效果和基本类型作为参数传递是一样的。

 

三、StringBuilder

用作StringBuffer的替换,功能基本与StringBuffer相同。但StringBuffer线程安全,StringBuilder线程不安全。在单线程中,可以使用StringBuilder效率高,在多线程中使用StringBuffer安全。

 

面试题:

1 String,StringBuffer,StringBuilder的区别?

A:String是内容不可变的,而StringBuffer,StringBuilder都是内容、长度可变的。

B:StringBuffer是同步的,数据安全,效率低;StringBuilder是不同步的,数据不安全,效率高

2 StringBuffer和数组的区别?

二者都可以看出是一个容器,装其他的数据。

但是呢,StringBuffer的数据最终是一个字符串数据。(也就是说,不管装什么都看成是字符)

而数组可以放置多种数据,但必须是同一种数据类型的。

 

 

四、基本数据类型包装类

1)作用

为了对基本数据类型进行更方便的操作,Java对每一种基本数据类型提供了对应的包装类。

 byte             Byte

 short            Short

 int              Integer

 long             Long

 float            Float

 double           Double

 char             Character

 boolean          Boolean 

 

2)基本数据类型与String之间的转换

Integer举例,其他类似:

public static void main(String[] args) {

System.out.println(Integer.parseInt("123"));

 

System.out.println(String.valueOf(123));

}


 

注:

parseXXX(String str);

除了Character外,其他包装类都有此方法。

 

3)自动装箱,自动拆箱机制

Integer ii = 100;

ii += 200;

System.out.println("ii:" + ii);

 

// 通过反编译后的代码

// Integer ii = Integer.valueOf(100); //自动装箱

// ii = Integer.valueOf(ii.intValue() + 200); //自动拆箱,再自动装箱

// System.out.println((new StringBuilder("ii:")).append(ii).toString());

 

 *static Integer valueOf(int i)  

 * static Integer valueOf(String s)

 

五 Arrays

1) Arrays工具类是针对数组进行操作的工具类。包括排序和查找等功能。

 

代码1

 * 1:public static String toString(int[] a) 把数组转成字符串

 * 2:public static void sort(int[] a) 对数组进行排序

 * 3:public static int binarySearch(int[] a,int key) 二分查找

public static void main(String[] args) {

int[] arr = { 24, 69, 80, 57, 13 };

 

Arrays.sort(arr);

System.out.println(Arrays.toString(arr));

 

System.out.println(Arrays.binarySearch(arr, 29));

System.out.println(Arrays.binarySearch(arr, 24));

}


 

2)Arrays工具类的源码解析

public static void sort(int[] a) 底层是快速排序

 

开发原则:

只要是对象,我们就要判断该对象是否为null

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值