JAVA learning lesson 3 (functions and their overloading)

Source: Internet
Author: User

JAVA learning lesson 3 (functions and their overloading)
Switch () applies to byte, short, int, char

If break is not added in case, it will be executed all the time, know}, or break.

For example:

public class Main {public static void main(String[] args){//int x = 5, y = 4,xx = 6;char a = 'f';switch(a){default :System.out.println("d");case 'a':System.out.println("a");case 'b':System.out.println("b");break;case 'c':System.out.println("c");break;}//System.out.println("56");}}


The execution result is d, a, and c.


Difference between if and switch
If:
1. determine the specific value
2. Determine the interval
3. Determine if the operation result is a boolean expression.


Switch:
1. determine the specific value
2. The number of values is fixed.
PS: For a few fixed values, it is best to use the switch, because the switch will load the specific answer into the memory at a time, the efficiency is relatively high
Switch is not commonly used, has poor functionality, and is difficult to write


In windows, the carriage return is \ r \ n;
Linux system is \ n


Function:



Function Definition:


Modifier (public can be added or not added to play the role of a permission) function type function name (parameter type parameter name, parameter type parameter name)
{
Execute the statement;
Return value;
}


Import java. util. extends; public class Main {public static void main (String [] args) {using cin = new using (System. in); int n = cin. nextInt (); int a = cin. nextInt (); int ans = sum (n, a); System. out. println ("ans =" + ans); int ans1 = sub (a, n); System. out. println ("ans1 =" + ans1); cin. close ();} static int sum (int a, int B) // do not add public {return a + B;} public static int sub (int a, int B) // Add public {return a-B ;}}





Function overloading:

Different from the function name and parameter types, the function features the same as that of the execution. For example, if all values are added, all values are subtracted and the maximum value is found.

Relatively simple and the features of C ++ are basically the same


Example code:

Import org. omg. cosNaming. namingContextExtPackage. addressHelper; public class Main {public static void main (String [] args) {int ans = add (4, 5); int sum = add (5, 6, 7); System. out. println (ans); System. out. println (sum);} static int add (int x, int y) {return x + y;} static int add (int x, int y, int z) {// return x + y + z; return add (x, y) + z;/* The defined amount function can be called. Therefore, the function is overloaded, there are some repeated functions that can be called to each other */}}


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.