Java Learning (17th), automatic packing and unpacking, enumeration type, java Enumeration

Source: Internet
Author: User

Java Learning (17th), automatic packing and unpacking, enumeration type, java Enumeration

Automatic packing and unpacking

Sometimes it is necessary to convert basic types such as int to reference type objects;

Automatic Packing of basic data types. The unboxing feature is a new feature provided by J2SE 5.0, which provides convenience for packaging basic data types, but also provides convenience while hiding details, we recommend that you use it again when you can distinguish between the basic data type and the reference type;

An example of automatic packing:

Integer I = 10; equivalent to Integer I = new Integer (10 );

During compilation, the compiler determines whether to perform automatic packing Based on the context. In the preceding example, variable I references an Integer class instance;

The same action applies to basic data types such as boolean, byte, short, char, long, float, and double, the corresponding packaging types are Boolean, Byte, Short, Character, Long, Float, and Double respectively;

 

J2SE 5.0 can also be automatically split, that is, the basic data type information in the object is automatically taken out;

For example:

1 Integer m = 10; 2 Int n = m; 3 // equivalent to n = m. intValue ();

After the m variable is automatically packed into an Integer instance, if it is assigned to an int Type Variable n, it is automatically converted to the int type and then assigned a value;

During computation, you can also perform automatic packing and unpacking;

For example:

1 Integer I = 10; 2 System. out. println (I + 10); 3 System. out. println (I ++ );

1 public class AutoBoxDemo {2 public static void main (String [] args) {3 int I = 10; 4 double d = 10.5; 5 6 // assign the basic type to the reference type 7 // the basic type will be automatically packed 8 Integer num1 = I; 9 Double num2 = d; 10 System during compilation. out. println (num1); 11 System. out. println (num2); 12 13 // assign the package class (reference type) to the basic type 14 // 15 int j = num1 will be automatically split; 16 // int j = num1.intValue (); 17 double dd = num2; 18 // int dd = num2.doubleValue (); 19 System. out. println (j); 20 System. out. println (dd); 21 22 Object o = I; 23 System. out. println (o); 24 Integer B = (Integer) o; 25 System. out. println (B); 26} 27}

Enumeration type

1 public enum Color {2 RED, BLUE, BLACK, YELLOW, GREEN; 3}

Enmu is similar to a special class. In fact, the type defined by enum is a class;

These classes are subclasses of The Enum class in the class library (java. lang. Enum <E>), and they inherit many useful methods in Enum;

The enumerated values are all public static final, that is, constants. Therefore, the enumerated values in the enumeration class should all be capitalized;

The enumeration type is class. There are constructors, methods, and fields in the enumeration type, but the enumeration constructors are very different;

The constructor is called only when constructing enumeration values;

The constructor is private and cannot have a public constructor;

You can use the enumerated values in the switch statement;

1 public class EnumDemo {2 public static void main (String [] args) {3 System. out. println (Color. RED); 4 Color [] colors = Color. values (); 5 for (Color c: colors) {6 System. out. println ("loop:" + c); 7} 8 9 System. out. println (Person. p1); 10 Person [] persons = Person. values (); 11 for (Person p: persons) {12 System. out. println ("loop:" + p); 13} 14 15 Person p = Person. p3; 16 switch (p) {17 case P1: System. out. println ("switch:" + Person. p1); break; 18 case P2: System. out. println ("switch:" + Person. p2); break; 19 case P3: System. out. println ("switch:" + Person. p3); break; 20} 21} 22} 23 24 // when jvm loads and uses enumeration classes, multiple enumerated objects will be created in advance for external classes to use 25 // public static final Color RED = new Color (); 26 // public static final Color BLUE = new Color (); 27 // public static final Color YELLOW = new Color (); 28 enum Color {29 RED, BLUE, YELLOW; // The value of the enumeration type must appear as the first statement 30 // public31 cannot be used // The number of calls is the same as the number of enumerations 32/* public */private Color () {33 System. out. println ("constructor"); 34} 35 36/* public String toString () {37 return "aa "; 38} */39} 40 41 // public static final Person P1 = new Person ("James", 30 ); 42 // public static final Person P2 = new Person ("", 20); 43 // public static final Person P3 = new Person ("", 18 ); 44 enum Person {45 P1 ("Zhang San", 30), P2 ("Li Si", 20), P3 ("Wang Wu", 18); 46 private String name; 47 private int age; 48 private Person (String name, int age) {49 this. name = name; 50 this. age = age; 51} 52 public String toString () {53 return name + "--" + age; 54} 55}

 

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.