文章目录
Object类中的toString()方法
Object类型相当于java中任意类型,直接输出对象名称,相当于调用Object中的toString()方法,默认打印包名.类名@十六进制数(地址值)
Object类的任意子类都建议重写toString()方法,描述当前对象简易并且易于读懂的信息表达式
快捷键Alt+shift+s—>s
String类对象的特点:
string str=“hello”;
字符串是常量,一旦被赋值,不能被更改
上面语句在内存中做的事情:
1). 在栈内存中开辟空间,定义String str
2). 在方法区的常量池中寻找是否有"hello",如果有,将地址值赋值给str,如果没有,在常量池中开辟空间,存放"hello",再将地址值赋值给str
Object类之方法---------clone()
protected Object clone() throw CloneNotSupportedException
克隆方法 创建并返回该对象的一个副本
插叙一段:
protected void finalize()throws Throwable(跟垃圾回收器有关系: System类的时候讲)
-
垃圾回收器确定不存在对该对象的更多引用时,由对象的垃圾回收器调用此方法。
-
异常的处理方式有两种:
-
throws:抛出
-
try…catch…finally:捕获异常
回头咱们再说克隆方法
使用克隆方法之前,对象所在的类必须实现cloneable这个接口
在使用克隆方法的时候需要处理clone()方法的异常,我们这里为了方便在调用克隆方法的方法上
public static void main(String[] args) throws CloneNotSupportedException {
抛出异常,即可顺利使用克隆方法
之前经常用的键盘输入对象问题
之前经常用 Scanner sc=new Scanner(System.in)
Scanner (java.util.Scanner)
其实是使用了Scanner类中的构造方法
public Scanner(InputStream source)
形式参数:
抽象类:java.io.InputStream:字节输入流(用来读取字节数)
在JDK提供的System类中有in属性,返回值类型为InputStream
同理:System.out.println()
System.out返回类型为PrintStream
println()是PrintStream类中的方法
Scanner类中的一些判断方法:
public boolean hasNextXXX(){}判断当前是否是XXX类型(解决当前录入的数据和接收的标记类型不匹配的问题)
可以是int double float …
举例:
Scanner sc=new Scanner(System.in); if(sc.hasNextInt){ int i=sc.nextInt(); }else if(sc.hasNextFloat){ float f=sc.nextFloat(); }else{......}
String类的一个特殊之处:
String类型作为形式参数,形参的改变不会影响实际参数的值
因为String类的对象是不可变的不可更改的,方法中的形参变化在方法调用完毕会消失,实际参数不会变化
length的区分
数组中有length属性
String类中有length()方法
集合中没有length()方法,有size()方法
String类的构造方法:
public String(){} 无参构造
public String(byte[] bytes){} 使用平台默认字符编码集将字节数组构造出来字符串
public String(byte[] bytes,int offset,int length){} 将字节数组的一部分从offset位置开始,长度为length构造成字符串
public String(char[] value){}将字符数组构造成字符串
public String(char[] value,int offset,int count){} 将字符数组的一部分构造为字符串,起始位置为offset,长度为count
public String(String original)将字符串存储进来,创建新的对象
String 类中的两个问题:
String s1="hello";
String s2=new String("hello");
System.out.println(s1==s2);//false
System.out.println(s1.equals(s2));//true
*****
第一行会在常量池中开辟空间存放"hello"将地址值给s1
第三行会先在堆内存中开辟空间,指向常量池中的"hello",将对内存的地址值给s2
所以地址值不同,内容相同
2).
变量相加,先去开空间,再然后在相加,==(比较的地址值是否相同)
常量相加:先相加,然后常量池看是否存在这个值,如果存在直接返回地址,如果不存在开辟空间
public class StringDemo4 {
public static void main(String[] args) {
String s1 = "hello" ;
String s2 = "world" ;
String s3 = "helloworld" ;
System.out.println((s1+s2)==s3);//false
System.out.println((s1+s2).equals(s3));//true
System.out.println("------------------------------");
System.out.println(s3 == "hello"+"world");//true
System.out.println(s3.equals("hello"+"world"));//true
}
}
插叙一句:String类中已经重写了Object类中的equals()方法,比较的是两个字符串的内容
String类中的一些判断功能
public boolean equals(Object anobject)判断两个字符串内容是否相同
public boolean equalsIgnoreCase(String anotherString)判断两个字符串是否相同,不区分大小写
public boolean startWith(String prefix){}判断字符串是否以prefix开头
public boolean endWith(String suffix){}判断字符串是否以suffix结尾
public boolean isEmpty(){}判断字符串是否为空
String类的一些获取功能
public int length()获取字符串的长度
public char charAt(int index)获取字符串中index位置的字符
public String concat(String str)将字符串与给定字符串进行拼接
public int indexOf(char ch)获取给定字符在字符串中第一次出现的位置
public int indexOf(String str)获取给定字符串在当前字符串中第一次出现的位置
String类分割功能
public String[] split(String regex)按照特定字符串对当前字符串进行分割,返回字符串数组
String类的转换功能
public char[] toCharArray(){}将字符串转换为字符数组
public byte[] getBytes(){}将字符串转换为字节数组(相当于编码)
public String valueOf(XXX xx){}将任意类型转换为字符串
public String toLowerCase()将字符串中大写字母转换为小写字母
public String toUpperCase()将字符串中小写字母转换为大写字母
符串转换为字符数组
public byte[] getBytes(){}将字符串转换为字节数组(相当于编码)
public String valueOf(XXX xx){}将任意类型转换为字符串
public String toLowerCase()将字符串中大写字母转换为小写字母
public String toUpperCase()将字符串中小写字母转换为大写字母