100-Day Java Learning program 03-talking about methods

Source: Internet
Author: User

Today is the third day, Oh, today is March 30, a great day, the blogger's birthday!
Today, the main simple to understand the Java Method!
In the previous article has already mentioned the method long what kind of, perhaps some small partners will say, like the C language function is similar to (PS: Forgive me will more than once mention C language, after all, this is Bo master writing code of the Enlightenment language! )。 From the looks, it is almost, but the status can be far worse, Java is not Yan control yo! Java is called object-oriented language, the most natural status is the class and object, and C is a structured language, so in C function is so important! In Java, the position of a method is never to catch up with classes and objects, so the method cannot exist independently or independently, and must belong to a class or object . if the method uses static modification, then this method belongs to a class, also known as a static method, usually called by the class. Method name, and if it is not modified with static, the object is used. The method name is called, again, the method can only be called by class or object, can never be executed independently!!! the little buddies are going to contradict me, I tried, in the same class, you can call another method directly in one method! This is actually the case, in the same class one method calls another method, Java will give it itself to add the caller, the ordinary method with the This keyword, static method with the class name !
Let's take a look at the parameter passing of the Java Method! in Java, there is only one way to pass method parameters, which is value passing! the value of the pass means that I have a parameter, if you want my parameters, then I will not give you parameters, I put the value of the parameter copy to you, but the parameters are still in my hand! The official point of view is that a copy of the actual parameter value is passed into the method and the method itself is not affected! The following code explains the value passing!

Import java.util.*;//Value Pass demo code 1 Public classmain{ Public Static void Changenum(intNUM1,intNUM2) {inttemp = NUM1;        NUM1 = num2;        num2 = temp; System. out. println ("In the method:"); System. out. println ("NUM1 ="+num1+"; num2 ="+NUM2); } Public Static void Main(string[] args) {intNUM1 =1;intnum2 =2;        Changenum (NUM1,NUM2); System. out. println ("in main:"); System. out. println ("NUM1 ="+num1+"; num2 ="+NUM2); }}

Run results

Here you can see clearly in the method, the incoming copy is exchanged, and in main, the original parameter is unaffected!
Small partners to look at this code carefully, changenum (int num1,int num2) method with the static modification, that is, to set him as a static method, so if I remove this static will be what?

Import java.util.*;//Value Pass demo code 2The only difference between this code and code 1 is that the static before changenum (int num1,int num2) is removed Public classmain{ Public void Changenum(intNUM1,intNUM2) {//No static in front of Yo!         inttemp = NUM1;        NUM1 = num2;        num2 = temp; System. out. println ("In the method:"); System. out. println ("NUM1 ="+num1+"; num2 ="+NUM2); } Public Static void Main(string[] args) {intNUM1 =1;intnum2 =2;        Changenum (NUM1,NUM2); System. out. println ("in main:"); System. out. println ("NUM1 ="+num1+"; num2 ="+NUM2); }}

So what happens if we compile at the command line? Such as:

Oh, it went wrong, this error is a good proof that static members (with static adornments) cannot directly access non-static members (without static adornments). Because the main method adds static before it, it cannot directly access the non-static Changenum Method! Note, is not directly accessible, want to access, or can do! In the main method it is ok to write like this!

Main test = new Main();test.changenum(num1,num2);

Create an object and you're done!
You think this is a value pass? It's naïve, about value passing, and the following code:

Import java.util.*;//Value Pass demo Code 3 Public classmain{intNUM1;intnum2; Public Static void Changenum(Main test) {inttemp = TEST.NUM1;        TEST.NUM1 = test.num2;        test.num2 = temp; System. out. println ("In the method:"); System. out. println ("NUM1 ="+test.num1+"; num2 ="+TEST.NUM2); } Public Static void Main(string[] args) {Main test =NewMain (); TEST.NUM1 =1; Test.num2 =2;        Changenum (test); System. out. println ("in main:"); System. out. println ("NUM1 ="+test.num1+"; num2 ="+TEST.NUM2); }}

Let's look at the results of the operation:

This method in the main results in the same, is not the value of the pass it?
Of course, the value of the pass, this time we pass an object, is a reference variable, the last one we said, the reference variable is an address, then I now copy this address to another reference (in the method), then two references to the same object, all the same object to operate, So whether you're working in a method or doing it in the main function is the same, because they all point to the same object!
There are a few gadgets you need to know about the method:
First, the formal parameters of the method can be not fixed! If you don't know how many parameters a method has, you can add three points (...) after the last parameter type. Indicates that the programmer has lost his language and compromised because he doesn't know how many parameters there are! just like this.

import java.util.*; //parameter variable demo code  public  class  main{public  static  void  meetingpeople  (int  num,string ... name) {System. out . Print (num); for  (String temp:name) {System. out . Print (temp); }} public  static   void  main  (string[] args) {meetingpeople (3  ,  "Xiao Zhang" ,  "Xiao Li" ,  "Xiao Wang" ); }}

Operation Result:

Second, the method overloads. method overloading means that a class can contain 2 or more methods of the same name, as long as the formal parameter list is not the same. Then, in the actual invocation, according to the method name and formal parameter list can determine which method is called. For example, the following:
Import java.util.*;
Method overloading Demo Code
public class main{

public static void fangfa(){    System.out.println("我是方法1");}public static void fangfa(String say){    System.out.println(say);}public static void fangfa(int num){    System.out.println("我是方法"+num);}public static void main(String[] args){    Main test = new Main();    test.fangfa();    test.fangfa("我是方法2");    test.fangfa(3);}

}
Operation Result:

100-Day Java Learning program 03-talking about methods

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.