Experience 19--JDK new Features-variable parameters, enumerations

Source: Internet
Author: User
Tags constant control characters modifier

1. Variable Parameters

Starting with JDK 1.5, Java allows you to define parameters with variable parameter lengths, allowing you to specify an indeterminate number of formal parameters for a method, or add three points after the type of the last formal parameter when you define the method (...). Is between the variable type and the variable name, and there is no space before and after it, it indicates that the parameter can accept more than one argument value and that multiple parameter values are passed in as an array

The deformable parameter can only be at the end of the formal parameter list, so a method can have at most one variable length of a parameter; When you call a method that contains a deformable parameter, you can pass in multiple parameters or an array for that parameter, and when you call a method of a variable parameter, The compiler implicitly creates an array for the variable parameter, accessing the variable parameter as an array in the method body

Code Case:

packagecom.java.test;

Publicclass changetest {

/**

* @param args

*/

publicstaticvoid Main (string[] args) {

Call the sum method, passing parameters

Integer sum = SUM (1,2,3,4,5,6);

SYSTEM.OUT.PRINTLN (sum);

Integer sum1 = sum1 (1,2,3,4,5,6);

System.out.println (SUM1);

Calling the sum argument, passing the array method

Integer[] arr = {7,8,9,0,4};

sum = SUM (arr);

SYSTEM.OUT.PRINTLN (sum);

}

publicstatic Integer sum (Integer...arr) {//... There is no space before and after can

int sum = 0;

for (Integer I:arr) {

sum + = i;

}

return sum;

}

publicstatic Integer sum1 (Integerj,integer...arr) {//Main method The first number is passed to the inside when it is passed to J, and the last one is passed to arr.

int sum = 0;

for (Integer I:arr) {

sum + = i;

}

return sum;

}

}

Test the Class Arrays.aslist () method with variable parameters in the JDK. The case of passing multiple parameters and arrays; Note: The problem of passing in an array of basic data types .

Case Study:

packagecom.java.test;

importjava.util.Arrays;

importjava.util.List;

Publicclass aslist {

/**

* @param args

*/

publicstaticvoid Main (string[] args) {

List List = Arrays.aslist ("1", "2", "3");

SYSTEM.OUT.PRINTLN (list); Results: [1, 2, 3]

integer[] num = {1,2,3,4};

List list1 = arrays.aslist (num);

System.out.println (List1); Results: [1, 2, 3, 4]

Here the NUM1 values 1, 2, 3, 4 are not objects, int[] NUM1 is an object, because arrays in Java are also objects

int [] Num1 = {1,2,3,4};

List List2 = arrays.aslist (NUM1);

System.out.println (LIST2); Results: [[I@B5DAC4]

}

}

2. Enumeration

Why do I need enumerations?

Some methods at run time, it needs the data can not be arbitrary, but must be a range of values, such problems in JDK5 before the use of custom with the enumeration function of the class solution, JAVA5 can be directly used to solve the enumeration.

not to enumerate cases:

packagecom.java.student;

Publicclass Student {

Private Stringname;

Private Stringgrade;

Public String GetName () {

return name;

}

publicvoid setname (String name) {

this. Name = name;

}

Public String Getgrade () {

return grade;

}

publicvoid Setgrade (String grade) {

if (!grade.matches ("[ABCDE]")) {

thrownew IllegalArgumentException ("The input score does not meet the requirements.") ");

}

this. Grade = Grade;

}

}

L JDK 5 The new enum keyword is used to define an enumeration class.

L Implement enumeration classes manually:

• Hide the builder by private

• Use publicstatic final modifier for all possible instances of this class

• Attributes should not be allowed to be altered, so you should use the private final modifier

Case:

packagecom.java.test;

Publicclass Student {

Private Stringname;

Private Gradegrade;

Public String GetName () {

return name;

}

publicvoid setname (String name) {

this. Name = name;

}

Public Grade Getgrade () {

return grade;

}

publicvoid setgrade (Grade Grade) {

this. Grade = Grade;

}

}

class grade{

Private Grade () {}

publicstaticfinal Gradea = new Grade ();

publicstaticfinal Gradeb = new Grade ();

publicstaticfinal Gradec = new Grade ();

publicstaticfinal graded = new Grade ();

publicstaticfinal Gradee = new Grade ();

}

publicvoid test1 () {

Student s = new Student ();

S.setname ("join");

S.setgrade (GRADE.A);

}

This is more cumbersome, Java provides a more convenient way----enumeration classes can solve the above problem; the enum keyword is used to define an enumeration class enumeration class as a special form of Java class .

L Enumeration class and ordinary class difference:

• The enumeration class defined with the enum inherits the Java.lang.Enum class by default

• The constructor of an enumeration class can only use private access control characters

• All instances of an enumeration class must be explicitly listed (, delimited, terminated ) in an enumeration class. the listed instance system automatically adds the public static final decoration

L JDK5 The switch statement, which, in addition to receiving int, Byte, char, short, can also receive an enumeration type

L If an enumeration has only one member, it can be implemented as a single instance pattern

L An enumeration class object's properties should not be allowed to be altered, so the private final adornment should be used; the attributes of the class should be assigned to them in the constructor ; the enumeration class explicitly defines a constructor with a parameter, then the incoming parameter must be corresponding when the enumeration value is listed

The enumerated classes declared in Java are children of the Java.lang.Enum class, and inherit all the methods of the enum class. Common methods:

• Name (): Returns the name of this enumeration constant, declaring it in its enumeration declaration

ordinal (): Returns the ordinal number of an enumerated constant (its position in the enumeration declaration, where the initial constant ordinal is 0).

valueof (Class enumclass, String name): Returns an enumerated constant of the specified enumeration type with the specified name. static Method

values () Although this method is not found in the JDK documentation, each enumeration class has this method, which is convenient for traversing all enumerated values of the enumeration class.

Enumerate class Advanced (in-step) cases:

Simple:

packagecom.java.student;

Publicclass Student2 {

Private Stringname;

Private Grade1grade;

Public String GetName () {

return name;

}

publicvoid setname (String name) {

this. Name = name;

}

Public Grade1 Getgrade () {

return grade;

}

publicvoid Setgrade (Grade1 grade) {

this. Grade = Grade;

}

}

enum grade1{

A,b,c,d,e;

}

enumeration class with constructor-parameter cases:

packagecom.java.student;

Publicclass Student3 {

Private Stringname;

Private Grade2grade;

Public String GetName () {

return name;

}

publicvoid setname (String name) {

this. Name = name;

}

Public Grade2 Getgrade () {

return grade;

}

publicvoid Setgrade (Grade2 grade) {

this. Grade = Grade;

}

}

enum Gr

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.