Understanding java-4.9 Enum types from the beginning

Source: Internet
Author: User

In this section, let's talk a little bit about enumeration types.

1. Enum type is a data type

Package Com.ray.ch04;public class Test {public static void main (string[] args) {MyType MyType = Mytype.one; System.out.println (MyType instanceof Object);}} Enum MyType {One, both, three}

Output:

True


2. The enumeration type has rewritten the Tosting method and can be output directly.

Package Com.ray.ch04;public class Test {public static void main (string[] args) {System.out.println (Mytype.one);}} Enum MyType {One, both, three}


Output:

One


3. Have your own unique approach

Package Com.ray.ch04;public class Test {public static void main (string[] args) {for (MyType myType:MyType.values ()) {Sys Tem.out.println (mytype.tostring () + ":" + mytype.ordinal () + "");}}} Enum MyType {One, both, three}

Output:

one:0
Two:1
Three:2

4. Use in conjunction with switch

Package Com.ray.ch04;public class Test {private static int getnum (MyType MyType) {switch (MyType) {case One:return 1;case Two:return 2;case three:return 3;default:return 1;}} public static void Main (string[] args) {System.out.println (Test.getnum (Mytype.one));}} Enum MyType {One, both, three}

Output:

1


Summary: This chapter focuses on the concept and application of enumeration types.


This chapter is here, thank you.

-----------------------------------

Directory



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Understanding java-4.9 Enum types from the beginning

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.