Java methods, arguments, and return values

Source: Internet
Author: User

To date, we have used the term function to refer to a named subroutine. In Java, however, a more commonly used word is "method", representing "the way to do something." Although the actual meaning of their expression is the same, from now on, the book will always use "method", not "function".
Java's "method" determines the message that an object can receive. Through this section of study, you will know how simple the definition of the method!
The basic components of a method include name, argument, return type, and body. Here is its most basic form:

return type method Name (/* Self variable list */) {/* method principal/}

The return type refers to the numeric type returned after the method is invoked. Obviously, the function of the method name is to identify and reference the specific method. The list of arguments lists the type and name of the information you want to pass to the method.
Java methods can only be created as part of a class. Only one method (annotation ③) can be invoked against an object, and that object must be able to execute that method call. If you try to invoke the wrong method for an object, you will get an error message at compile time. When you invoke a method for an object, you need to list the name of the object, followed by a period, followed by the method name and its argument list. That is, the object name. Method Name (independent variable 1, independent variable 2, independent variable 3 ...). For example, suppose we have a method called F (), which has no arguments, and returns a value of type int. So, suppose you have an object named a that you can call method F (), the code is as follows:
int x = A.F ();
The type of the return value must be compatible with the type of x.
The act of calling a method like this is often called "send a message to an object." In the example above, the message is F (), and the object is a. Object-oriented programming is often simply summed up as "sending messages to objects."

③: As you'll soon learn, the "static" method can be invoked against a class, without the need for an object.

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.