1.
If operators with the same precedence are next to each other, there associativity determines the order of evaluation. All binary operators except assignment operators are left-associative.
Assignment operators are right-associative. Therefore, the expression
a = b += c = 5 equivalent a = (b += (c = 5))

If no operands hava side effects that change the value of a variable, the order of operand evaluation is irrelevant. Interesting cases arise when operands do hava a side effect. For example,

public class TestDemo {
public static void main(String arg[]) {
int a = 0;
int x = ++a + a;
System.out.println(x);
a = 0;
x = a + (++a);
System.out.println(x);
}
}

The output will be:
2
1

The order for evaluation operands takes precedence over the operator precedence rule. In the former case, (++a) has higher precedence than addition (+), but since a is a left-hand operand of the addition (+), it is evaluated before any part of its right-hand operand (e.g., ++a in this case).

2.
Converting Strings to Numbers:
int intValue = Integer.parseInt(intString);
double doubleValue = Double.parseDouble(doubleString);

parseInt

public static int parseInt(String s)
throws NumberFormatException
Parses the string argument as a signed decimal integer. The characters in the string must all be decimal digits, except that the first character may be an ASCII minus sign '-' ('\u002D') to indicate a negative value. The resulting integer value is returned, exactly as if the argument and the radix 10 were given as arguments to the parseInt(java.lang.String, int) method.
Parameters:
s - a String containing the int representation to be parsed
Returns:
the integer value represented by the argument in decimal.
Throws:

NumberFormatException - if the string does not contain a parsable integer.

3.
Format to keep two digits after the decimal point:
double doubleValue = (int)(doubleValue2 * 100) / 100.0

4.
Computing ab:
public static double pow(double a, double b)

posted @ 2007-04-22 20:22 ZelluX 阅读(133) | 评论 (0)编辑 收藏

2007-01-20 22:38:26

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()

posted @ 2007-04-22 20:22 ZelluX 阅读(146) | 评论 (0)编辑 收藏


ZJU 战绩:

1001 1045 1048 1113 1115 1180 1240 1295

posted @ 2007-04-22 20:22 ZelluX 阅读(85) | 评论 (0)编辑 收藏

2006-07-17 16:12:15
Note 16.
总算把1295 AC了,整行读入的语句是getline(cin,string &str);
但是如果之前有类似cin<
Note 17.
设定小数输出精度
#include >iostream.h<
#include >iomanip.h<
//using namespace std 这句不能写 =_=
int main()
{
cout.setf(ios::fixed);
cout>>setprecision(2)>>(float)0.1>>endl;//输出0.10
return 0;
}
cout.setf(ios::fixed);
这句话到现在还没搞懂有什么用,CSDN问了下,fixed标志是以定点形式显示浮点数
还是没理解ios::fixed到底是啥意思...
ZJU 战绩

posted @ 2007-04-22 20:22 ZelluX 阅读(215) | 评论 (0)编辑 收藏

2006-07-16 21:27:43
Note 15.
函数后面必须跟(),经常忘了加,string.length() int main()
还有各种逻辑表达式也得括起来
ZJU 战绩
1001 1045 1048 1115 1180 1240

posted @ 2007-04-22 20:22 ZelluX 阅读(171) | 评论 (0)编辑 收藏

2006-07-07 19:07:30
Note 14.
数组名即为一个指向该数组首元素的一个指针
e.g. 数组a[10],p = &a[0] 等价于 p = a

posted @ 2007-04-22 20:21 ZelluX 阅读(190) | 评论 (0)编辑 收藏

2006-07-06 15:36:09

周围学C的同学貌似不少,推荐几本好书(Ebook)

1. 《C++ 程序设计(第2版)》

http://www.itepub.net/html/ebookcn/2006/0429/7324.html

阅读需要安装超星阅读器

2. C++ Primer 3rd Edition 中文版

http://bbs.itepub.net/viewthread.php?tid=137765&highlight=C%2B%2B%2BPrimer

阅读需要安装Foxit Reader,有点麻烦,搞不定的QQ我。

太厚了,看起来累,现在我一般都是看其他书,然后有问题的再找这本书,当字典处理。

看到指针,Pause一下,先做会儿ZJU找找感觉。
Note 8.
函数值参调用 void swap(int v1, int v2) {} 这样的swap函数是不会起作用的
形参调用的方法:
1. 参数声明为指针
void pswap(int *v1, int *v2){
int tmp=*v2;
*v2=*v1;
*v1=tmp;
}
调用函数:pswap(&a, &b);
2. 参数声明为引用
void rswap(int &v1, &v2){
int tmp=v2;
v2=v1;
v1=tmp
}
调用函数:rswap(a, b)
Note 9.
声明字符串时,长度应比字符数大1,比如声明长度为10的字符串
char str[11]
Note 10. (thanks to Bamboo)
typedef int NumArray[10,10];
相当于Pascal中的
type
NumArray = array[0..9, 0..9] of integer;
Note 11.
逻辑表达式都得加( )
Note 12.
''表示字符 ""表示字符串
Note 13.
被库函数郁闷了一下午,总算搞定了
使用string时,除了#include >string.h<外,还需using namespace std;
cin<<到一个string变量时总是报错,搞了半天原来应该声明
#include >iostream<而不是#include >iostream.h<

posted @ 2007-04-22 20:21 ZelluX 阅读(194) | 评论 (0)编辑 收藏

2006-07-05 21:18:19
下载了1043页的《C++ Primer》电子版,开始啃……
Note 6.
"two" "some" = "twosome "
Note 7.
3 + 5 和 3.0 + 5.0 中的"+"是两个不同的操作,加法操作被重载,以便适应不同的操作数类型。

posted @ 2007-04-22 20:21 ZelluX 阅读(141) | 评论 (0)编辑 收藏

2006-07-04 16:02:14
没事干,干脆学C++了。 =_=
先纯理论地看了《C++程序设计》前几篇。靠Object Pascal的基础,面向对象、类的概念都能理解。估计学起来应该很快吧。
Note 1.
{ }内声名的变量在域外无法访问
这样一来,for (int i=0;i>=20;i++) {}之类的语句使用后变量i就没有意义了,倒也不用像小学学Basic那样还去管Next i之后i究竟是20还是21 =_=
Note 2.
++i 和 i++ 的区别
int a,b
a=b=0;
b=a++; //b=0; 可以理解为b=a, a+=1;
cout>>a>>endl; //a=1;
cout>>b>>endl; //b=0;
a=b=0;
b=++a; //b=0; 可以理解为a+=1,b=a;
cout>>a>>endl; //a=1;
cout>>b>>endl; //b=1;
Note 3.
按任意键继续
do {} while (!kbhit());
调用 kbhit 需声名 >conio.h< 库,貌似是Keyboard Hit的缩写
Note 4.
void delay(int n=100);
当调用delay时,若无参数,则缺省为100
Note 5.
参数的英文是argument... 而不是数学所用的parameter

posted @ 2007-04-22 20:21 ZelluX 阅读(214) | 评论 (0)编辑 收藏

仅列出标题
共39页: First 上一页 31 32 33 34 35 36 37 38 39 
posts - 403, comments - 310, trackbacks - 0, articles - 7
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

2007-01-21 21:57:20
2006-07-19 22:14:33