跨境建站平台,接做网站需要问什么软件,注册安全工程师考试结果查询,电子商务网站建设论文开题报告toString()方法
toString()方法在Object类中定义#xff0c;其返回值是String类型#xff0c;返回类名和它的引用地址。在进行String与其它类型数据的连接操作时#xff0c;自动调用toString()方法。
Date nownew Date();
System.out.println(“now”now); 相当于
System.…toString()方法
toString()方法在Object类中定义其返回值是String类型返回类名和它的引用地址。在进行String与其它类型数据的连接操作时自动调用toString()方法。
Date nownew Date();
System.out.println(“now”now); 相当于
System.out.println(“now”now.toString());可以根据需要在用户自定义类型中重写toString()方法。例如String 类重写了toString()方法返回字符串的值。
s1“hello”;
System.out.println(s1);//相当于System.out.println(s1.toString());基本类型数据转换为String类型时调用了对应包装类的toString()方法。
int a10; System.out.println(“a”a);代码示例 /** Object类中toString()的使用* * 1. 当我们输出一个对象的引用时实际上就是调用当前对象的toString()* * 2. Object类中toString()的定义* public String toString() {return getClass().getName() Integer.toHexString(hashCode());}* * 3. 像String、Date、File、包装类等都重写了Object类中的toString()方法。* 使得在调用对象的toString()时返回实体内容信息* * 4. 自定义类也可以重写toString()方法当调用此方法时返回对象的实体内容*/
public class ToStringTest {public static void main(String[] args) {Customer cust1 new Customer(Tom, 21);System.out.println(cust1.toString());// com.atguigu.java1.Customer15db9742--Customer[name Tom,age 21]System.out.println(cust1);// com.atguigu.java1.Customer15db9742--Customer[name Tom,age 21]String str new String(MM);System.out.println(str);// MMDate date new Date(4534534534543L);System.out.println(date.toString());// Mon Sep 11 08:55:34 GMT08:00 2113}
}