JDK5.0的新特性

JDK 5.0新特性详解与应用实战 Java Development Kit(JDK)是为Java开发者提供的一个开发工具包,其中包含了解释器/编译器、Java运行时环境(JRE)和核心库等组件。2004年,Java平台经历了重大更新,推出了JDK 5.0,也称为Java 1.5。这一版本引入了若干重要的语言和库增强,极大地改进了Java编程的便捷性和表达能力。 阅读详情

I have been writing JDK 5.0 code for over six months now, so I thought I would take some time to reflect on my experience and draw a few conclusions on the features that were introduced.

Enhanced for loop

The undisputed winner. I can't even begin to describe how good it feels to use the new for loop everywhere (well, almost everywhere). I mentally cringe the few times when I am forced to use the old for loop, typically when I need the index or that I want the Iterator to be visible outside the loop.

The code is much more readable and feels less cluttered with noise (e.g. indices when you don't need them or incrementation exposing the underlying implementation). This latter point was an unexpected benefit of the new loop, by the way. Imagine that you have:

String[] names = ...;
for (String name : names) {
  // ...
}

and you decide that you want to change the type of names to a Collection. How do you modify your code?

List<String> names = ...;
for (String name : names) {
  // ...
}

That's right, just one line. It doesn't get better than that.

Annotations

Obviously, I am partial to annotations since they are at the heart of TestNG but I am a firm believer that annotations are going to change the way we build software in Java. We have been relying for far too long on reflection hacks to introduce meta-data in our programs, and annotations are finally going to provide an excellent solution to this problem.

Also, I haven't felt the need to use some of the predefined annotations such as @Override, so I haven't formed an opinion on them yet.

It seems inescapable to me that in a couple of years, most of the Java code that we will be reading and writing will contain annotations.

Static imports

I hardly use them at all, except in my annotations type for Retention and Target. I am still not convinced that the original intent that motivated the addition of this feature (discourage the anti-pattern of implementing an interface to be able to reuse its constants without having to qualify them) justifies the introduction of a new language feature, but time will tell.

I guess that in some way, the use of an IDE in my day-to-day programming makes imports absolutely obsolete, so I can't really get myself to feel strongly about this feature anyway.

Variable-length arguments

I haven't had the need for this feature at all so far. It might come in handy once in a while but I'm really not convinced it warranted a change in the language.

Enums

While I definitely give Enums a theoretical nod of approval, I haven't really converted my code to them yet, and I haven't acquired the reflex to use them either. I believe that when I do, I'll be happy with the result and it will make my code a tad more robust.

Generics

I left the best for the end... but since this entry is getting a bit long, I will save the Generics discussion for tomorrow.

Autoboxing

I have stayed away from autoboxing so far, probably because I have a vague feeling of losing control of the performance of my code. I don't think it is justified, though, so autoboxing can come in handy and make your code a little bit more readable. I think I would encourage developers to flag their code when such autoboxing is happening, and I am pretty sure that IDE's will soon be able to do the same.

Generics

Where to start?

Well, first of all, nobody can dispute that Generics are a solid concept that tends to improve the robustness of your code. The reason why they are so usually controversial regardless of the language is because of their implementation. And for having been a member of the C++ committee for many years , I can definitely vouch for the difficulty of getting them right.

In a nutshell, I have this to say about Java generics: my code feels more robust, but it's harder to read.

So what's the problem?

Redundancy.

First of all, I have always had a hard time with the redundancy introduced by the necessity of casting in general. For example, instead of writing:

Map accounts = new HashMap();  // no generics
...
Account a = (Account) accounts.get("Cedric");

why can't I just write:

Map m = new HashMap();  // no generics
...
Account a = m.get("Cedric");

and let the compiler introduce a silent cast, since obviously, it's an object of type Account that I am trying to retrieve from the Map?
Obviously, Generics don't solve this problem entirely but they make a decent job at alleviating it somewhat. But they also make it worse in some other ways:

Map<String, List<Account>> accounts =
  new HashMap<String, List<Account>>();

Ouch.
Not only is the code significantly harder to read, but it fails to obey the DRY principle ("Don't repeat yourself"). What if I need to change the value type of this map from List<Account> Collection<Account>? I need to replace all these statements everywhere in my code. While IDE refactoring will help, it's still an awful lot of code for a modification of this kind that hardly impacts the semantics of this code.

Admittedly, there is no nice way to avoid this syntax when you are creating a new object, but what I am driving at is that I think Generics would have been better off if typedefs had been introduced with them.

Or so I thought at first.

But after thinking about it more, I realized that typedefs were the wrong solution to the problem, because simply put, they don't add anything to the use of a separate class to define your complex Generic type.

class AccountMap extends HashMap<String, List&lAccount>> {
 ...
}

Except for the fact that you need to extend an implementation (HashMap, and not Map, obviously), this solution is probably better than introducing typedef, which has its own quirks.

I haven't gone to this trouble so far, but my recommendation would be: do it if you write the type more than three times (twice in the initialization and you use it more than once in your code).

Except for this little annoyance, I am quite happy with Generics overall and I particulary enjoy reading the TestNG Javadocs so nicely typed.

Conclusion

I am very happy with the new features of JDK 5.0 and I am quite proud to have had a chance to influence it with my participation in JSR 175 and JSR 201. Like all radical evolutions, not all of the new features will be popular with everyone, but as long as most developers find some of these features useful and that backward compatibility is preserved, I think JDK 5.0 is a very solid step toward more solid Java code.



About the author

Cedric Beust cedric@beust.com
Blog: http://www.beust.com/weblog/

Cedric Beust is a Senior Software Developer on the WebLogic Server team and provides his thoughts on J2EE, Java, AOP and software development in his weblog, Otaku.  
RK3568开发板Debian系统屏幕旋转终极指南:从临时调整到永久设置 本文提供了RK3568开发板在Debian系统下实现屏幕旋转的完整解决方案。从使用xrandr命令进行临时调整,到修改X11配置文件实现永久设置,详细讲解了操作步骤与核心原理。指南还涵盖了多屏异显配置、触摸屏坐标校准等进阶技巧,帮助开发者彻底解决显示方向问题。 阅读详情

相关推荐

【FLINK+Kerberos+kafka】在华为云上 bootstrap.server使用IP的GSS initiate failed 问题处理

目前互联网上所有的资料显示都是说没有正确设置KDC之类的,但本博主实际通过java代码低版本 如使用kafka-client 0.11.0 ,2.4.0等版本手动创建kafkaconfig 或者使用华为云的LoginUtils类 都能够正常链接,所以应该是高版本添加了相关配置,或者flink在subtask 创建kafkaAdminClient时 丢弃或使用了默认的一些配置行为,导致了客户端和kdc验证之间出现了principal 不一致导致验证失败。client.dns.lookup配置项作用。

honghanyue1791的博客 574

【Java SE 代码回顾】——day06_HashMap、HashSet、增强for循环

这个系列是对javaSE课程代码的回顾,所有Demo都是亲自动手实现的,并做了小小的change,为了方便自己理解和后期复习,代码里加了大量的注释。现对所有知识点的代码进行复习整理,进一步理解、完善代码,对注释进行补充。其中重要的知识点会单独说明,但主要以代码为主。 说明:博客是对学习内容的巩固,很多注释是为了自己理解,所以不会很专业。 ------------------------------------------------------HashMap--------------------..

laozaoxiaowanzi的博客 336

NX/UG二次开发—CAM—设置程序修剪边界Boundary

最近做了一个设置CAM程序边界的小工具,分享一下经验(官方的代码在文章末尾处),以下均以修剪边界曲线为例。 以下为官方提供的源码: /***************************** Include Files **********************************/ #include <stdlib.h> #include <uf.h> #include <uf_obj.h> #include <uf_object_type

门前大桥下,游过一群鸭,快来看一看,原来是C++ 3733

【Java】jdk5.0、jdk7、jdk8、jdk11、jdk17新特性

文章目录前言一、jdk5.0新特性二、jdk7新特性三、jdk8新特性四、jdk11新特性五、jdk17新特性 前言 了解jdk新特性 一、jdk5.0新特性 泛型:允许类型或方法对各种类型的对象进行操作,同时提供编译时类型安全性。它为集合框架增加了编译时类型安全性,并消除了转换的繁琐工作 增强for循环:消除了迭代器和索引变量在迭代集合和数组时的繁琐和容易出错的弊端 自动装箱和自动拆箱:包装类包括Integer、Double、Float、Long、Short、Character、Boolean 枚举

weixin_45995287的博客 1470

Windows 11登录界面密码框消失Bug解析与解决方案

Windows操作系统登录认证机制是系统安全的第一道防线,其图形界面由桌面窗口管理器(DWM)负责渲染。当UI元素渲染异常时,会导致密码输入框等关键组件不可见。这类图形子系统故障通常源于驱动兼容性或主题服务冲突,在Windows 11最新累积更新后尤为常见。通过系统文件检查(sfc)、注册表修复或创建新用户等工程实践方法可有效解决问题。对于企业IT环境,建议采用PIN码或生物识别等替代认证方案,同时定期执行磁盘清理和系统健康检查等Windows维护操作,确保认证服务稳定运行。

weixin_33806859的博客 609

YOLO26 无损剪枝:稀疏训练 + 结构化通道裁剪

YOLO26 无损剪枝链路:YOLO26s (7.2M) → 稀疏训练 30 epoch (L1 on BN gamma, lam=0.01) → 阈值剪枝 50% (gamma<quantile(0.5)) → 结构化重建 Conv+BN → 微调 10 epoch (lr=1e-4) → YOLO26n (1.9M, mAP50 0.808, 仅降 0.6%)。核心贡献:稀疏训练让 BN gamma 趋向二元分布 (近 0 或近 1), 剪枝阈值可直接取 median 自动 50% 压缩。

m0_64661770的博客 263

java jdk5.0_Java JDK5.0新特性

JDK5.0新特性虽然JDK已经到了1.8但是1.5(5.0)的变化是最大的1.增强for循环foreach语句foreach简化了迭代器作用: 对存储对象的容器进行迭代(数组, collection, map)1> 格式增强for循环括号里写两个参数第一个是声明一个变量第二个就是需要迭代的容器for( 元素类型变量名: Collection集合& 数组) {...}2> 增...

weixin_39918128的博客 275

【项目实战】YOLOV5 +实时吸烟目标检测+手把手教学+开源全部

本原创项目长期更新,旨在完成校园异常行为实时精检测,做到集成+N次开发+优化(不止局限于调包)为止,近期将不断更新以下模型+数据+标注文件+教程。关注博主,Star 一下github,一起开始美妙的目标检测之路吧~~ 文章目录本原创项目长期更新,旨在完成校园异常行为实时精检测,做到集成+N次开发+优化(不止局限于调包)为止,近期将不断更新以下模型+数据+标注文件+教程。关注博主,Star 一下github,一起开始美妙的目标检测之路吧~~一、项目展示二、项目资源共享1:训练图片:香烟图片+吸烟手势+烟雾三、

cv君的博客 13万+

JDK 5.0新特性

JDK5.0新特性中主要包含以下方面:1.泛型(Generics)    2.增强的for 循环                  3.自动装包与自动拆包4.枚举(Enum)          5.静态导入(static import)   6.可变参数7.Printf格式输出      8.IO流输入(Scanner)          9.注解(Annotation) 注解是非

daozhongyang的专栏 1090

【SpringBoot 3.x 第92节】Spring Modulith:构建模块化单体架构(基于 Spring Boot 3),一文带你搞透(超详细)!

🏆本文收录于《滚雪球学SpringBoot 3.x》,专门攻坚指数提升,本年度国内最系统+最专业+最详细(永久更新)。    该专栏致力打造最硬核 SpringBoot3 从零基础到进阶系列学习内容,🚀均为全网独家首发,打造精品专栏,专栏持续更新中…欢迎大家订阅持续学习。 如果想快速定位学习,可以看这篇【SpringBoot3教程导航帖】,你想学习的都被收集在内,快速投入学习!!两不误。

**My Coding Family** 997

如何让 Mac mini 24小时不休眠运行 OpenClaw

如果 OpenClaw 每 5-10 分钟自动退出,launchd 连续 4 次快速重启后会判定为崩溃循环并卸载服务。:方案一(pmset)+ 方案三(launchd)组合使用,既防止系统休眠,又保证 OpenClaw 开机自启和崩溃自动恢复。:caffeinate 只在进程运行期间有效,终端关闭或进程结束后系统恢复正常睡眠策略。这条命令会自动创建 launchd plist 并启用服务,是最推荐的方式。替换为你的实际用户名路径,Node.js 路径也要改成你自己的。确保 plist 中有。

Maynor的博客 2199

JavaSE--- 设计模式、JDK5.0新特性、JDK7.0新特性

12 扩展知识 12.1 设计模式 12.1.1 装饰设计模式 1.g

Oner.wv的专栏 850

2025年华为杯中国研究生数学建模竞赛A题 通用神经网络处理器下的核内调度问题 思路+代码

针对通用神经网络处理器下的核内调度问题,我们提出了一套基于分层优化框架的系统性解决方案。该方案首先通过缓存敏感的启发式拓扑排序算法生成最小峰值缓存占用的调度序列,在满足计算图依赖关系的前提下优先选择能够释放或减少缓存占用的节点;随后采用最佳适应算法进行精细化的缓存分配,结合LRU换出策略动态管理多级缓存空间,最小化额外数据搬运量;最后引入多目标遗传算法进行联合优化,在严格控制数据搬运量不显著增加的前提下,通过关键路径分析、流水线并行和数据预取等技术进一步优化总执行时间。

YOLO 785

jdk5.0的一些新特性(1)

在此记录我学习的笔记,方便以后查阅以下简述了jdk5.0的一些新特性,还没有例举完静态导入:import static +包(好比绝对路径和相对路径的问题)例:import static java.lang.System.out;//调用打印函数则可以这样调用out.println("hello");自动装箱:可以把一个基本数据类型直接赋给对应的包装类例:Integer i=1;//实际上Inte...

IT______Rookie的博客 615

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

黑马程序员——JAVA学习笔记——Jdk5.0新特性

------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! ------- Jdk5.0新特性: 1、JAVA新特性对数组的支持 1.1   可变参数: 以往:调用一个方法时,必须根据方法的定义传递指定的参数 新特性:方法中可以接收的参数不再固定,可以随意。 定义格式: 返回值类型  方法名称(类型…参数名称){} 注意:此时向方法中传递可变参数后,其中

tubao1991的专栏 496

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

jdk5.0新特性

jdk5.0新特性 jdk 1.1 1.2 1.4 5.0 由于5.0加入了很多新特性,sun公司觉得叫jdk5.0比较合适。(对此我表示666) 新特性: 泛型、枚举、静态导入、自动拆装箱、增强for、可变参数 反射(**重要**) 结构图如下:  ...

weixin_42472048的博客 343

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

JDK5.0 新特性

JDK5.0 叫 Tiger,6.0叫海豚,7.0野马。J2SE改称为Java SE 。1.自动封箱。可以直接将基本类型装入到集合中。因为编译器做了变动,可以自动将基本类型和其对应的包装类转换。2.静态引入。例如:import static java.lang.System.*;       在程序中用到System.out.println();时可以写作:out.println();3.for循环;统一了数组和集合的遍历;数组遍历:String  ss[]= {"123","456","789"};for

zhangchangsheng_2011的专栏 407

jdk5.0的新特性

jdk的版本在1.4后变化很大,所以叫jdk5.0 下面是总结jdk5.0的新特性: (1)泛型(***) 泛型简介   泛型是J2SE 5.0最重要的特性。他们让你写一个type(类或接口)和创建一个实例通过传递一个或多个引用类型。这个实例受限于只能作用于这些类型。比如,在java 5,java.util.List 已经被泛化。当建立一个list对象时,你通过传递一个java类型...

weixin_30668887的博客 95

《JDK5.0新特性培训》

课程目录 第1节:JDK5.0泛型入门 第2节:JDK5.0泛型入门(续) 第3节:JDK5.0泛型方法 第4节:JDK5.0泛型类型的继承规则 第5节:JDK5.0增强的for循环 第6节:JDK5.0自动封包和解包 第7节:JDK5.0静态导入 第8节:JDK5.0可变参数 第9节:JDK5.0枚举入门 第10节:JDK5.0枚举的应用 第11

xo_zhang的专栏 1044

Jdk5.0新特性(转贴)

[color=olive]JDK1.5 令我们期待很久,可是当他发布的时候却更换版本号为5.0。这说明Java已经有大幅度的变化。本文将讲解JDK5.0支持的新功能-----Java的泛型. [/color] [color=green]请下载附件[/color]...

lkfnn的专栏 112

JDK5.0新特性

JDK5中新增了很多新的java特性,利用这些新语法可以帮助开发人员编写出更加清晰,安全,高效的代码。静态导入自动装箱/拆箱增强for循环可变参数枚举泛型JDK 1.5 增加的静态导入语法用于导入类的某个静态属性或方法。使用静态导入可以简化程序对类静态属性和方法的调用。

棉花糖老丫 1903

Java学习笔记22 jdk5.0新特性

Jdk5.0中出现新特性: 包括:泛型、自动装箱、拆箱、枚举、静态导入 1. 泛型(Genercics)泛型就是变量类型的参数表示。 2. 增强型for循环,简化了集合的遍历:语法如下: For(type element :array) {    System.out.println(element); } 3. 自动装箱/拆箱(autoboxing/unboxing) 自动

雪地车的博客 292
上一篇: 提高应用程序安全性应该考虑的10个方面
andyjava2006
博客等级 码龄20年 1粉丝 9原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值