Java Foundation __tostring () method

Source: Internet
Author: User
Tags stub

Java toString () method

(i), convenient println () The output of the method

 Public clasststring {PrivateString name;  Publictstring (String name) { This. Name =name; }         PublicString toString () {return"Name:" +name; }         Public Static voidMain (string[] args) {//TODO auto-generated Method StubTstring student=NewTstring ("Gary");    System.out.println (Student); }}

Output:

Name: Gary

ToString is a method that is already in object inheritance, and all classes inherit object, so "all objects have this method"

It is usually just for the convenience of the output, such as System.out.println (XX), the parentheses inside the "XX" if it is not a string type, automatically call XX's ToString () method

The (ii), ToString () method is used to return the value of a number object expressed as a string.

 Public class Text {    publicstaticvoid  main (string[] args) {        // TODO auto-generated Method stub              Integer x = 5;            System.out.println (X.tostring ());              System.out.println (integer.tostring);}     }

Output:

512

If the method uses the native data type as a parameter, the String object value of the native data type is returned.

If the method has two parameters, returns the string representation of the first argument that is specified by the second argument as a cardinality.

return value
    • toString (): Returns a String object that represents an Integer value.

    • toString (int i): returns a String object that represents the specified int.

Java Foundation __tostring () method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.