1.toString()方法
1)toString()方法在Object类里定义的,其返回值类型为String类型,返回类名和它的引用地址。
2)toString()会在还未重写之前默认调用,如
Date now = new Date();
System.out.println("now = " + now);//相当于下一行代码
System.out.println("now = " + now.toString());
3)重写toString(重点)
public String toString() {
return "名字是:"+name+","+"年龄是:"+age;
}
上述代码即重写toString方法,重写之后再通过对象引用toString方法后,则会输出编辑好的返回值。
4)toSstring与数组
to String()方法还可以用于对象数组,使用时既要在类中重写to String()方法又要调用Arrays类的to String()方法。
public static void main4(String[] args) {
final int array[] = {1,2,3,4,5};
array[0] = 100;
System.out.println(Arrays.toString(array));
}
public static void main(String[] args) {
int a[] = {1,2,3,4};
a[0]=100;
System.out.println(Arrays.toString(a));
}
上述两个mian方法均可实现数组的更换,但这是再重写toString后才能实现。
若未重写,则会报错。
2.基本类型转换String类型
基本上所有的基本类型都可以使用 String.valueOf() 方法去转换为 String 类型
public static void main(String[] args) {
int a = 10;
String b = String.valueOf(a);
System.out.println(b);
}
包装类型时可以使用 toString() 方法去成功转换为 String 类型的。
Integer c = 10;
String cc = c.toString();
System.out.println(cc);
注意:若变量未null时,使用 toString() 方法转换为 String 类型会报错
包装类:
3.String,StringBuffer和StringBuilder
大致区别:
string的值无法改变
public static void main6(String[] args) {
String str = new String("abc");
str=new String("CDF");
System.out.println(str);
}//输出结果为CDF
虽然输出结果为“CDF”,但这并补代表string的值被改变了,举个例子:一间宿舍叫518里面的人总是在换,但它依旧是518,仍然在第五层第18间。就想这里只是让str指向了一个新的对象,并不是改变了它的值。
因为String类型无法修改的特性,在需要大量修改字符串的时候,特别是在循环中使用的时候,运用String会产生过多的对象,从而造成空间的浪费和运行效率降低。这时就需要用到StringBuffer和StringBuilder。
StringBuffer和StringBuilder二者的区别不大,重点是掌握其常用的一些方法。
4.hasNext()和next()
hasNext()遇到空格会自动把字符串截断,也就是说hasNext()只能获取到空格之前和空格之后的字符串,并返回true。
public static void main(String[] args) {
Scanner str = new Scanner(System.in);
while (str.hasNext()){
System.out.println(str.next());
}
//System.out.println(str.next());
}
上述代码中hasNext()方法自动把字符串截断,然后再通过next()方法将截断的字符串输出
5.String对象的比较
public static void main(String[] args) {
int a = 10;
int b = 20;
int c = 10;
// 对于基本类型变量,==比较两个变量中存储的值是否相同
System.out.println(a == b); // false
System.out.println(a == c); // true
// 对于引用类型变量,==比较两个引用变量引用的是否为同一个对象
String s1 = new String("hello");
String s2 = new String("hello");
String s3 = new String("world");
String s4 = s1;
System.out.println(s1 == s2); // false
System.out.println(s2 == s3); // false
System.out.println(s1 == s4); // true
public static void main(String[] args) {
String s1 = new String("abc");
String s2 = new String("ac");
String s3 = new String("abc");
String s4 = new String("abcdef");
System.out.println(s1.compareTo(s2)); // 不同输出字符差值-1
System.out.println(s1.compareTo(s3)); // 相同输出 0
System.out.println(s1.compareTo(s4)); // 前k个字符完全相同,输出长度差值 -3
public static void main(String[] args) {
String s1 = new String("abc");
String s2 = new String("ac");
String s3 = new String("ABc");
String s4 = new String("abcdef");
System.out.println(s1.compareToIgnoreCase(s2)); // 不同输出字符差值-1
System.out.println(s1.compareToIgnoreCase(s3)); // 相同输出 0
System.out.println(s1.compareToIgnoreCase(s4)); // 前k个字符完全相同,输出长度差值 -3
}
谢谢观看,如果觉得博主写的还ok,别完了点赞,这对我真的很重要!!!