Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the tostring method in java

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article focuses on "how to use the tostring method in java". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to use the tostring method in java.

We can know from the java document that the toString () method is defined in the object class and its return type is of type String, which returns the class name and its reference address.

When the String class connects with other types, the toString () method is called automatically, as shown in demo:

Date now = new Date (); System.out.println ("now =" + now); / / equivalent to the next line of code System.out.println ("now =" + now.toString ())

In practical application, you can override the toString () method in a user-defined type as needed. For example, the Stirng class overrides the toString () method and returns the value of the string. The dome is as follows

System.out.println (S1); / / equivalent to the next line of code System.out.println (s1.toString ())

When the basic data type is converted to String, the toString () method of the corresponding wrapper class is called, and the demo is as follows:

Int a = 10 × system. Out.println ("a =" + a)

Now let's take a look at the source code in jdk:

Public String toString () {return getClass () .getName () + "@" + Integer.toHexString (hashCode ());}

Now let's practice using it:

Person p1 = new Person (); System.out.println (p1.toString ()); / / TestEquals.Person@15db9742System.out.println (p1); / / TestEquals.Person@15db9742 because the Objec class toString () method is called by default when exporting

When we print a reference to an object, we actually call the object's toString () method by default

When the class of the printed object does not override the toString () method in Object, the toString () method in the Object class is called by default.

Returns the first address value of the class in which this object resides and the corresponding heap space object entity.

When the class in which we print the object overrides toString (), we call the overridden toString () method, which generally returns the property information of the class object.

We can also customize a tostring () method:

/ / manually implement public String toString () {return "Person:name=" + name + "age=" + age;}

At this point, I believe you have a deeper understanding of "how to use the tostring method in java". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report