1.
public class Equivalence {
public static void main(String[] args){
Integer n1 = new Integer(47);
Integer n2 = new Integer(47);
System.out.println(n1==n2);
}
}
尽管n1, n2均为等于47的整型,即对象的内容是相同的,但对象的引用却是不同的,因此输出结果为
false

所以对内容的相同与否判断应该使用equals方法
System.out.println(n1.equals(n2)) 显示 true

然而,当创建了自己的类时
class Value{
int i;
}

public class EqualsMethod {
public static void main(String[] args)
{
Value v1 = new Value();
Value v2 = new Value();
v1.i = v2.i = 100;
System.out.println(v1.equals(v2));
v1 = v2;
System.out.println(v1.equals(v2));
}
}
结果仍为
false
true
这是由于equals()的默认行为是比较引用。所以除非在自己的新类中覆盖equals()方法,否则不可能表现出希望的行为。

2. Java provides the & and | operators. The & operator works exactly the same as the && operator, and the | operation works exactly the same as the || operator with one exception: the & and | operators always evaluate both operands. Therefore, & is referred to as the unconditional AND operator and | is referred to as conditional OR operator.

3. Math.random() 随机产生在[0,1)的一个双精度数

random

public static double random()
Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0. Returned values are chosen pseudorandomly with (approximately) uniform distribution from that range.

When this method is first called, it creates a single new pseudorandom-number generator, exactly as if by the expression

new java.util.Random
This new pseudorandom-number generator is used thereafter for all calls to this method and is used nowhere else.

This method is properly synchronized to allow correct use by more than one thread. However, if many threads need to generate pseudorandom numbers at a great rate, it may reduce contention for each thread to have its own pseudorandom-number generator.

Returns:
a pseudorandom double greater than or equal to 0.0 and less than 1.0.
See Also:
Random.nextDouble()

posts - 403, comments - 310, trackbacks - 0, articles - 7
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

1.20 Java notes

Posted on 2007-04-22 20:22 ZelluX 阅读(142) 评论(0)  编辑  收藏 所属分类: OOP
2007-01-20 22:38:26
只有注册用户登录后才能发表评论。


网站导航: