3.5 Overview of method overloads and basic usage

Source: Internet
Author: User

/* Requirements: The number of requests and the constant change in demand, corresponding to provide a number of ways to sum. But the name is not the same. Also requires the method name to do: see name know. But, obviously, it's not done. In this case: the function of the method is the same, the parameter list is different, in order to see the name, Java allows them to play the same name. In fact, this situation has a professional noun: method overloading. Method overloading: In the same class, the method name is the same, and the parameter list is different. Is independent of the return value type. The parameter list is different: A: The number of parameters is different B: The parameter types are different */class functiondemo4 {public static void main (string[]  args)  {// JVM will invoke different function System.out.println (sum (10, 20)) according to different parameters; System.out.println (sum (10, 20, 30)); System.out.println (sum (10, 20, 30, 40)); System.out.println (sum (10.5f, 20f));}   Demand 1: Two numbers and Public static int sum (int a, int b)  {system.out.println (" int "); return a + b;} Requirement 2: Three and/*public static int sum1 (INT A,INT B,INT C)  {return a  + b + c;} */public static int sum (int a, int b, int c)  {return a  + b + c;} Requirement 3: Four numbers and/*public static int sum2 (Int a,int b,int c,int d)  {return a + b + c + d;} */public static int sum (int a, int b, int c, int d)  { Return a + b + c + d;} Public static float sum (float a, float b)  {system.out.println ("float"); Return a + b;}}

/* Compare two data for equality. The parameter types are two byte types, two short types, two int types, two long types, and are tested in the Main method */class functiontest6 {public static  void main (String[] args)  {//  test byte b1 = 3;byte b2 = 4 ; System.out.println ("Byte:"  + compare (B1, B2));//  test short s1 = 5;short  s2 = 5; System.out.println ("Short:"  + compare (S1, S2));//  two self-test}// byte types public  Static boolean compare (byte a, byte b)  {system.out.println ("byte"); Return a  == b;}  short type Public static boolean compare (short a, short b)  { System.out.println ("short"); return a == b;}  int type Public static boolean compare (int a, int b)  { SYSTEM.OUT.PRINTLN ("int"); return a == b;}  long type Public static boolean compare (long a, long b)  {System.Out.println ("Long"); return a == b;}} 


3.5 Overview of method overloads and basic usage

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.