Methods with no parameters and return values in Java

Source: Internet
Author: User
Tags instance method

First, define a tyrants class (Localtyrant)
Property: Name Moeney smoke
Behavior One: (No arguments no return value):
Behavior Two: (No parameters have return value):
Behavior Three: (with parameters have return value):
Behavior four: (with parameter no return value):
The following examples to look carefully, and slowly analysis, must not be anxious for haste

Package cm.tr;
    Class localtyrant{String name;
    int money;
    String Somke=null; * * Type one: no parameter no return value * Format: public void Method Name () {* Method body *} */public void fun1 () {SYSTEM.OUT.PR
    INTLN (name + "Running"); 
    /* * Type two: No parameters have return value * Format: Public return value type method name () {* Method body *return Value//return value must be the same as return value type *} *
        public string fun2 () {string str = "Express";      return str; There must be return value}/* * Type three: have parameters have return value * Format: Public return value type method name (parameter 1, parameter 2, ...) {* Parameters can have multiple, in the middle separated by a comma * method body *return return value *}/Public String fun3 (int money) {If [Money &G
        T;=25) {Somke = "Furong King";
        }else{Somke = "Huangshan";
    return Somke; /* * Type four: Parameter no return value * Format: Public return value type method name (parameter 1, parameter 2, ...)  {* Method Body *}/public void Fun4 (String apple,int money) {System.out.println (name + charity) + Apple
    + "and" +money + "block Money"); } public String TOstring () {return "self-introduction:" + name + "There are a lot of" + money+ "RMB"; Type must be String, method name must be ToString, cannot be modified} public class Demo2 {public static void main (string[] args) {//To
        Do auto-generated method stub localtyrant th= new Localtyrant ();
        th.money= 100;
        Th.name = "John";
        Method One: System.out.println ("self-introduction:" + th.name + "There are many" + th.money+ "RMB");  Method Two: The ToString method must cooperate with the public String toString () System.out.println (Th.tostring ()) in the definition class.
        You can also write//system.out.println (th); This method automatically invokes the call to the ToString Method//Method One: The method th.fun1 () is invoked without a parameter return value;
        Invocation of method Two: Calling a method that has no parameter return value must receive the return value in order to output the returned object String s = th.fun2 ();
        System.out.println (s);
        Equivalent to System.out.println (Th.fun2 ());
        Method Three invocation: The invocation has a return value System.out.println (TH.FUN3 (50));
        String Apple = "iphone 6plus";
        int money = 100;
         Method type Four invocation: There is no return value th.fun4 (Apple, money); Equivalent to Th.fun4 ("Iphone6plus", 100);Must give the parameter the same type of value}} 

Run Result:
Self Introduction: John has a lot of 100 yuan
Self Introduction: John has a lot of 100 yuan
John is running.
Express
Lotus King
John Dole out iphone 6plus and 100 bucks

PS: Clarify several methods in Java, easy to confuse
instance methods, class methods, and construction methods are distinguished in Java :
class method with the static modifier
Typical main function: public static void Main (String args[]) {}
Instance method: Is the General method
Construct method: No return value (none in the Void), the method name is the same as the class name
public class test{
public static void MyMethod ();//class method
public void myMethod2 (); Instance method
public teat ();//constructor method, no return value, same method name as class name
public string GetInfo ()//This is a method that has a return value without arguments, and a method that returns a string type
}

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.