JDK5.0的11个主要新特征
ArrayList<Integer> listOfIntegers; // <TYPE_NAME> is new to the syntax
Integer integerObject;
listOfIntegers = new ArrayList<Integer>(); // <TYPE_NAME> is new to the syntax
listOfIntegers.add(new Integer(10)); // 只能是Integer类型
integerObject = listOfIntegers.get(0); // 取出对象不需要转换
|
1.2 用法
J2SE 5.0目前不支持原始类型作为类型参数(type parameter)
public class GenList <T>{
private T[] elements;
private int size = 0;
private int length = 0;
public GenList(int size) {
elements = (T[])new Object[size];
this.size = size;
}
public T get(int i) {
if (i < length) {
return elements[i];
}
return null;
}
public void add(T e) {
if (length < size - 1)
elements[length++] = e;
}
}
|
public class Limited<T extends Number> {
public static void main(String[] args) {
Limited<Integer> number; //正确
Limited<String> str; //编译错误
}
}
|
1.4 泛型与异常
import java.io.*;
interface Executor<E extends Exception> {
void execute() throws E;
}
public class GenericExceptionTest {
public static void main(String args[]) {
try {
Executor<IOException> e = new Executor<IOException>() {
public void execute() throws IOException{
// code here that may throw an
// IOException or a subtype of
// IOException
}
};
e.execute();
} catch(IOException ioe) {
System.out.println("IOException: " + ioe);
ioe.printStackTrace();
}
}
}
|
1.5 泛型的通配符"?"
LinkedList list = new LinkedList();
list.add("Hi");
list.add("everyone!");
list.add("Was");
list.add("the");
list.add("pizza");
list.add("good?");
for (int i = 0; i < list.size(); i++)
System.out.println((String) list.get(i));
//或者用以下循环
//for(Iterator iter = list.iterator(); iter.hasNext(); ) {
//Integer stringObject = (String)iter.next();
// ... more statements to use stringObject...
//}
新的循环
LinkedList<String> list = new LinkedList<String>();
list.add("Hi");
list.add("everyone!");
list.add("Was");
list.add("the");
list.add("pizza");
list.add("good?");
for (String s : list)
System.out.println(s);
|
很清晰、方便,一看便知其用法
public static int add(int ... args){
int total = 0;
for (int i = 0; i < args.length; i++)
total += args[i];
return total;
}
public static void main(String[] args){
int a;
a = Varargs.add(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
System.out.println(a);
}
|
4 自动实现装箱和解箱操作(Boxing/Unboxing Conversions)
Integer intObject;
int intPrimitive;
ArrayList arrayList = new ArrayList();
intPrimitive = 11;
intObject = new Integer(intPrimitive);
arrayList.put(intObject); // 不能放入int类型,只能使Integer
|
int intPrimitive;
ArrayList arrayList = new ArrayList();
intPrimitive = 11;
//在这里intPrimitive被自动的转换为Integer类型
arrayList.put(intPrimitive);
|
5 静态导入(Static Imports)
public enum Colors {Red, Yellow, Blue, Orange, Green, Purple, Brown, Black}
public static void main(String[] args){
Colors myColor = Colors.Red;
System.out.println(myColor);
}
|
import java.util.*;
enum OperatingSystems {windows, unix, linux, macintosh}
public class EnumExample1 {
public static void main(String args[]) {
OperatingSystems os;
os = OperatingSystems.windows;
switch(os) {
case windows:
System.out.println(“You chose Windows!”);
break;
case unix:
System.out.println(“You chose Unix!”);
break;
case linux:
System.out.println(“You chose Linux!”);
break;
case macintosh:
System.out.println(“You chose Macintosh!”);
break;
default:
System.out.println(“I don’t know your OS.”);
break;
}
}
}
|
import java.util.*;
public class EnumTest
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Enter a size: (SMALL, MEDIUM, LARGE, EXTRA_LARGE) ");
String input = in.next().toUpperCase();
Size size = Enum.valueOf(Size.class, input);
System.out.println("size=" + size);
System.out.println("abbreviation=" + size.getAbbreviation());
if (size == Size.EXTRA_LARGE)
System.out.println("Good job--you paid attention to the _.");
}
}
enum Size
{
SMALL("S"), MEDIUM("M"), LARGE("L"), EXTRA_LARGE("XL");
private Size(String abbreviation) { this.abbreviation = abbreviation; }
public String getAbbreviation() { return abbreviation; }
private String abbreviation;
}
|
enum ProgramFlags {
showErrors(0x01),
includeFileOutput(0x02),
useAlternateProcessor(0x04);
private int bit;
ProgramFlags(int bitNumber) {
bit = bitNumber;
}
public int getBitNumber() {
return(bit);
}
}
public class EnumBitmapExample {
public static void main(String args[]) {
ProgramFlags flag = ProgramFlags.showErrors;
System.out.println(“Flag selected is: “ +
flag.ordinal() +
“ which is “ +
flag.name());
}
}
|
String input = JOptionPane.showInputDialog(prompt);
int n = Integer.parseInt(input);
double x = Double.parseDouble(input);
s = input;
|
Scanner in = new Scanner(System.in);
System.out.print(prompt);
int n = in.nextInt();
double x = in.nextDouble();
String s = in.nextLine();
|
10 Covariant Return Types(不晓得怎么翻译,大概是 改变返回类型)
public Object clone() { ... }
...
Employee cloned = (Employee) e.clone();
|
public Employee clone() { ... }
...
Employee cloned = e.clone();
|
11 格式化I/O(Formatted I/O)
public class TestFormat{
public static void main(String[] args){
int a = 150000, b = 10;
float c = 5.0101f, d = 3.14f;
System.out.printf("%4d %4d%n", a, b);
System.out.printf("%x %x%n", a, b);
System.out.printf("%3.2f %1.1f%n", c, d);
System.out.printf("%1.3e %1.3e%n", c, d*100);
}
}
|
输出结果为:
Conversion Character
|
Type
|
Example
|
d
|
Decimal integer
|
159
|
x
|
Hexadecimal integer
|
9f
|
o
|
Octal integer
|
237
|
f
|
Fixed-point floating-point
|
15.9
|
e
|
Exponential floating-point
|
1.59E+01
|
g
|
General floating-point (the shorter of e and f)
|
|
a
|
Hexadecimal floating point
|
0x1.fccdp3
|
s
|
String
|
Hello
|
c
|
Character
|
H
|
b
|
Boolean
|
TRUE
|
h
|
Hash code
|
42628b2
|
tx
|
Date and time
|
See Table 3-7
|
%
|
The percent symbol
|
%
|
n
|
The platform-dependent line separator
|
Conversion Character
|
Type
|
Example
|
C
|
Complete date and time
|
Mon Feb 09 18:05:19 PST 2004
|
F
|
ISO 8601 date
|
2004-02-09
|
D
|
U.S. formatted date (month/day/year)
|
02/09/2004
|
T
|
24-hour time
|
18:05:19
|
r
|
12-hour time
|
06:05:19 pm
|
R
|
24-hour time, no seconds
|
18:05
|
Y
|
Four-digit year (with leading zeroes)
|
2004
|
y
|
Last two digits of the year (with leading zeroes)
|
04
|
C
|
First two digits of the year (with leading zeroes)
|
20
|
B
|
Full month name
|
February
|
b or h
|
Abbreviated month name
|
Feb
|
m
|
Two-digit month (with leading zeroes)
|
02
|
d
|
Two-digit day (with leading zeroes)
|
09
|
e
|
Two-digit day (without leading zeroes)
|
9
|
A
|
Full weekday name
|
Monday
|
a
|
Abbreviated weekday name
|
Mon
|
j
|
Three-digit day of year (with leading zeroes), between 001 and 366
|
069
|
H
|
Two-digit hour (with leading zeroes), between 00 and 23
|
18
|
k
|
Two-digit hour (without leading zeroes), between 0 and 23
|
18
|
I
|
Two-digit hour (with leading zeroes), between 01 and 12
|
06
|
l
|
Two-digit hour (without leading zeroes), between 1 and 12
|
6
|
M
|
Two-digit minutes (with leading zeroes)
|
05
|
S
|
Two-digit seconds (with leading zeroes)
|
19
|
L
|
Three-digit milliseconds (with leading zeroes)
|
047
|
N
|
Nine-digit nanoseconds (with leading zeroes)
|
047000000
|
P
|
Uppercase morning or afternoon marker
|
PM
|
p
|
Lowercase morning or afternoon marker
|
pm
|
z
|
RFC 822 numeric offset from GMT
|
-0800
|
Z
|
Time zone
|
PST
|
s
|
Seconds since 1970-01-01 00:00:00 GMT
|
1078884319
|
E
|
Milliseconds since 1970-01-01 00:00:00 GMT
|
1078884319047
|
Flag
|
Purpose
|
Example
|
+
|
Prints sign for positive and negative numbers
|
+3333.33
|
space
|
Adds a space before positive numbers
|
| 3333.33|
|
0
|
Adds leading zeroes
|
003333.33
|
-
|
Left-justifies field
|
|3333.33 |
|
(
|
Encloses negative number in parentheses
|
(3333.33)
|
,
|
Adds group separators
|
3,333.33
|
# (for f format)
|
Always includes a decimal point
|
3,333.
|
# (for x or o format)
|
Adds 0x or 0 prefix
|
0xcafe
|
^
|
Converts to upper case
|
0XCAFE
|
$
|
Specifies the index of the argument to be formatted; for example, %1$d %1$x prints the first argument in decimal and hexadecimal
|
159 9F
|
<
|
Formats the same value as the previous specification; for example, %d %<x prints the same number in decimal and hexadecimal
|
里面都有一些很精彩的描述,中文名称就是《Java核心技术》。只有第七版才有J2SE5.0的介绍,但是第七版好像还没有中文版。本文还参考了Professional Java JDK - 5th Edition.
转载于:https://www.cnblogs.com/windchill/archive/2008/06/13/2065004.html
JDK5.0的11个主要新特征相关推荐
- JDK5.0新特性--可变参数
2019独角兽企业重金招聘Python工程师标准>>> /*** 可变参数*/ public class VariableParamater {/*** 统计2个或多个数的和*/pu ...
- JDK5.0新特性系列---目录
JDK5.0新特性系列---目录 JDK5.0新特性系列---1.自动装箱和拆箱 JDK5.0新特性系列---2.新的for循环 JDK5.0新特性系列---3.枚举类型 JDK5.0新特性系列--- ...
- JDK5.0环境下配置PKCS#11
JDK5.0环境下配置PKCS#11 发表日期:2008-1-5 新浪微博 QQ空间 QQ微博 百度搜藏 腾讯朋友 QQ收藏 百度空间 人人网 开心网 这篇文章不介绍具体的编程方法,而是针对PKCS# ...
- jdk5.0的新特性
jdk的版本在1.4后变化很大,所以叫jdk5.0 下面是总结jdk5.0的新特性: (1)泛型(***) 泛型简介 泛型是J2SE 5.0最重要的特性.他们让你写一个type(类或接口)和创建一个 ...
- 微信小程序0.11.122100版本新功能解析
微信小程序0.11.122100版本新功能解析 我的知乎专栏-小楼昨夜又秋风 https://zhuanlan.zhihu.com/oldtimes 新版本就不再吐槽了,整的自己跟个愤青似的.人老了, ...
- Spring.NET 1.3.0公布平添新特征
相信致力.net开发的人员一定对spring.net有所懂得,今天是去sping的官网是冲着spring 3.0 的,结果发现spring.net 1.3.0 刚公布,于是乎写点它更新的引见让大家伙儿 ...
- JDK5.0新特性之:泛型
文/陈刚 2005-11-09 一.前言 泛型这个词在现在的JAVA挺时髦,光从字面上你是无法知道它代表些什么东东的,所以我们还是不要从字面去理解,而是从一些实例去了解它吧. 二.泛型之前的日子 JD ...
- LIC-Fusion 2.0:基于滑动窗口法平面特征跟踪的激光雷达惯性相机里程计
点击上方"3D视觉工坊",选择"星标" 干货第一时间送达 摘要 来自商用惯性.视觉和激光雷达传感器的多模态测量的多传感器融合提供了鲁棒和精确的6自由度姿态估计, ...
- ML之FE:基于自定义数据集(银行客户信息贷款和赔偿)对比实现特征衍生(手动设计新特征、利用featuretools工具实现自动特征生成)
ML之FE:基于自定义数据集(银行客户信息贷款和赔偿)对比实现特征衍生(手动设计新特征.利用featuretools工具实现自动特征生成) 目录 基于自定义数据集(银行客户信息贷款和赔偿)对比实现特征 ...
最新文章
- 如何让我们的vmware虚拟机上网!!
- Android 缓存池工具类
- webpack 配置详解
- pat 乙级 1001 害死人不偿命的(3n+1)猜想(C++)
- java switch case怎么判断范围_【转】Java期末复习攻略!
- java支付管理有源码_java支付宝支付案例源码
- 如何检查python是否安装了pip_如何检测和删除通过pip安装的Python包?
- NASA: Seeing Jupiter(注视木星)
- 功能安全 李艳文_李艳文:智能网联全新安全问题凸显 相关自动驾驶事故逐年增加...
- 2. wordpress 友情链接的备份和导入
- VMware+WinDBG_配置_资料
- Editplus 破解
- HDRP shader 获取阴影(Custom Pass)
- thinkpad触控笔怎么用_联想yoga720触控笔怎么用
- 洛谷P3975 弦论
- html无插件播放流,浏览器无插件播放网络视频流RTSP/H264/WEB CAM
- max232c语言,串行口通信(STC89C52+MAX232):串行口通信硬件设计详解
- 一心多用多线程-线程池ThreadPoolExecutor-看这篇就够了
- 封装的含义,作用,如何封装,什么情况下进行封装?
- Java阿里云发送短信
热门文章
- Spring Boot + Log4j2 日志框架配置 (Maven)
- 《企业软件交付:敏捷与高效管理精要》——2.1 引言
- Spring AOP体系学习
- .NET Core RSA 签名和验签(密钥为 16 进制编码)
- SteinerTree模板
- hibernate 多对多注解配置
- phpeclipse+xdebug配置
- android如何使用BroadcastReceiver后台实现来电通话记录的监听并存取到sqllite数据库通过Contentprovilder实现接口...
- Reactive Extensions入门(4):Rx实战
- 在linux中emacs安装package.el