toString()方法使用
生活随笔
收集整理的這篇文章主要介紹了
toString()方法使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
toString()方法在Object類中定義,其返回值是String類型,返回類名和它的引用地址。在進行String與其它類型數據的連接操作時,自動調用toString()方法。
Account account = new Account(); System.out.println(account); //com.atyeman.Account@1b6d3586 System.out.println(account.toString()); //com.atyeman.Account@1b6d3586 Date now=new Date(); System.out.println(“now=”+now); //相當于下面 System.out.println(“now=”+now.toString()); //因為Date重寫過,所以輸出的是自定義的時間信息。可以根據需要在用戶自定義類型中重寫toString()方法如String 類重寫了toString()方法,返回字符串的值。
s1=“hello”; System.out.println(s1);//相當于System.out.println(s1.toString());輸出“hello”總結
以上是生活随笔為你收集整理的toString()方法使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 备案迁移麻烦(备案迁移)
- 下一篇: Integer包装类特殊之处