Java Arrays Tutorial (3)
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)的更多相关文章
- Java Arrays.sort源代码解析
前提: 当用到scala的sortWith,发现: def sortWith(lt: (A, A) ⇒ Boolean): List[A] // A为列表元素类型 根据指定比较函数lt进行排序,且排序 ...
- 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:// ...
- Java 11 Tutorial
Java 11 Tutorial 参考 https://blog.csdn.net/sihai12345/article/details/82889827 原文 https://winterbe.co ...
- Java NIO Tutorial
Java NIO Tutorial Jakob JenkovLast update: 2014-06-25
- java Arrays.asList用法
java Arrays.asList用法 用途 Arrays是java容器相关操作的工具类,asList方法将Array转换为list,是Array和List之间的桥梁. 注意 Arrays.asLi ...
- Java Arrays.sort相关用法与重载
Java Arrays.sort() Java中的数组排序函数, 头文件 import java.util.Arrays; 相关API Arrays.sort(arys[]) Arrays.sort( ...
- Top 10 Methods for Java Arrays
作者:X Wang 出处:http://www.programcreek.com/2013/09/top-10-methods-for-java-arrays/ 转载文章,转载请注明作者和出处 The ...
- Java Arrays类进行数组排序
排序算法,基本的高级语言都有一些提供.C语言有qsort()函数,C++有sort()函数,java语言有Arrays类(不是Array).用这些排序时,都可以写自己的排序规则. Java API对A ...
- JAVA Arrays.binarySearch
转自:http://blog.csdn.net/somebodydie/article/details/8229343 package com.jaky; import java.util.*; pu ...
随机推荐
- vs 2010 网站项目和asp.net 应用程序引用webservice
项目需要做对接,对方提供的是一个webservice接口,自己建了个网站项目,然后添加web 引用,一切正常. 当实际编码的时候,发现一些类.方法找不到. 百思不得其解,只得对照对方提供的demo 看 ...
- SQL Identity自增列清零方法
1.使用DBCC控制台命令: dbcc checkident(表名,RESEED,0) 2.truncate table 也可将当前标识值清零 但当有外键等约束时,无法truncate表 可以先禁用外 ...
- PCB设计铜铂厚度、线宽和电流关系
以下总结了网上八种电流与线宽的关系公式,表和计算公式,虽然各不相同(大体相近),但大家可以在实际的PCB板设计中,综合考虑PCB板的大小,通过电流,选择一个合适的线宽. 一.PCB电流与线宽 PCB载 ...
- 使用SharedPreferences即时存储之后,不能即时获取到数据
在这里简介一下我所遇到的情况,由于情况非常特殊,所以我就来记录一下自己在这个方面的经历! 事由:在我所做的app中有一个视频的播放功能,因为之前做优化的时候.我听说对于视频这种比較耗费资源的应该给他独 ...
- LeetCode——Path Sum II
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
- c# .net 读取json 字符串 与序列化和反序列化json字符串
命名空间 using Newtonsoft.Json.Linq; JObject obj = JObject.Parse("json字符串");用 obj["" ...
- [GDUT 决赛]--GCD,LCM——我是好人(数论)
Description 众所周知,我是好人!所以不会出太难的题,题意很简单 给你两个数n和m,问你有多少对正整数对最大公约数是n,最小公倍数是m最后友情提供解题代码(我真是太好人了) void sol ...
- Ubuntu14.04 Y460闪屏问题解决方案
我的笔记本是联想Y460,安装了Ubuntu之后发现屏幕闪烁移位,而且在使用IDE的时候出现无法输入中文等问题,其实是显卡驱动的问题,N卡官网给的驱动不好用,尝试使用大黄蜂 参考:https://wi ...
- 面向对象程序设计-C++ Default constructor & Copy constructor& Destructor & Operator Overloading【第九次上课笔记】
先上笔记内容吧: 这次上课的内容有关 构造函数 析构函数 运算符重载 return * this 内容很细,大家好好回顾笔记再照应程序复习吧 :) #include <iostream> ...
- Android GridView增加HeaderView和FooterView的实现
Android GridView增加HeaderView和FooterView的实现 做的项目中遇到一个问题,需要实现一个页面 页面的上面是一个自定义的View和GridView,当向下滚动屏幕的时候 ...