Java Arrays Tutorial (3)

Data types have a specific set of values. A byte cannot hold a value larger than 127 and an int cannot hold a value larger than 2,147,483,647. You can also create your own data types that have a finite set of legal values. A programmer created data type with a fixed set of values is an enumerated data type.

In Java, you create an enumerated data type in a statement that uses the keyword enum, an identifier for the type, and a pair of curly braces that contain a list of the enum constants, which are the allowed values for the type. For example, the following code creates an enumerated type named Month that contains 12 values:

Enum Month{JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,NOV,DEC};

After you create an enumerated data type, you can declare variables of that type. For example, you might declare the following:

Month birthMonth;

You can assign any of the enum constants to the variables. Therefore, you can code a statement such as the following:

birthMonth = Month.MAY;

An enumeration type like Month is a class, and its enum constants act like instantiated from the class, including having access to the methods of the class.

Creating an enumeration type provides you with several advantages:

(1). If you did not create an enumerated type for month values, you could use another type, for example, ints or Strings. The problem is that any value could be assigned to an int or String variable, but only the 12 allowed values can be assigned to a Month.

(2). If you did not create an enumerated type for month values, you could create another type to represent months, but invalid behavior could be applied to the values. For example, you could add or substract two months, which is not logical. Programmers say using enums makes the values type-safe. Type-safe describes a data type for which only appropriate behaviors are allowed.

(3). The enum constants provide a form of self-documentation. Someone reading your program might misinterpret what 9 means as a month value, but there is less confusion when you use the identifier OCT.

(3). As with other classes, you can also add methods and other fields to an enum type.

Java Arrays Tutorial (3)的更多相关文章

  1. Java Arrays.sort源代码解析

    前提: 当用到scala的sortWith,发现: def sortWith(lt: (A, A) ⇒ Boolean): List[A] // A为列表元素类型 根据指定比较函数lt进行排序,且排序 ...

  2. json:JSONObject包的具体使用(JSONObject-lib包是一个beans,collections,maps,java arrays和xml和JSON互相转换的包)

    1.JSONObject介绍 JSONObject-lib包是一个beans,collections,maps,java arrays和xml和JSON互相转换的包. 2.下载jar包 http:// ...

  3. Java 11 Tutorial

    Java 11 Tutorial 参考 https://blog.csdn.net/sihai12345/article/details/82889827 原文 https://winterbe.co ...

  4. Java NIO Tutorial

    Java NIO Tutorial     Jakob JenkovLast update: 2014-06-25

  5. java Arrays.asList用法

    java Arrays.asList用法 用途 Arrays是java容器相关操作的工具类,asList方法将Array转换为list,是Array和List之间的桥梁. 注意 Arrays.asLi ...

  6. Java Arrays.sort相关用法与重载

    Java Arrays.sort() Java中的数组排序函数, 头文件 import java.util.Arrays; 相关API Arrays.sort(arys[]) Arrays.sort( ...

  7. Top 10 Methods for Java Arrays

    作者:X Wang 出处:http://www.programcreek.com/2013/09/top-10-methods-for-java-arrays/ 转载文章,转载请注明作者和出处 The ...

  8. Java Arrays类进行数组排序

    排序算法,基本的高级语言都有一些提供.C语言有qsort()函数,C++有sort()函数,java语言有Arrays类(不是Array).用这些排序时,都可以写自己的排序规则. Java API对A ...

  9. JAVA Arrays.binarySearch

    转自:http://blog.csdn.net/somebodydie/article/details/8229343 package com.jaky; import java.util.*; pu ...

随机推荐

  1. Codeforces 492B B. Vanya and Lanterns

    Codeforces  492B   B. Vanya and Lanterns 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid= ...

  2. BZOJ 1691: [Usaco2007 Dec]挑剔的美食家( 平衡树 )

    按鲜嫩程度排个序, 从大到小处理, 用平衡树维护价值 ---------------------------------------------------------------------- #i ...

  3. BZOJ 1599: [Usaco2008 Oct]笨重的石子( 枚举 )

    直接枚举 ------------------------------------------------------------------------------- #include<cst ...

  4. 调试带有源代码的DLL文件

    工作环境:dll源代码是c,在Visual studio 2010中调试. 第一步,调试的准备. 用C#语言编写一个测试dll文件的程序,由于dll源程序是c的,且运行结果是黑屏的,所以C#代码也是运 ...

  5. Python每日一练(3):爬取百度贴吧图片

    import requests,re #先把要访问URL和头部准备好 url = 'http://tieba.baidu.com/p/2166231880' head = { 'Accept': '* ...

  6. python成长之路17

    一:web框架的本质,对于所有的Web应用,本质上其实就是一个socket服务端,用户的浏览器其实就是一个socket客户端. 1.1:python实现: #!/usr/bin/env python ...

  7. python笔记之字符串

    列表,元组,字符串的相互转换: 将字符串转换为序列和元组: >>> s="hello" >>> list(s)['h', 'e', 'l', ' ...

  8. poj 2661 Factstone Benchmark

    /** 大意: 求m!用2进制表示有多少位 m! = 2^n 两边同时取对数 log2(m!) = n 即 log2(1) + log2(2)+log2(3)+log2(4)...+log2(m) = ...

  9. AOI自动光学检测机技术在电路板检查中的应用

    1.简述 AOI技术在许多不同的制造业领域使用,自从电子影像技术开始发展,就被各种人利用在不同的应用领域.大家最熟悉的数字相机.数字摄影机是大家生活中最常用到的器材之一,而工业产品的生产也大量使用这些 ...

  10. 数据切分——MySql表分区概述

    定义:         表的分区指根据可以设置为任意大小的规则,跨文件系统分配单个表的多个部分.实际上,表的不同部分在不同的位置被存储为单独的表.用户所选择的.实现数据分割的规则被称为分区函数,这在M ...