J2SE 5.0 的角落

1,dynamic cast

类似C++的dynamic_cast<T*>操作符,C#的as操作符,Java 5.0提供了安全的dynamic cast功能,不同的是它以类库的形式提供的,并且类型不匹配时是要抛异常的,大大降低了可用性:

Class.cast

public T cast(Object obj)
Casts an object to the class or interface represented by this Class object.  
Parameters:
obj - the object to be cast
Returns:
the object after casting, or null if obj is null
Throws:
ClassCastException - if the object is not null and is not assignable to the type T.
Since:
1.5

2,返回值协变

interface SomeInterface{
    Object get();
}

class CovariantImpl implements SomeInterface{
    public String get(){
        return "covariant";
    }
}

public class TestUntitled2 extends TestCase {
    public void testCovariant() {
        SomeInterface obj = new CovariantImpl();
        Assert.assertEquals("covariant", obj.get());
    }
}

3,类型安全的代理

类似只读代理Collections.unmodifiableXXX(), 同步代理Collections.synchronizedXXX(), J2SE 5.0提供了类型安全的代理:Collections.checkedXXX()

4,Arrays.deepEquals()

不愧是放在java.util包里的
 

5,皇帝的“generic”新衣

  • 加了一堆眼花缭乱的概念,却受“擦除法”实现方式所累,目前个人用到的最实用的功能仅仅是避免部分强制类型转换

  • 因为要擦除,所以无法特化,只能使用继承加实例化模拟,彻底扼杀根据实参类型进行自动分派的一切想法

  • 因为要擦除,所以与类型参数(TypeVariable)有关的一切运行期计算,如强制类型转换,都毫无意义

  • 要想在泛型类方法里调用类型参数(TypeVariable)的某个业务方法,则类型参数必须<? extends SomeSuper>,一个推论就是<? extends MarkerInterface>在<MarkerInterface>面前毫无意义

  • 讽刺的是,摆明了将问题扔给编译器解决,编译器却除了帮你强转,塞些TypeVariable信息外几乎没干什么事

  • Class.forName()后,一切都不安全了

6,基于返回值的类型推导

不知算不算“擦除法”带来的特性 

例一、Collections.emptyXXX()

emptyList

public static final <T> List<T> emptyList()
Returns the empty list (immutable). This list is serializable.

This example illustrates the type-safe way to obtain an empty list:

     List<String> s = Collections.emptyList();
 
Implementation note: Implementations of this method need not create a separate List object for each call. Using this method is likely to have comparable cost to using the like-named field. (Unlike this method, the field does not provide type safety.)

 

Since:
1.5
See Also:
EMPTY_LIST

例二、一步一次推导

但如果其返回值用于另外一个函数的泛型参数,则必须用临时变量过渡一下,可能是因为不能同时进行两步推导

public class ConditionParser {
    private ConditionParser(){
    }

    public static <T> Condition<T> deserialize(String condition){
        ....
    }
}

List< Condition<T> > conditions = new ArrayList< Condition<T> >();
Condition<T> safe = ConditionParser.deserialize(condition.ToXML());
conditions.add(safe);//ok
 

List< Condition<T> > conditions = new ArrayList< Condition<T> >();
conditions.add(ConditionParser.deserialize(condition.ToXML()));//error

 

7,类型安全的Varargs

其实是数组的简写形式,因此是类型安全的,带来方便的同时,尚未发现有什么副作用

8,serialVersionUID

到今天了还用实现来暴露意图,殆也,用Annotation来实现也比现在顺眼一点,眼睁睁的看着它混在业务数据对象中

9,面向对象的enum

区别于C++中的enum,J2SE 5.0中的enum是符合“UML有限子类”情形的面向对象的实现(尽管看起来像“有限实例”):可以实现接口,可以有构造函数,可以有方法,成员,除了不能继承和被继承,构造函数必须私有,其它的和普通Java类差不多

(to be continue...)
(The Java Programming Language Notes )

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值