Java fourth Lesson, method

Source: Internet
Author: User
Tags modifier

Method:in order to improve the reusability of code, it can be defined as a separate functionis composed of modifier, return value, method name, parameter list, method body five parts. 1* no parameter no return value:public class Test {//no parameter no return value public static void Main (string[] args) {test t=new test (); T.show ();} public void Show () {System.out.println ("cool Bright is the most handsome");}}----- non-parametric return valuepublic class Test {//no parameter return value public static void Main (string[] args) {test t=new test (); int sum=t.su (); System.out.println ("Sum of two numbers:" +sum);} public int su () {int i=5;int j=12;int sum=i+j; return sum;}}There is no return value for the parameterpublic class Test {//Parameter no return value public static void Main (string[] args) {test t=new test (); T.su (34,56); }public void su (int java,int html) {double avg= (java+html)/2.0; System.out.println ("Average divided into:" +avg);}}The return value of the ginseng bandImport Java.util.Arrays; public class Test {//parameter with return value public static void Main (string[] args) {test t=new test (); int[] Scorse={79,53,98,81};int count =t.sort (Scorse); System.out.println ("A total of" +count+ "scores");} public int sort (int[] scorse) {arrays.sort (scorse); System.out.println (arrays.tostring (Scorse)); return scorse.length; } }//Use method in the same class, to be called by the method name. //methods can be called each other. //Call external method in static method, external method must use static//Accessing external variables in static methods, also with static modifiersIf the return value type of the function is void, the Returan statement can be omitted from the write. return: the End Function. End Function. when passing parameters in a function call in Java, the principle of passing is followed;The basic type of data passed is the data value itself. A reference type passes a reference to an object, not the object itself. 'recursive operation of the method (Fibonacci sequence)F (0) =0,f (1) =1,f (n) =f (n-1) +f (n-2)n>=2,n belong to a positive integer;ask for the value of F (5)? overloads of the method:A method that contains two or more than two method names, the same number of arguments, or different types of methods, is called an overload of the method. has no relation to the modifier or return value of the method. Construction Method: is a method that is defined in Java to initialize an object, with the same method name as the class name and no return value. overloading of constructor methods: multiple methods with the same method name, but different arguments, and calling the appropriate method depending on the parameter. The construction method can not only assign values to the properties of the object, but also guarantee a reasonable value to the property of the object. when all objects are created, they need to be initialized before they can be used, with targeted Constructorsfeatures of the default constructor:* conform to the characteristics of the constructor* Non-parametric* No method body* There is at least one default constructor for a class* If Class A does not use the public adornment, the constructor created by the compiler also does not have a public adornment. With the public adornment, the compiler creates a constructor that is also decorated with the publicwhat does creating an object do in memory? Person p=new person ();1, first load the Person.class file in the specified location on the hard disk into memory. 2, executes the main method, in the stack memory to open up the Mian method of space (stack-to-stack), and then in the main method of the stack area assigned a variable p3, an entity space is created in the inner layer of the heap, and a memory header address value is assigned a new4, the spatial allocation of attributes in the entity space, and the default initialization5, the display initialization of the properties in the space. 6, the construction code block initialization of the entity,7, the constructor of the entity is called, and the constructor is initialized. 8, the first address is assigned to the P,P variable to reference the entity. (pointing to the object)

Java fourth Lesson, method

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.