Java List of arguments

Source: Internet
Author: User
Tags class definition

The argument list sets out what information we send to the method. As you may have guessed, this information-as anything else in Java-takes the form of objects. Therefore, we must specify the type of object to pass in the argument list, and the name of each object. As we do with objects elsewhere in Java, we actually pass "handle" (Comment ④). However, the handle must be of the correct type. If you want the argument to be a "string", then the pass must be a string.

④: Is an exception for the "special" data types mentioned previously Boolean,char,byte,short,int,long,,float and double. When passing an object, however, it usually means passing a handle to an object.

Let's consider the method used to use a string as an argument. The following list defines the code, which must be placed in a class definition, otherwise it cannot be compiled:

int storage (String s) {
return S.length () * 2;
}

This method tells us how many bytes are needed to hold the information in a particular string (each character in the string is 16 bits, or 2 bytes, and a long integer to provide support for Unicode characters). The type of the argument is string and is called S. Once you pass s to a method, you can treat it as if it were another object (you can send a message to it). Here, we call the length () method, which is one of the methods of string. The method returns the number of characters in a string.
Using the example above, you can also learn about the return keyword. It mainly does two things. First of all, it means "leave the method, I have finished". Second, assuming that the method generates a value, the value is immediately after the return statement. In this case, the return value is generated by evaluating the expression "s.length () *2".
You can return any type as you wish, but if you do not want to return anything, you can indicate that the method returns void (empty). Some examples are listed below.

Boolean flag () {return true;}
Float Naturallogbase () {return 2.718;}
void Nothing () {return;}
void Nothing2 () {}

If the return type is void, then the only function of the returns keyword is to exit the method. So once you arrive at the end of the method, the keyword is not needed. Can be returned from one method anywhere. However, assuming that a non void return type has been specified, the compiler will ensure that we return the correct type regardless of where it is returned.
So far, you've probably got the impression that a program is just a collection of objects, whose methods use other objects as their own variables and send messages to those objects. This statement is generally correct, but through later study, we will also know how to make decisions in a way, do some more detailed grass-roots work. As for this chapter, it is sufficient to understand the message transmission.

Related Article

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.