Java methods use

Source: Internet
Author: User

Use of no parameter-return-value method in Java
1, defining the method
Eg:public void Show () {
System.out.println ("helloworld!")
}
---method to implement a specific action in a pair of curly braces
---naming specification, first word letter lowercase, other words first letter uppercase
---Call the method, create the object first, and then manipulate it by the object name. Method Name ().
Eg:public class helloworld{
public static void Main (string[] args) {
HelloWorld Hello = new HelloWorld ();//Create object, object named Hello
Hello.show ();//Call Object
}
public void Show () {//define Method
System.out.println ("helloworld!");
}
}

N!!! Use of the non-parametric return value method in Java
Defines a method that has no parameters but returns a value of type int
public int Add () {
int A=5;int b=4;
int sum=a+b;
return sum;
}//returns a value of type int in the Add method, so you must return an integer value using return in the method body
When a method with a return value is called---, the method returns a result when it is executed, so the return value is typically received and processed when a method with a return value is called
Here are a few things to note n!!
---If the return type of the method is void, it is not possible to return a value with return in the method.
The return value of a---method can have at most one
---method return value type must be compatible with eg return type int cannot return string type

N!!! Use of no return value method with parameters in Java
Eg:public void Show (String name) {
System.out.println ("Welcome" +name+ "!");
}
HelloWorld Hello = new HelloWorld ();
Hello.show ("NSU");
The output is Welcome nsu!
---Call the parameter method, you must ensure that the number, type, and order of the arguments are the same as the formal parameters!
---The method is called, the argument does not need to specify the data type Eg:hello.show ("Welcome");
---method can be a basic data type Eg:int double, etc. can also be a reference data type String array, etc.
When there are multiple---methods, multiple parameters are separated.

N!!! Use of the return value method with a parameter in Java
Eg:public string Show (string name) {//defines a method with a parameter return value
Return "Welcome" +NAME;
}
HelloWorld hello= new HelloWorld ();
String welcome=hello.show ("NSU");//Call the method with the return value of the parameter, save the return value in the variable welcome
SYSTEM.OUT.PRINTLN (welcome);

Java methods use

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.