X-Spirit的陋室铭

生活有的时候面临许多选择,这些选择让人困惑。 生活有的时候没有任何选择,这个时候让人压抑。 但无论生活有没有给你选择,你只能做一件事情: 排除困难,勇往直前!

2007年8月29日星期三

3.5.1 Operators运算符之算术运算符

The usual arithmetic operators + – * / are used in Java for addition, subtraction, multiplication, and division. The / operator denotes integer division if both arguments are integers, and floating-point division otherwise. Integer remainder (sometimes called modulus) is denoted by %. For example, 15 / 2 is 7, 15 % 2 is 1, and 15.0 / 2 is 7.5.

通常的算术运算符+ – * /在Java中用来进行加,减,乘,除。当两个操作数都是整数时,运算符/表示整数除法,其他情况表示浮点除法。整数余数(有时也叫模数)用%表示。例如,15 / 2 等于 7, 15 % 2 等于 1, 而 15.0 / 2 等于 7.5。

Note that integer division by 0 raises an exception, whereas floating-point division by 0 yields an infinite or NaN result.

注意整数除法中0作除数将导致异常,而浮点除法中用0作除法将产生无穷或者NaN的结果。

There is a convenient shortcut for using binary arithmetic operators in an assignment. For example,

在一条赋值语句中有一个使用二元算术运算的捷径。例如:

x += 4;

is equivalent to

等效于

x = x + 4;

(In general, place the operator to the left of the = sign, such as *= or %=.)

(总之,就是把运算符放在等号的左边,例如*= 或者 %= 。)

NOTE注释

 

One of the stated goals of the Java programming language is portability. A computation should yield the same results no matter which virtual machine executes it. For arithmetic computations with floating-point numbers, it is surprisingly difficult to achieve this portability. The double type uses 64 bits to store a numeric value, but some processors use 80-bit floating-point registers. These registers yield added precision in intermediate steps of a computation. For example, consider the computation:

Java编程语言的一个初衷就是可移植性。对于一个运算应该产生相同的结果,无论何种虚拟机执行该运算。对于浮点数参与的算术运算,要实现这种可移植性是惊人的困难的。双精度类型的用64位存储一个数值,但是一些处理器使用的是80位浮点寄存器。这些寄存器在中等级别的计算中产生额外的精度。例如,考虑如下计算:

double w = x * y / z;

Many Intel processors compute x * y and leave the result in an 80-bit register, then divide by z and finally truncate the result back to 64 bits. That can yield a more accurate result, and it can avoid exponent overflow. But the result may be different from a computation that uses 64 bits throughout. For that reason, the initial specification of the Java virtual machine mandated that all intermediate computations must be truncated. The numeric community hated it. Not only can the truncated computations cause overflow, they are actually slower than the more precise computations because the truncation operations take time. For that reason, the Java programming language was updated to recognize the conflicting demands for optimum performance and perfect reproducibility. By default, virtual machine designers are now permitted to use extended precision for intermediate computations. However, methods tagged with the strictfp keyword must use strict floating-point operations that yield reproducible results. For example, you can tag main as

许多Intel处理器计算x*y并将结果存放于80位的寄存器中,然后除以z并最终将结果省略成64位的。那可以产生更为精确的结果,并可以避免指数溢出。但是完全使用64位计算将可能产生不同的结果。由于那个原因,起初Java虚拟机规格规定所有中间运算必须被舍掉多余的精度。数字团体厌恶这种处理方式。舍去操作不仅会引起溢出,而且由于舍去操作需要占用时间而花费了比精确计算更多的时间。由此,Java编程语言进行了升级,考虑了最适宜的性能和完美的再现性这两个互相冲突的要求。现在虚拟机设计者被许可使用扩展精度于中间级运算中。但是使用关键字strictfp标明的方法必须使用严格的浮点操作以产生有复验性的结果,例如,你可以标记main方法如下:

public static strictfp void main(String[] args)

Then all instructions inside the main method use strict floating-point computations. If you tag a class as strictfp, then all of its methods use strict floating-point computations.

于是main方法中的所有指令都采用严格的浮点运算。如果你标记一个类为strictfp,那么该类的所有方法都将使用严格的的浮点计算。

The gory details are very much tied to the behavior of the Intel processors. In default mode, intermediate results are allowed to use an extended exponent, but not an extended mantissa. (The Intel chips support truncation of the mantissa without loss of performance.) Therefore, the only difference between default and strict mode is that strict computations may overflow when default computations don't.

详细资料非常依赖于Intel处理器的表现。在默认模式下,中间结果被许可使用扩展指数,但不能使用扩展尾数。(Intel芯片支持不影响性能的情况下舍去尾数。)因此,默认模式和严格模式唯一的不同是严格计算可能引起溢出而默认模式不会。

If your eyes glazed over when reading this note, don't worry. Floating-point overflow isn't a problem that one encounters for most common programs. We don't use the strictfp keyword in this book.

如果阅读此注释时你的眼睛变得迟钝,不要紧。浮点溢出对大多数普通编程而言并不是个能遇到的问题。在本书中我们不使用strictfp关键字。(汗:你既然用不着,说这么一大堆废话干啥?害得老子翻译了足足一个小时,还稀里糊涂的。)

2007年8月28日星期二

3.4 Variables变量

 

In Java, every variable has a type. You declare a variable by placing the type first, followed by the name of the variable. Here are some examples:

在Java中,每个变量都有一个类型。你可以用把类型写在前面,后面紧跟变量名的方式来声明变量。下面是一些例子:

double salary;

int vacationDays;

long earthPopulation;

boolean done;

Notice the semicolon at the end of each declaration. The semicolon is necessary because a declaration is a complete Java statement.

注意每一个声明后面的分号。由于每个声明都是一条完整的Java语句,所以分号是必须写的。

A variable name must begin with a letter and must be a sequence of letters or digits. Note that the terms "letter" and "digit" are much broader in Java than in most languages. A letter is defined as 'A'–'Z', 'a'–'z', '_', or any Unicode character that denotes a letter in a language. For example, German users can use umlauts such as 'ä' in variable names; Greek speakers could use a p. Similarly, digits are '0'–'9' and any Unicode characters that denote a digit in a language. Symbols like '+' or '©' cannot be used inside variable names, nor can spaces. All characters in the name of a variable are significant and case is also significant. The length of a variable name is essentially unlimited.

变量名必须以字母开头的一串字母或数字序列。注意,Java中所说的“字母”和“数字”比大多数编程语言要宽泛的多。字母是指'A'–'Z', 'a'–'z', '_',或者任何在某种语言中表示某个字母的Unicode字符。例如,德国人可以在变量名中使用元音变音'ä';希腊人可以使用p。类似的,数字也是'0'–'9'或者任何在某种语言中表示一个数位的Unicode字符。诸如'+' or '©'此类的字符也不能被用于变量名,空格也不可以。所有的变量名中的字符乃至大小写都是有意义的。变量名的长度几乎是没有限制的。

TIP

 

If you are really curious as to what Unicode characters are "letters" as far as Java is concerned, you can use the isJavaIdentifierStart and isJavaIdentifierPart methods in the Character class to check.

提示

 

如果你真的像关心Java那样对于何种Unicode字符属于“字母”感到好奇的话,不妨用Character类中的isJavaIdentifierStart和isJavaIdentifierPart方法去检查。

You also cannot use a Java reserved word for a variable name. (See Appendix A for a list of reserved words.)

你不能用Java保留字作为变量名。

You can have multiple declarations on a single line:

你可以在同一行声明多个变量:

int i, j; // both are integers

However, we don't recommend this style. If you declare each variable separately, your programs are easier to read.

但是,我们并不推荐使用这种方式。如果你分别声明每一个变量,你的程序可读性更好。

NOTE

 

As you saw, names are case sensitive, for example, hireday and hireDay are two separate names. In general, you should not have two names that only differ in their letter case. However, sometimes it is difficult to come up with a good name for a variable. Many programmers then give the variable the same name of the type, such as

Box box; // ok--Box is the type and box is the variable name

Other programmers prefer to use an "a" prefix for the variable:

Box aBox;

注释

 

如你所见,命名是区分大小写的,例如,hireday和hireDay是两个不同的名字。通常,你不应该仅仅通过大小写来区分两个命名。但是,有时候很难给一个变量起一个好名字。很多程序员就用与类型名相同的名字来给变量命名,例如:

Box box; // ok--Box 是个类型而 box 是变量名

有的程序员喜欢给变量名加上一个“a”前缀:

Box aBox;

Initializing Variables变量初始化

After you declare a variable, you must explicitly initialize it by means of an assignment statement—you can never use the values of uninitialized variables. For example, the Java compiler flags the following sequence of statements as an error:

声明变量以后,你必须明确的用一个赋值语句对其初始化——你绝不能够使用一个没有初始化的变量。例如,Java编译器将会标记如下语句为一个错误:

int vacationDays;

System.out.println(vacationDays); // ERROR--variable not initialized

You assign to a previously declared variable by using the variable name on the left, an equal sign (=), and then some Java expression that has an appropriate value on the right.

给变量赋值时,你可以把先前声明好的变量名写在左边,再写一个等号(=),然后右边紧跟一些Java具有适当值的表达式。

int vacationDays;

vacationDays = 12;

You can both declare and initialize a variable on the same line. For example:

你也可以把变量的声明和初始化写在一行,例如:

int vacationDays = 12;

Finally, in Java you can put declarations anywhere in your code. For example, the following is valid code in Java:

最后要说明的,在Java中,你可以把变量的声明放在你代码的任何地方。比如,下面的代码在Java中是正确的:

double salary = 65000.0;

System.out.println(salary);

int vacationDays = 12; // ok to declare a variable here

In Java, it is considered good style to declare variables as closely as possible to the point where they are first used.

在Java中,一种比较好的方式就是声明变量以后紧接着指出该变量第一次使用的位置。

C++ NOTE

 

C and C++ distinguish between the declaration and definition of variables. For example,

int i = 10;

is a definition, whereas

extern int i;

is a declaration. In Java, no declarations are separate from definitions.

C++ 附注

 

C和C++中区分声明和定义。例如:

int i = 10;

是一个定义,而

extern int i;

是一个声明。在Java中,没有独立于定义的声明。

Constants常量

In Java, you use the keyword final to denote a constant. For example,

在Java中,你可以用关键字final来指明一个常量,例如:

public class Constants

{

public static void main(String[] args)

{

final double CM_PER_INCH = 2.54;

double paperWidth = 8.5;

double paperHeight = 11;

System.out.println("Paper size in centimeters: "

+ paperWidth * CM_PER_INCH + " by " + paperHeight * CM_PER_INCH);

}

}

The keyword final indicates that you can assign to the variable once, and then its value is set once and for all. It is customary to name constants in all upper case.

关键字final指出你可以给变量赋值一次,而其值也只能设置一次。通常习惯上将常量名全部大写。

It is probably more common in Java to want a constant that is available to multiple methods inside a single class. These are usually called class constants. You set up a class constant with the keywords static final. Here is an example of using a class constant:

在Java中,我们或许更多时候需要一个能被一个类中的多个方法使用的常量。这种常量被称之为类常量。类常量用关键字static final来设置。下面是一个使用类常量的例子:

public class Constants2

{

public static void main(String[] args)

{

double paperWidth = 8.5;

double paperHeight = 11;

System.out.println("Paper size in centimeters: "

+ paperWidth * CM_PER_INCH + " by " + paperHeight * CM_PER_INCH);

}

public static final double CM_PER_INCH = 2.54;

}

Note that the definition of the class constant appears outside the main method. Thus, the constant can also be used in other methods of the same class. Furthermore, if (as in our example) the constant is declared public, methods of other classes can also use the constant—in our example, as Constants2.CM_PER_INCH.

注意到对类常量的定义出现在了主方法的外面。正因为如此,类常量也可以被同类的其他方法访问。此外,如果(如我们的例子)常量被声明为public,那么其他类的方法也可以使用这个常量——在我们的例子中,就像Constants2中的CM_PER_INCH一样。

C++ NOTE

 

const is a reserved Java keyword, but it is not currently used for anything. You must use final for a constant.

C++ 注释

 

const是一个Java预留的关键字,但是现在不再用于任何场合。你必须使用final来定义常量。

2007年8月27日星期一

适合程序员的健身方法

前几天在DearBook上买了《人体使用手册》和《人体经络使用手册》,有感于祖国传统医学的博大精深,同时也从中学到了一些简单的健身方法。之所以说是“适合程序员的健身方法”,是因为发现这些方法基本不需要什么特殊装备,也不需要专门腾出大量时间运动,更不需要花钱去学什么健身操之类的,对我们这些不太喜欢运动,又没时间运动的程序员来说比较合适。因此摘抄下来,与众分享……

简单有效的手保健

    当感到大脑迟钝,精力不集中时,不妨把双手手指交叉地扭在一起。可能有的人把右手拇指放在上面,有的人则把左手拇指放在上面。哪只手的拇指放在上面产生的效果是各不相同的,所以某只手指在上交叉一会儿后,要换成另一只手拇指在上交叉。如果这样感觉不舒服,这是由于采用了与平时不同的动作,会给大脑一种刺激,由此可以促进大脑功能的提高。
    然后,使手指朝向自己,某只手拇指在上,从手指根部把双手交叉在一起,并使双手手腕的内侧尽量靠在一起。紧靠一会儿后,换成另一只手拇指在上交叉。这也同样会给大脑以刺激。一般交叉3秒钟左右就要松开,然后再用力地紧靠在一起,反复进行几次。
拍击手掌脑清爽

 手掌中央存在着有助于增强心脏功能,开发大脑潜力的重要部位。只要对此进行强烈刺激,大脑潜力就能得到开发,原来早上懒得起床或白天要打瞌睡的人,头脑就会变得清爽。要达到这个目的,只要强烈地拍击双手手掌就行。
 把手掌合起来拍击时会发出“嘭嘭”的声音,这个声音通过听觉神经传到大脑,可以增强大脑功能。如果早上爱睡懒觉,白天昏昏沉沉,记忆力不佳,注意力也不集中,就应该进行拍击手掌的锻炼。
 这种锻炼方法很简单。早上,如果想睡懒觉时,可以把双手向上方伸展,强烈地拍击手掌3次。接着,把向上方伸展的双手放在胸前,再拍击3次。应该注意,手腕要用力伸展,尽量使用左右手的中指牢牢地靠拢。
 这样一来,头脑的模糊和心中的烦躁都可以完全消除。早上头脑清醒,是一天最重要的起点。通过拍击手掌,就可以精力充沛地进行学习和工作,并能提高效率。
简单有效的脚保健

敲击脚底消疲劳

 每晚临睡时只要用拳头“咚咚”地敲击脚底,就可以消除一天的疲劳。
 脚底与人体器官有密切的关系,通过敲击对脚底给予适度的刺激,能促进全身血液循环,内脏功能得到增强,全身的精力也恢复了。
 正确的脚底敲击法,是以脚掌心为中心,有节奏地进行敲击,以稍有疼痛感为度。可以盘腿坐在床上或椅子上,把一只脚放在另一条腿的膝盖上进行敲击。每只脚分别敲击100次,但是不可用力过度,以免引起出血。
单脚站立强内脏

 乘坐公交车上下班时,是锻炼脚底的良好机会。锻炼方法非常简单,就是采取“金鸡独立”的姿势,踮着脚尖站立着。初时也许很不习惯,而且感到非常痛苦,那么可以先让双脚的脚后跟稍微离开地面一些,习惯以后,再踮着双脚的脚尖站立,最后过渡到踮着一只脚的脚尖站立。
 单脚站立时,可以先踮着右脚的脚尖站1-2分钟,再休息1-2分钟,然后踮着左脚的脚尖同样站立1-2分钟,反复地进行。
 单脚站立对腰部和脚部的强化作用不言而喻,而更重要的是有利于增强内脏功能。

脚尖登楼梯平血压

 除了在乘车时采用踮脚尖锻炼之外,日常生活中也可以抓住一切机会锻炼,踮着脚尖登楼梯就是一个能使人全身得到锻炼的好机会。
 踮着脚尖登楼梯,可以使血压平衡,而且精神饱满。与平地行走相比,登楼梯的运动量更大,不但可以使肌肉、呼吸器官和循环器官得到锻炼,腰部和脚部肌肉也得到增强,全身的机能都能得到改善。同时,由于尽可能踮着脚尖登楼梯,脚前掌得到锻炼,与之联系的内脏和大脑功能也会得到增强。
刷子摩脚底美白皮肤

 使皮肤白皙而细嫩是女性朋友最关心的事情。其实,只要刺激脚底,就可以使皮肤健美,方法就是在洗澡时用刷子摩擦脚底。由于人体的一切内脏都与脚底相联系,所以,通过刷子的刺激,可促进体内激素的分泌,使皮肤变得白嫩。
 实行这种保健法时,并不需要专用的刷子,只要使用一般家用的刷子就行。但是,应该选用天然纤维制成的刷子。因为天然纤维制成的刷子比较柔软,不会损伤脚底皮肤。

程序员健身宝典

楼主AutoAsm(风流总被雨打风吹去)2004-11-04 09:58:28 在 扩充话题 / 程序人生 提问

健身何需去健身房?俺来讲点心得。  
  1.   胸肌,肱三。软件公司一般都有格子间,格子间的走廊正好当双杠用,而且走廊一般比双杠宽,效果更好。呵呵,根握推销过差不多。  
  2.   前臂,肱二。程序员一般书都很多,把书打成捆,当哑铃,可以练肱二,根据个人状况,可以5公斤一捆,也可以10公斤一捆。  
  3。三角肌。   用捆好的书练侧平举和前平均。  
  4。腹肌。在公司练“双杠”的时候把腿抬平,呵呵,看看能坚持多长时间  
  差不多了,呵呵,省钱阿。  
问题点数:0、回复次数:10Top

1 楼revolkiss(revol)回复于 2004-11-04 09:59:59 得分 0

让老板看见k死你!Top

2 楼whizstorm(Popo)回复于 2004-11-04 10:17:33 得分 0

Top

3 楼Mickeylqz(月圆之夜,紫禁之巅,一剑袭来,天外飞仙)回复于 2004-11-04 10:35:50 得分 0

被轰出去啊!Top

4 楼tengxiang05(一座桥)回复于 2004-11-04 10:45:04 得分 0

没有想到书还有这用途   当哑铃Top

5 楼wujunhao_niu(我是一头牛)回复于 2004-11-04 12:46:19 得分 0

没必要这样,一般的公司都会组织一些体育活动,就看你个人喜不喜欢!Top

6 楼posedge(世界在踌躇之心的键上跑过去,奏出忧郁的乐章)回复于 2004-11-04 17:06:31 得分 0

真得很吓人Top

7 楼yuxh312(方块--正在吹竹叶笛)回复于 2004-11-04 17:56:37 得分 0

昏,小心点好Top

8 楼shadowDLL(Tomorrow is another day!)回复于 2004-11-04 18:00:01 得分 0

格子间承重不行!!Top

9 楼baobeixiong(宝贝熊)回复于 2004-11-04 23:35:54 得分 0

hehe,   smilingTop

10 楼znjq1001(追风)回复于 2004-11-07 00:22:10 得分 0

床上运动吧.呵呵

男人的健康计划从现在开始

不管你是否愿意接受这个事实,男性确实比女性寿命短。生命在于运动,也许男人比女人更加深刻地体会到这一点。做事要趁早,储蓄健康也一样。你的健康计划,从现在就要开始实行。
  20至30岁 储备健康
  20多岁也许是一个男性觉得最有精力的时候。如同女人要从20来岁开始注意保养皮肤一样,男性则应该从20岁开始注重锻炼自己的身体。
  ●你的身体变化及应对方法●
  变化1:荷尔蒙混乱
  通常在18岁至25岁,男性体内的人体生长激素和睾丸激素会急遽增长,肌肉发育将达到高峰。可惜,这个荷尔蒙的分泌高峰不会持续很长时间。22岁或23岁开始,男性体内的人体生长激素就会开始下降,以后以每10年2%—5%的速度递减,男性的肌肉力量将逐步下降。
  ●对策:趁着生长高峰,最好能把你的肌肉锻炼得越结实越好。不然的话,到了40岁你就会觉得肌肉松弛,有气无力。
  变化2:脆弱的膝盖
  约翰斯·霍普金斯大学的研究人员曾经跟踪过1321名医学院学生,发现年轻时候膝盖受过伤的人日后患关节炎的几率是没受过伤的人的三倍。
  ●对策:保持腿筋的灵活。腿筋僵硬容易引起膝盖受伤。此外,最好减少跑步的运动量,每周至多不要超过4次慢跑。可以选择多打打篮球,这样可以增加关节软骨的灵活度和弹性。伸展训练也可以强壮软骨。
  变化3:发胖
  耶鲁大学最近一项研究显示,人们普遍对肥胖人士存在偏见,认为他们懒惰、愚蠢、没有价值。事实证明,外形能直接影响你求职就业和与异性交往。年纪轻轻就失去良好的外形,恐怕很难起飞。
  ●对策:尽量选择健康均衡的饮食,保持低卡路里和足量的各种蔬菜,少喝啤酒和碳酸饮料。一定要锻炼一身结实的肌肉,特别是胸肌、臂肌和二头肌,可以多做爆发力强的力量性动作,如杠铃推举。
  锻炼套餐
  跳蹲运动———杠铃推举———双臂屈伸———举腿———压臂———抬膝(重复20次)
  每个动作重复六至八次,整套动作每天做两至三次,每周做两天。跳蹲运动所用的杠铃重量稍轻,其它动作加重。做跳蹲运动时越快越好,其它动作的频率为向上两秒,向下四秒。

2007年8月25日星期六

加油,兄弟们!先坐稳当了!

今天在回龙观见到了一帮宿舍的兄弟,还有隔壁宿舍的兄弟。十分开心。看到兄弟们都有份满意的工作,心里也很舒坦。虽然自己现在这份工作并不很满意,但是我认为自己曾经期待的开发也不一定就是唯一的出路。大学生毕业参加工作,起初一两年所谓工作经验,我觉得最重要的不是技术经验,更多的是励炼一种成熟的对待事业的心态。热情,好学是我们的优势,但浮燥,惰性却成为我们的职场之路上的一大障碍。其实不论现在做什么,最重要的是把眼下的工作拿起来,坐稳,做好,做到位。没有这样的心态,不管做开发,做测试,还是做别的,都只能是原地踏步,对自我的成长没有什么帮助。记得有个故事,一个博士生出来做清结工,有人问他,你一个博士,怎么来做这个?他的回答是:博士生就是扫厕所也要做的比别人好!
兄弟们现在有的做开发,有的做市场,有的做工程,而我做测试。但无论做什么都不要放弃学习!一是对工作经验的学习,二是对知识技能的学习。
写下上面的文字与兄弟共勉

--
X-Spirit

2007年8月23日星期四

3.3 Data Types数据类型

Java is a strongly typed language. This means that every variable must have a declared type. There are eight primitive types in Java. Four of them are integer types; two are floating-point number types; one is the character type char, used for code units in the Unicode encoding scheme (see the section on the char type); and one is a boolean type for truth values.

Java是一种强类型语言。这意味着每个变量必须声明类型。Java中有八种原始类型。其中4种是整数类型;两种是浮点数类型;一种是字符类型char,适用于Unicode编码方案中的代码单元(参见本节中的char类型小节);还有一种是用来表示真值的布尔类型。

NOTE注释

 

Java has an arbitrary precision arithmetic package. However, "big numbers," as they are called, are Java objects and not a new Java type. You see how to use them later in this chapter.

Java有一个任意精度的算术包。顾名思义,“大数”是一个Java对象,而并非新的Java类型。你将在本章后面看到如何使用它们。

Integers整数

The integer types are for numbers without fractional parts. Negative values are allowed. Java provides the four integer types shown in Table 3-1.

整数类型是四种无小数部分的数字,允许负值。Java提供表3-1所示的四种整数类型。

Table 3-1. Java Integer Types

类型

存储空间

范围(闭区间)

Int

4 字节

–2,147,483,648 to 2,147,483, 647 (超过20亿)

Short

2字节

–32,768 to 32,767

Long

8字节

–9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

Byte

1字节

–128 to 127

In most situations, the int type is the most practical. If you want to represent the number of inhabitants of our planet, you'll need to resort to a long. The byte and short types are mainly intended for specialized applications, such as low-level file handling, or for large arrays when storage space is at a premium.

大多数情况下,int类型是最实用的。如果你想表示我们星球上居民的数量,你就需要诉诸于long类型。Byte和short类型主要用于一些特殊的应用,比如较低级的文件处理,或者当存储空间非常珍贵的时候处理大的数组。

Under Java, the ranges of the integer types do not depend on the machine on which you will be running the Java code. This alleviates a major pain for the programmer who wants to move software from one platform to another, or even between operating systems on the same platform. In contrast, C and C++ programs use the most efficient integer type for each processor. As a result, a C program that runs well on a 32-bit processor may exhibit integer overflow on a 16-bit system. Because Java programs must run with the same results on all machines, the ranges for the various types are fixed.

Long integer numbers have a suffix L (for example, 4000000000L). Hexadecimal numbers have a prefix 0x (for example, 0xCAFE). Octal numbers have a prefix 0. For example, 010 is 8. Naturally, this can be confusing, and we recommend against the use of octal constants.

在Java中,整数类型的范围不取决于你运行Java代码的机器。这避免了将软件在两个平台或者在同一平台上的两个操作系统之间转移而带来的主要麻烦。与之相反的是,C和C++程序对每个处理器使用最有效的整数类型。造成的结果就是,一个C程序在一个32位处理器上运行良好而在一个16位系统中可能表现出整数溢出。因为Java程序必须保证在所有的机器上运行得到相同的结果,不同类型的范围是固定的。长整型数字有一个L后缀(例如4000000000L)。十六位数字有一个0x前缀(例如,0xCAFE)。八进制数字有一个0前缀。例如,010就是8。当然,这可能导致混淆,所以我们建议避免使用八进制数。

C++ NOTE C++注释

 

In C and C++, int denotes the integer type that depends on the target machine. On a 16-bit processor, like the 8086, integers are 2 bytes. On a 32-bit processor like the Sun SPARC, they are 4-byte quantities. On an Intel Pentium, the integer type of C and C++ depends on the operating system: for DOS and Windows 3.1, integers are 2 bytes. When 32-bit mode is used for Windows programs, integers are 4 bytes. In Java, the sizes of all numeric types are platform independent.

Note that Java does not have any unsigned types.

在C和C++中,int表示的整数类型取决于目标机。在16位处理器上,例如8086机,整数占用2字节。在32位处理器,例如Sun公司的SPARC,整数占用4字节。在Intel奔腾上,C和C++的整数类型取决于操作系统:对于DOS和Windows3.1,整数是2字节。当Windows程序使用32位模式时,整数占用4字节。在Java中,一切数字类型的大小都是平台无关的。注意,Java没有任何无符号类型。

Floating-Point Types浮点类型

The floating-point types denote numbers with fractional parts. The two floating-point types are shown in Table 3-2.

浮点类型表示的数字含有小数部分。表3-2显示了两种浮点类型。

Table 3-2. Floating-Point Types

类型

存储空间

范围

float

4字节

大约 ±3.40282347E+38F (6–7 个有效十进制位)

double

8字节

大约 ±1.79769313486231570E+308 (15个有效十进制位)

The name double refers to the fact that these numbers have twice the precision of the float type. (Some people call these double-precision numbers.) Here, the type to choose in most applications is double. The limited precision of float is simply not sufficient for many situations. Seven significant (decimal) digits may be enough to precisely express your annual salary in dollars and cents, but it won't be enough for your company president's salary. The only reasons to use float are in the rare situations in which the slightly faster processing of single-precision numbers is important or when you need to store a large number of them.

Numbers of type float have a suffix F (for example, 3.402F). Floating-point numbers without an F suffix (such as 3.402) are always considered to be of type double. You can optionally supply the D suffix (for example, 3.402D).

Double这个名字说明该类型具有两倍于float类型的精度。(有人将之称为双精度数字。)这里,大多数应用程序选择double类型。Float类型有限的精度对于许多情况都是不足的。七个有效的十进制位也许用来以美元和美分的单位表示你的年薪还是足够的,但是用来表示你公司老总的薪水就显得不够了。仅当极少数需要对单精度数字进行敏捷的处理或者需要存储大量单精度数字的时候,才有理由使用float类型。

As of JDK 5.0, you can specify floating-point numbers in hexadecimal. For example, 0.125 is the same as 0x1.0p-3. In hexadecimal notation, you use a p, not an e, to denote the exponent.

从JDK5.0起,你可以指定十六进制的浮点数字。例如,0.125就等同于0x1.0p-3。在十六进制符号中,采用p而非e来表示指数。

All floating-point computations follow the IEEE 754 specification. In particular, there are three special floating-point values:

positive infinity

negative infinity

NaN (not a number)

to denote overflows and errors. For example, the result of dividing a positive number by 0 is positive infinity. Computing 0/0 or the square root of a negative number yields NaN.

所有浮点计算遵循IEEE 754规范。特别的,有三种特殊的浮点值:

positive infinity正无穷

negative infinity负无穷

NaN (非数字)

来表示溢出和错误。例如,将一个正数除以0得到的结果就是正无穷。0/0或者负数的平方根就是NaN。

NOTE注释

 

The constants Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, and Double.NaN (as well as corresponding Float constants) represent these special values, but they are rarely used in practice. In particular, you cannot test

if (x == Double.NaN) // is never true

to check whether a particular result equals Double.NaN. All "not a number" values are considered distinct. However, you can use the Double.isNaN method:

if (Double.isNaN(x)) // check whether x is "not a number"

常量Double.POSITIVE_INFINITY、Double.NEGATIVE_INFINITY、Double.NaN(以及相应的Float常量)表示以上特殊值。但它们在实际应用中非常少用。特别的,你无法测试

if(x==Double.NaN)//永不为真

来检查实际结果是否等于Double.NaN。所有“非数字”值被归为独特的。但是你可以使用Double.isNaN方法。

if (Double.isNaN(x)) // 检测x是否是“非数字”。

CAUTION注意

 

Floating-point numbers are not suitable for financial calculation in which roundoff errors cannot be tolerated. For example, the command System.out.println(2.0 - 1.1) prints 0.8999999999999999, not 0.9 as you would expect. Such roundoff errors are caused by the fact that floating-point numbers are represented in the binary number system. There is no precise binary representation of the fraction 1/10, just as there is no accurate representation of the fraction 1/3 in the decimal system. If you need precise numerical computations without roundoff errors, use the BigDecimal class, which is introduced later in this chapter.

浮点数字在不能容忍循环错误的财政计算中是不合适的。例如,System.out.println(2.0 - 1.1) 命令打印出 0.8999999999999999, 而非你期望得到的0.9。这种循环错误是由于在二进制系统中表示浮点数而引起的。对于分数1/10没有精确的二进制表示,正如对于分数1/3没有精确的十进制表示一样。如果你需要没有循环错误的精确数值计算,请使用BigDecimal类,这将在本章稍后介绍。

The char Type char类型

To understand the char type, you have to know about the Unicode encoding scheme. Unicode was invented to overcome the limitations of traditional character encoding schemes. Before Unicode, there were many different standards: ASCII in the United States, ISO 8859-1 for Western European languages, KOI-8 for Russian, GB18030 and BIG-5 for Chinese, and so on. This causes two problems. A particular code value corresponds to different letters in the various encoding schemes. Moreover, the encodings for languages with large character sets have variable length: some common characters are encoded as single bytes, others require two or more bytes.

要理解char类型,你需要了解Unicode编码规则。Unicode是为克服传统字符编码规则的局限性而发明的。在Unicode之前,有许多标准:美国的ASCII、西欧语言的ISO 8859-1、俄语使用的KOI-8、中文使用的GB18030 和 BIG-5等等。这造成了两个问题。一个特定的码值在不同的编码规则中对应于不同的字母。此外,大字符集的语言使用的编码具有可变的长度:一些普通的字符以单字节编码,而其余的需要两个或更多字节。

Unicode was designed to solve these problems. When the unification effort started in the 1980s, a fixed 2-byte width code was more than sufficient to encode all characters used in all languages in the world, with room to spare for future expansion—or so everyone thought at the time. In 1991, Unicode 1.0 was released, using slightly less than half of the available 65,536 code values. Java was designed from the ground up to use 16-bit Unicode characters, which was a major advance over other programming languages that used 8-bit characters.

Unicode的初衷就是解决这个问题。在统一化进程始于20世纪80年代的时候,一个定长的两字节码已经足够对世界上所有语言中的所有字符进行编码,剩下的空间还可以用于将来的扩展——大概当初每个人都是这样认为的。1991年,Unicode 1.0诞生了,仅使用了全部可用65536个码值中的一半。Java被设计为完全采用16位Unicode字符,这是Java优于其他采用8位字符的一个主要优点。

Unfortunately, over time, the inevitable happened. Unicode grew beyond 65,536 characters, primarily due to the addition of a very large set of ideographs used for Chinese, Japanese, and Korean. Now, the 16-bit char type is insufficient to describe all Unicode characters.

We need a bit of terminology to explain how this problem is resolved in Java, beginning with JDK 5.0. A code point is a code value that is associated with a character in an encoding scheme. In the Unicode standard, code points are written in hexadecimal and prefixed with U+, such as U+0041 for the code point of the letter A. Unicode has code points that are grouped into 17 code planes. The first code plane, called the basic multilingual plane, consists of the "classic" Unicode characters with code points U+0000 to U+FFFF. Sixteen additional planes, with code points U+10000 to U+10FFFF, hold the supplementary characters.

不幸的是,随着时间的过去,不可避免的事情发生了。Unicode超过了65535个字符,主要是由于像中文、日文、韩文这样的大量的象形文字的加入而造成的。现在,16位char类型已经不足以描述所有Unicode字符。

我们需要使用一点术语来解释这个问题在Java中,从JDK5.0开始是如何得以解决的。一个代码点就是一个编码规则中与一个字符相关联的码值。在Unicode标准中,代码点是用16进制写成,加上U+前缀,例如U+0041就是字母A的代码点。Unicode的代码点被分成17个代码组。第一个代码组叫做基本多语言组,是由采用U+0000至U+FFFF代码点的“经典”Unicode字符组成。额外的16个代码组,代码点从U+10000至U+10FFFF,保存辅助字符。

The UTF-16 encoding is a method of representing all Unicode code points in a variable length code. The characters in the basic multilingual plane are represented as 16-bit values, called code units. The supplementary characters are encoded as consecutive pairs of code units. Each of the values in such an encoding pair falls into an unused 2048-byte range of the basic multilingual plane, called the surrogates area (U+D800 to U+DBFF for the first code unit, U+DC00 to U+DFFF for the second code unit).This is rather clever, because you can immediately tell whether a code unit encodes a single character or whether it is the first or second part of a supplementary character. For example, the mathematical symbol for the set of integers has code point U+1D56B and is encoded by the two code units U+D835 and U+DD6B. (See http://en.wikipedia.org/wiki/UTF-16 for a description of the encoding algorithm.)

UTF-16编码是一种以变长编码表示所有Unicode代码点的方法。基本多语言组中的字符以16位值表示,叫做代码单元。辅助字符以连续的代码单元对编码。这样一个编码对中的每个值就属于一个未占用的2048字节的基本多语言组范围,称作代理区域(U+D800至U+DBFF 表示第一个代码单元, U+DC00至U+DFFF表示第二个代码单元)。这是相当明智的,因为你可以立即说出一个代码单元是否对一个单字符进行编码或者它是辅助字符的第一部分还是第二部分。例如,整数集的算术符号的代码点为U+1D56B,它是由两个代码单元U+D835和U+DD6B编码而成。(参见http://en.wikipedia.org/wiki/UTF-16获得有关编码算法的描述)

In Java, the char type describes a code unit in the UTF-16 encoding.

Our strong recommendation is not to use the char type in your programs unless you are actually manipulating UTF-16 code units. You are almost always better off treating strings as abstract data types.

Java中,char类型描述UTF-16编码中的一个代码单元。

我们强烈建议在程序中避免使用char类型,除非你对UTF-16代码单元十分熟练。你几乎总是将字符串视为抽象数据类型即可。

Having said that, there will be some cases when you will encounter char values. Most commonly, these will be character constants. For example, 'A' is a character constant with value 65. It is different from "A", a string containing a single character. Unicode code units can be expressed as hexadecimal values that run from \u0000 to \uFFFF. For example, \u2122 is the trademark symbol (™) and \u03C0 is the Greek letter pi (p).

尽管如上所述,但是有时候你也会遇到char值。最常见的就是字符常量。例如,‘A’是一个值为65的字符常量。它与“A”这个仅包含一个字符的字符串不同。Unicode代码单元可被表示成从\u0000到\uFFFF的十六进制值。例如,\u2122是商标符号(™) 而 \u03C0 是希腊字母 (p).

Besides the \u escape sequences that indicate the encoding of Unicode code units, there are several escape sequences for special characters, as shown in Table 3-3. You can use these escape sequences inside quoted character constants and strings, such as '\u2122' or "Hello\n". The \u escape sequence (but none of the other escape sequences) can even be used outside quoted character constants and strings. For example,

public static void main(String\5B\5D args)

除了用\u转义符来表示Unicode代码单元的编码,还有一些特殊的转义符来表示特殊字符,如表3-3所示。你可以在用引号引起来的字符常量和字符串中使用这些转义符,例如'\u2122'或"Hello\n"。\u转义符(但其他转义符除外)甚至可以在引号引起来的字符常量和字符串外使用。例如:public static void main(String\5B\5D args)

Table 3-3. Escape Sequences for Special Characters

Escape Sequence

Name

Unicode Value

\b

退格

\u0008

\t

Tab

\u0009

\n

换行

\u000a

\r

回车

\u000d

\"

双引号

\u0022

\'

单引号

\u0027

\\

反斜线

\u005c

is perfectly legal—\u005B and \u005D are the UTF-16 encodings of the Unicode code points for [ and ].

是完全合法的——\u005B和\u005D 是“[”和“]”的Unicode代码点的UTF-16编码。

NOTE注释

 

Although you can use any Unicode character in a Java application or applet, whether you can actually see it displayed depends on your browser (for applets) and (ultimately) on your operating system for both.

尽管你可以在任何Java程序和Applet中使用Unicode字符,但实际上你能否看到这些字符还要取决于你的浏览器(对Applet而言)和你的操作系统(这是最基本的)。

The boolean Type boolean类型(也作布尔类型)

The boolean type has two values, false and true. It is used for evaluating logical conditions. You cannot convert between integers and boolean values.

boolean类型有两个值,false和true。这是用来判断逻辑条件的。你不能在整型和布尔型之间进行转换。

C++ NOTE C++注释

 

In C++, numbers and even pointers can be used in place of boolean values. The value 0 is equivalent to the bool value false, and a non-zero value is equivalent to true. This is not the case in Java. Thus, Java programmers are shielded from accidents such as

if (x = 0) // oops...meant x == 0

In C++, this test compiles and runs, always evaluating to false. In Java, the test does not compile because the integer expression x = 0 cannot be converted to a boolean value.

在C++中,数字乃至小数点都被用于替代布尔值。0值就相当于布尔值false而非0值相当于true。Java中并非如此。因此Java程序员不会遇到下面的情况:

if (x = 0) // 哇。。。意味着x==0

C++中,这个测试可以编译并运行,并且总是判断为false。在Java中,这个测试无法编译,因为整型表达式x=0不能被转换为布尔值。

2007年8月21日星期二

3.2 Comments注释

Comments in Java, like comments in most programming languages, do not show up in the executable program. Thus, you can add as many comments as needed without fear of bloating the code. Java has three ways of marking comments. The most common method is a //. You use this for a comment that will run from the // to the end of the line.

Java中的注释,和大多数编程语言中的注释一样,在可执行程序中不会显示。因此,你可以根据需要任意添加注释而不必担心导致代码膨胀。Java有三种注释方法。最普通的是用一个//符号。用这个符号来注释从//开始到一行结尾的内容。

System.out.println("We will not use 'Hello, World!'"); // is this too cute?

When longer comments are needed, you can mark each line with a //. Or you can use the /* and */ comment delimiters that let you block off a longer comment. This is shown in Example 3-1.

当需要更长的注释的时候,你可以用//来注释每一行。或者你也可以使用/*和*/这两个注释定界符来隔离一个较长的注释。如例子3-1所示

Example 3-1. FirstSample.java

1. /*

2. This is the first sample program in Core Java Chapter 3

3. Copyright (C) 1997 Cay Horstmann and Gary Cornell

4. */

5.

6. public class FirstSample

7. {

8.      public static void main(String[] args)

9.      {

10.            System.out.println("We will not use 'Hello, World!'");

11.     }

12. }

Finally, a third kind of comment can be used to generate documentation automatically. This comment uses a /** to start and a */ to end. For more on this type of comment and on automatic documentation generation, see Chapter 4.

最后,第三种注释用于自动生成文档。该注释以/**开始以*/结束。有关这种注释和自动生成文档的更多内容,请见第四章。

CAUTION注意

 

/* */ comments do not nest in Java. That is, you cannot deactivate code simply by surrounding it with /* and */ because the code that you want to deactivate might itself contain a */ delimiter.

/* */注释在Java中是不可嵌套的。也就是说,当你想要注释掉一段代码的时候,你不能简单的使用/*和*/,因为你想注释掉的代码可能就含有一个*/定界符。

3.1 A Simple Java Program一个简单Java程序

Let's look more closely at about the simplest Java program you can have—one that simply prints a message to the console window:

让我们更进一步来看一个最简单的向控制台窗口输出信息的Java程序。

 

public class FirstSample
{
      public static void main(String[] args)
      {
            System.out.println("We will not use 'Hello, World!'"); 
      }
}

It is worth spending all the time that you need to become comfortable with the framework of this sample; the pieces will recur in all applications. First and foremost, Java is case sensitive. If you made any mistakes in capitalization (such as typing Main instead of main), the program will not run.

你需要花费来熟悉这个例子的框架,这是值得的,因为这个框架将在所有应用程序中重现。首先,Java是大小写敏感的。如果你在大小写上犯了错误(例如把Main写成main),程序就不会运行。

Now let's look at this source code line by line. The keyword public is called an access modifier; these modifiers control the level of access other parts of a program have to this code. We have more to say about access modifiers in Chapter 5. The keyword class reminds you that everything in a Java program lives inside a class. Although we spend a lot more time on classes in the next chapter, for now think of a class as a container for the program logic that defines the behavior of an application. As mentioned in Chapter 1, classes are the building blocks with which all Java applications and applets are built. Everything in a Java program must be inside a class.

现在让我们一行行地来阅读这段代码。关键字public被称作访问修饰符;这些修饰符控制着这段代码对一个程序其他部分的访问权限。在第五章中我们将对访问修饰符做更多讨论。关键字class说明Java程序中的一切都是存在于类当中。尽管在下一章中我们将花费更多时间来讲述类,但现在我们不妨把类想像成一个承载描述应用行为的程序逻辑的容器。正如在第一章中提到的,类是一切Java应用程序和applet的构建模块。Java程序中的一切必须存在于类中。

Following the keyword class is the name of the class. The rules for class names in Java are quite generous. Names must begin with a letter, and after that, they can have any combination of letters and digits. The length is essentially unlimited. You cannot use a Java reserved word (such as public or class) for a class name. (See Appendix A for a list of reserved words.)

关键字class后面的就是类名。Java当中类的命名规则是相当宽松的。类名必须以字母开始,其后可以是任意字母和数字的组合。类名的长度基本上是无限的。你不可以使用Java保留字来作为类名。(保留字列表请参见附录A)

The standard naming convention (which we follow in the name FirstSample) is that class names are nouns that start with an uppercase letter. If a name consists of multiple words, use an initial uppercase letter in each of the words. (This use of uppercase letters in the middle of a word is sometimes called "camel case" or, self-referentially, "CamelCase.")

标准的命名规则是类名需是以一个大写字母开头的名词(如FirstSample这个名字中遵循的规则)。如果一个类名以多个单词组成,则每个单词使用首字母规则。(这种在一个词中间使用大写字母的用法有时又叫做“camel case”,或者形象的写作“CamelCase”)

You need to make the file name for the source code the same as the name of the public class, with the extension .java appended. Thus, you must store this code in a file called FirstSample.java. (Again, case is important—don't use firstsample.java.)

你需要使文件名和public类的名字相同,后面加上.java扩展名。因此,你需要将以上代码存储到一个文件名为FirstSample.java的文件中。(再次说明,大小写很重要,不要写成firstsimple.java)

If you have named the file correctly and not made any typos in the source code, then when you compile this source code, you end up with a file containing the bytecodes for this class. The Java compiler automatically names the bytecode file FirstSample.class and stores it in the same directory as the source file. Finally, launch the program by issuing the command:

如果你正确给文件命名并且没有在源代码中写入任何打字稿,那么当你编译这个源文件时,你将得到一个属于这个类的包含字节码的文件。Java编译器会自动把字节码文件命名为FirstSimple.class并将其存储在Java源文件的同一目录下。最后用如下命令来启动程序:

java FirstSample

(Remember to leave off the .class extension.) When the program executes, it simply displays the string We will not use 'Hello, World'! on the console.

(切记去掉.class扩展名)程序执行时,将在控制台简单输出一个字符串:We will not use ‘Hello,World’!

When you use

当你用

java ClassName

to run a compiled program, the Java virtual machine always starts execution with the code in the main method in the class you indicate. Thus, you must have a main method in the source file for your class for your code to execute. You can, of course, add your own methods to a class and call them from the main method. (We cover writing your own methods in the next chapter.)

来运行一个已经编译的程序时,Java虚拟机总是从你指定的类当中的main方法开始执行。当然你也可以在一个类中添加你自己的方法,并将之成为是main方法。(我们将在下一章中讨论编写自定义方法。)

NOTE

注意

 

According to the Java Language Specification, the main method must be declared public. (The Java Language Specification is the official document that describes the Java language. You can view or download it from http://java.sun.com/docs/books/jls.) However, several versions of the Java launcher were willing to execute Java programs even when the main method was not public. A programmer filed a bug report. To see it, visit the site http://bugs.sun.com/bugdatabase/index.jsp and enter the bug identification number 4252539. However, that bug was marked as "closed, will not be fixed." A Sun engineer added an explanation that the Java Virtual Machine Specification (at http://java.sun.com/docs/books/vmspec) does not mandate that main is public and that "fixing it will cause potential troubles." Fortunately, sanity finally prevailed. The Java launcher in JDK 1.4 and beyond enforces that the main method is public.

根据Java语言规范,main方法必须被声明为public。(Java语言规范是描述Java语言的官方文档。你可以从http://java.sun.com/docs/books/jls下载并查看)然而,有些版本的Java启动器在main方法不是public的时候也会执行Java程序。一个程序员提交了一个bug报告。请访问站点http://bugs.sun.com/bugdatabase/index.jsp 并输入bug标识号4252539来查看这个报告。然而这个bug被标记为“关闭,将不予修正”。一个Sun的工程师添加了一条解释,即Java虚拟机规范(http://java.sun.com/docs/books/vmspec)不要求main方法为public,并且“修正这个问题将导致潜在的麻烦.”幸运的是,心智健全最终获胜。JDK1.4及以上版本的Java启动器强制main方法为public。

There are a couple of interesting aspects about this story. On the one hand, it is frustrating to have quality assurance engineers, who are often overworked and not always experts in the fine points of Java, make questionable decisions about bug reports. On the other hand, it is remarkable that Sun puts the bug reports and their resolutions onto the Web, for anyone to scrutinize. The "bug parade" is a very useful resource for programmers. You can even "vote" for your favorite bug. Bugs with lots of votes have a high chance of being fixed in the next JDK release.

关于这个故事有两个有趣的方面。一方面,让那些经常过劳工作的且不专于Java的细琢的质量保证工程师对bug报告做出充满问题的决议是一件让人沮丧的事情。另一方面,值得注意的是Sun公司将bug报告和他们的决议放在网上,供大家细察。Bug检阅对程序员来说是一个很有用处的资源。你甚至可以为你最青睐的bug投票。那些票数最高的bug将会有很大的几率在下一版的JDK中被修复。

Notice the braces { } in the source code. In Java, as in C/C++, braces delineate the parts (usually called blocks) in your program. In Java, the code for any method must be started by an opening brace { and ended by a closing brace }.

注意程序中的括号{ }。和在C++中一样,在Java中,括号划分出程序中的各个部分(通常叫做程序块或代码块)。在Java中,任何方法的代码都必须以开括号{开始,以闭括号}结束。

Brace styles have inspired an inordinate amount of useless controversy. We use a style that lines up matching braces. Because whitespace is irrelevant to the Java compiler, you can use whatever brace style you like. We will have more to say about the use of braces when we talk about the various kinds of loops.

括号的使用风格引发了大量混乱的无用争论。我们使用一种风格将匹配括号排队。因为空格对Java编译器而言无关,你可以使用任何你喜欢的括号风格。我们将会在讲述各种循环的时候对括号的使用做更多的讨论。

For now, don't worry about the keywords static void—just think of them as part of what you need to get a Java program to compile. By the end of Chapter 4, you will understand this incantation completely. The point to remember for now is that every Java application must have a main method that is declared in the following way:

现在,暂时不关注关键字static void,就把他们看作是你要编译一个Java程序所必须的。在第四章最后,你将完全理解这个咒语。现在要记住的是每个Java应用程序必须有一个像下面这样定义的main方法:

public class ClassName
{
     public static void main(String[] args)
     {
           program statements
     }
}

C++ NOTE C++注释

 

As a C++ programmer, you know what a class is. Java classes are similar to C++ classes, but there are a few differences that can trap you. For example, in Java all functions are methods of some class. (The standard terminology refers to them as methods, not member functions.) Thus, in Java you must have a shell class for the main method. You may also be familiar with the idea of static member functions in C++. These are member functions defined inside a class that do not operate on objects. The main method in Java is always static. Finally, as in C/C++, the void keyword indicates that this method does not return a value. Unlike C/C++, the main method does not return an "exit code" to the operating system. If the main method exits normally, the Java program has the exit code 0, indicating successful completion. To terminate the program with a different exit code, use the System.exit method.

作为C++程序员,你知道类是什么。Java类和C++中的类相似。但是也有一些不同会使你陷入其中。例如,在Java中,所有的函数都是某个类的方法。(他们的标准术语叫做方法,而非成员函数。)因此,在Java中,你必须给main方法一个外壳类。你或许对C++中的静态成员函数也很熟悉。这是一些定义在类中但不对对象进行操作的成员函数。Java中的main方法总是静态的。最后,和在C++中一样,void关键字说明该方法不返回值。与C/C++不同的是,在C/C++中,main方法不会向操作系统返回一个“退出码”。而在Java中,如果main方法正常退出,Java程序将返回一个退出码0,以表示成功完成。要以不同的退出码终止程序,请使用System.exit()方法。

Next, turn your attention to this fragment.

下面,请注意以下这个片段:

{

     System.out.println("We will not use 'Hello, World!'");

}

Braces mark the beginning and end of the body of the method. This method has only one statement in it. As with most programming languages, you can think of Java statements as being the sentences of the language. In Java, every statement must end with a semicolon. In particular, carriage returns do not mark the end of a statement, so statements can span multiple lines if need be.

括号将方法体的开始和结束标记出来。方法中只有一个句子。和大多数编程语言一样,你可以认为Java语句就是一个语言的句子。在Java中,每个句子必须以分号结束。特别要指出的是,回车并不表示语句的结束,所以在需要的情况下语句可以跨越多行。

The body of the main method contains a statement that outputs a single line of text to the console.

main方法的方法体包含一条向控制台输出单行文本的语句。

Here, we are using the System.out object and calling its println method. Notice the periods used to invoke a method. Java uses the general syntax

这里,我们使用了System.out对象和它的println方法。注意用于调用方法的句点。Java使用一般语法:

object.method(parameters)

for its equivalent of function calls.

来进行函数调用。

In this case, we are calling the println method and passing it a string parameter. The method displays the string parameter on the console. It then terminates the output line so that each call to println displays its output on a new line. Notice that Java, like C/C++, uses double quotes to delimit strings. (You can find more information about strings later in this chapter.)

此时,我们调用println方法并传一个字符串参数给它。这个方法将该字符串参数显示在控制台中。之后就结束该输出行,因此,每个println调用都可以将其输出写入一个新行。注意到Java和C++一样,也使用双引号来划分字符串。(本章稍后将详细讨论字符串)

Methods in Java, like functions in any programming language, can use zero, one, or more parameters (some programmers call them arguments). Even if a method takes no parameters, you must still use empty parentheses. For example, a variant of the println method with no parameters just prints a blank line. You invoke it with the call

Java中的方法,像任何编程语言中的一样,可以使用零个一个或者更多参数(一些程序员习惯称之为变量)。即使有些方法没有参数,你也必须使用空圆括号。例如,不同的无参println方法只输出一个空行。你可以通过以下方法调用它:

System.out.println();

NOTE 注释

 

System.out also has a print method that doesn't add a new line character to the output. For example, System.out.print("Hello") prints "Hello" without a new line. The next output appears immediately after the "o".

System.out 也有一个不换行的print方法,例如System.out.print(“Hello”)就可以输出”Hello”而不换行。下个输出将紧跟在“o”的后面。

Core Java学习笔记声明

终于决定好好的学习Core Java了。

我的Core Java学习笔记基本上是自己把原文翻译一遍,然后对部分有价值的代码进行一些讨论。

学习笔记仅从最重要的部分开始。前两章暂不涉及。

原版书籍来源于Flyheart提供的电子书。本学习笔记连载的内容仅供自学之用,严禁用于任何商业用途,由此引起的任何后果本人概不负责。。。

由于本人水平有限,难免会有很多错误,希望大家多多捧场,多多指教。。

坚持

心情似乎总是随着外界的触动而不停变化。但是无论怎样,坚持才是最重要的。

在每一次感到自己卑微的时候,一定要想想自己为什么会卑微。自己有什么资本变得高尚了吗?

如果答案是否定的,就一定要坚持努力。

时间的付出其实并不重要,重要的是时间的利用。能够坚持不浪费一点一滴的时间,那么这些时间的付出就是值得的。

促成转变的机会尚未到来。

 

————坚持

2007年8月15日星期三

Google Earth上不可错过的15个坐标

从失事的船只到麦田圈不一而足,可能你还没有仔细的寻找过这些地标。不必遗憾错过了GoogleEarth上最有意思的部分之一,因为所有GoogleEarth里面最奇怪的地标都将一一的为您呈现。

当地人称它为荒地守护者。这是位于加拿大艾伯特的一处地理场景,从空中看起来就像是一个戴着美洲土著头巾和耳环的人头。当然,这个荒地守护者完全是大自然的鬼斧神工。GoogleEarth参数:50.010083,-110.113006

通过GoogleEarth寻找巨大和独一无二的东西,是发烧友们最热衷的事情之一。这个巨大的粉色兔子,位于意大利的PrataNevoso,是一群来自维也纳的艺术家建造起来的,据说长大200英尺。GoogleEarth参数:44.2442

毫无疑问,GoogleEarth是麦田圈最好的“朋友”。这些圈位于内华达州Beatty附近的沙漠之中。GoogleEarth参数:37.401437,-116.86773

奥普拉本人永远不可能这么大,幸好在亚利桑那有一个农民制造一个一个十英亩大的奥普拉头像迷宫,用以向这位脱口秀主持人致敬。在里面玩的人们可以告诉自己的朋友:我在奥普拉的大脑里面迷路了。GoogleEarth参数:33.225488,-111

如果你是一个情报工作者,那么GoogleEarth绝对能让大物体无所遁形。据说这是一个位于中印边境上的。控制的一片1.8平方英里土地。据说这个地方是用来训练坦克驾驶员的。GoogleEarth参数:38.265652,105.9517

如果你在GoogleEarth上待的时间比较长,你会沮丧的发现好多地方的地图分辨率都比较低。当然GoogleEarth也在逐步的更新高分辨率的图片,我们不妨先来看看澳大利亚Bondi海滩吧,在这里你甚至能看到比基尼上的标签

国家地理与GoogleEarth一道进行了一个名为AfricaMegaflyover的项目。国家地理杂志拍摄了超过500张高分辨率的图片,而GoogleEarth同样也能使用这些图片,比方说这一队在尼日利亚休整喝水的骆驼队

你住的地方停车困难么?在荷兰Westenbergstraat,似乎得把自己的汽车停在墙上…… GoogleEarth参数:52.069207,4.3139865

Google的卫星图片经常能够捕捉到地球居民的移动,例如这十个非洲大象。GoogleEarth参数:10.903497,19.93229

谁都希望能够乘坐飞机到处观光,GoogleEarth就提供了这样一个感受的机会。这张图片这是位于赞比亚和津巴布韦的维多利亚瀑布。GoogleEarth参数-17.925511,25.858223

GoogleEarth上没有广告,除非谁家的广告大到足以能够从太空中看清楚。比方说这个大大的福特标志,这是在密歇根州底特律附近发现的。GoogleEarth参数:42.302284,-83.231215

GoogleEarth有时能够拍到半空中飞行的飞机,这种照片已经发现了超过3300个,其中包括这张二战轰炸机飞过英格兰Huntingdon上空的瞬间。GoogleEarth参数:52.336392,-0.1953462

卫星的眼睛不单能够记录下人们的成就,当然也会记录下那些不成功的时间。一个例子就是上面这艘大船,翻了一半漂浮在伊拉克巴士拉附近的水面上。GoogleEarth参数:30.541634,47.825445

有些时候GoogleEarth能够很幸运的拍摄到正在发生的事件,比方说这张GoogleEarth拍摄下来的车祸现场,位置是在北达科他Bismarck郊外。  GoogleEarth参数:46.765669,-100.79274

有一些从GoogleEarth里面翻出来的照片相当的怪异,例如为什么巴黎附近的一个停车场上会有一个战斗机呢?GoogleEarth参数:48.825183,2.1985795

咱们中国的,更加神奇!
为人民服务 42°32"33.95"N, 94°19"36.80"E

排除万难去争取胜利 42°27"12.08"N, 94° 8"49.36"E

只争朝夕 42°39"33.30"N, 94°16"0.59"E

毛主席万万岁 42°39"18.85"N, 94°10"0.80"E

向斗争中学习 42°27"40.95"N, 94°14"36.80"E
祖国在我心中 39°41"43.47"N, 73°55"37.17"E
香格里拉-松赞林寺(经文) 27°52"5.10"N, 99°41"52.20"E
其中前五个在新疆哈密附近,“毛主席万万岁”在新疆喀什与吉尔吉斯斯坦接壤的边境线附近,最后一个在云南中甸市附近。
在Earth界面左上角的搜索框中录入坐标, 就可以直接看到了.
你会在 42°33'6.31"N, 94°13'6.95"E 找到柳树泉老机场,这是一个现在废弃的机场.
新的柳树泉机场位置为 43° 4'39.09"N, 92°48'29.61"E 。
卫星地图上出现的“毛主席万万岁”等5条标语信息,为原第八航校官兵在戈壁滩上创造的奇迹,这些环绕机场的“语录”地标,可以帮助飞行员保持空域位置,引导教、学员准确进出空域,地标所在区域的中心点,便是柳树泉机场。其中一角还有一条废弃跑道。因为柳树泉机场地处戈壁,附近没有明显的地物特征!飞机都是设备简单的初教机,导航设备简陋,所以就弄了语录地标。

2007年8月13日星期一

用Java向IPMSG发送消息(转)

飞鸽传书(IP Messenger,简为IPMsg)是一个小巧方便的即时通信软件,它适合用于局域网内甚至广域网间进行实时通信和文档共享。特别是在局域网内传送文件/文件夹的速度非常快!

  • IPMsg 是一款局域网内即时通信软件, 基于 TCP/IP(UDP).
  • 可运行于多种操作平台(Win/Mac/UNIX/Java), 并实现跨平台信息交流.
  • 不需要服务器支持.
  • 支持文件/文件夹的传送 (2.00版以上)
  • 通讯数据采用 RSA/Blofish 加密 (2.00版以上)
  • 十分小巧, 简单易用, 而且你可以完全免费使用它
  • 目前已有的版本包括: Win32, Win16, MacOS, MacOSX, X11, GTK, GNOME,Java 等, 并且公开源代码。

本文演示了如何使用Java的net包,向IPMSG客户端发送消息。
IPMSG Command 常量定义如下:

1 /*========== Constant Value ==========*/
 2 public static final long IPMSG_COMMASK = 0x000000ff;
 3 public static final long IPMSG_OPTMASK = 0xffffff00;
 4 public static final long IPMSG_NOOPERATION = 0x00000000;
 5 public static final long IPMSG_BR_ENTRY = 0x00000001;
 6 public static final long IPMSG_BR_EXIT = 0x00000002;
 7 public static final long IPMSG_ANSENTRY = 0x00000003;
 8 public static final long IPMSG_BR_ABSENCE = 0x00000004;
 9
10
11
12 public static final long IPMSG_BR_ISGETLIST = 0x00000018;
13 public static final long IPMSG_OKGETLIST = 0x00000015;
14 public static final long IPMSG_GETLIST = 0x00000016;
15 public static final long IPMSG_ANSLIST = 0x00000017;
16
17 public static final long IPMSG_SENDMSG = 0x00000020;
18 public static final long IPMSG_RECVMSG = 0x00000021;
19
20 public static final long IPMSG_READMSG = 0x00000030;
21 public static final long IPMSG_DELMSG = 0x00000031;
22
23 public static final long IPMSG_GETINFO = 0x00000040;
24 public static final long IPMSG_SENDINFO = 0x00000041;
25
26 // other opt
27 public static final long IPMSG_ABSENCEOPT = 0x00000100;
28 public static final long IPMSG_SERVEROPT = 0x00000200;
29 public static final long IPMSG_DIALUPOPT = 0x00010000;
30
31 // send opt
32 public static final long IPMSG_SENDCHECKOPT = 0x00000100;
33 public static final long IPMSG_SECRETOPT = 0x00000200;
34 public static final long IPMSG_BROADCASTOPT = 0x00000400;
35 public static final long IPMSG_MULTICASTOPT = 0x00000800;
36 public static final long IPMSG_NOPOPUPOPT = 0x00001000;
37 public static final long IPMSG_AUTORETOPT = 0x00002000;
38 public static final long IPMSG_RETRYOPT = 0x00004000;
39 public static final long IPMSG_PASSWORDOPT = 0x00008000;
40 public static final long IPMSG_NOLOGOPT = 0x00020000;
41 public static final long IPMSG_NEWMUTIOPT = 0x00040000;
42
43 public static final int MAXBUF = 8192;
44 /*========== end ==========*/

IPMSG收发数据包的格式(一行):

1 version(IPMSG版本):no(消息编号,可以用系统时间):user(发送消息的用户名):host(发送消息的主机名):command(上述 Command 常量,可以用 | 组合多个值):msg(消息内容)

示例(向IPMSG发送消息,需要先打开对方的IPMSG):

1 import java.io.IOException;
 2 import java.net.DatagramPacket;
 3 import java.net.DatagramSocket;
 4 import java.net.InetAddress;
 5 import java.net.SocketException;
 6 import java.net.UnknownHostException;
 7 import java.util.Date;
 8
 9 /**
10  * @author 乱 7 8 糟 http://blog.csdn.net/comstep
11 */
12 public class TestIPMSG
13 {
14 public static void main(String[] args)
15   {
16     DatagramSocket socket;
17     InetAddress address;
18
19 long IPMSG_SENDMSG = 0x00000020;
20
21     String SENDER = "乱 7 8 糟";
22     String HOST = "Localhost";
23     String MSG_CONTENT = "Hello World!";
24
25 try
26     {
27       socket = new DatagramSocket();
28       address = InetAddress.getByName("192.168.1.20");// 发送给消息的地址
29
30 /**
31        * IPMSG收发数据包的格式(一行):
32        * 
33        * version(IPMSG版本):no(消息编号,可以用系统时间):user(发送消息的用户名):
34        * host(发送消息的主机名):command(上述 Command 常量,可以用 | 组合多个值):
35        * msg(消息内容)
36        * 
37 */
38 byte[] buffer = ("1:" + new Date().getTime() + ":" + SENDER + ":" + HOST
39 + ":" + IPMSG_SENDMSG + ":" + MSG_CONTENT).getBytes();
40
41       DatagramPacket packet = new DatagramPacket(buffer, buffer.length,
42           address, 2425);
43       socket.send(packet); // 发送报文
44
45       packet = new DatagramPacket(buffer, buffer.length);
46       socket.receive(packet);// 接收回应
47
48       String message = new String(packet.getData()); // 得到报文信息
49
50       System.out.println(message); // 显示对方返回的信息
51     }
52 catch (UnknownHostException e)
53     {
54       e.printStackTrace();
55     }
56 catch (SocketException e)
57     {
58       e.printStackTrace();
59     }
60
61 catch (IOException e)
62     {
63       e.printStackTrace();
64     }
65
66   }
67
68 }
69

你可以在 SourceForge 找到开源的 IP MSG for Java
http://blog.csdn.net/comstep

补记

这两天其实一直都想写点东西。但是苦于没有时间,于是思维的闪光一次一次被打入理性的冷宫。

上周,度过了一段紧张而又忙碌的时光。

周三的时候,忙碌了一天,下班就马上去参加公司安排的英语课了。讲课的是一个中国老师。去的人不多,我去的也不算早,但是课程还是没有开始。那个老师似乎在等待更多的人光临他的课程。。。。内容主要就是语法。一时间,我发现自己的英语竟然堕落到There be句型还会用错的地步了。。。一直满足于英语六级的我似乎还不知道什么叫做退步,可是当这个残忍的事实真的到来的时候,我发现自己对自己太有自信了。。。

周四的时候,本来已经累得筋疲力尽的自己还是硬着头皮去上了公司给安排的英语课。刚一进门,就发现那个老外已经拿着笔在白板上写写画画了。这个老师就和中国老师的风格不一样。讲课的时候,明确的提出The class will start on time。这个时候我突然想到了大学里的老师。很多老师都没有守时的规矩,学生不守时,老师也跟着往后拖。但是西方人的做法就是和我们不同,在他们的逻辑里,迟到是不值得照顾的。无论你有什么事,迟到就是迟到,是个不可弥补的失误。而在我们国人的眼里,有时候,一个小小的借口就成了原谅迟到的理由。。。

周五,真的是要挺不过去了。整个人像疯了一样,拼命的Ctrl+C、Ctrl+V来完成那些就剩一点点的任务。精神表面的轻松和坚持掩盖不住内心的疲惫,那种想要加速飞行却总是在山头徘徊的感觉真的比双脚踩在热锅上还让人难受。一直都相信只要狠一点点,就能促成飞跃的我,现在深刻感到自己那种勉强振作的逻辑的可笑!

周六,稀里糊涂的一天——一顿懒觉,一顿烧烤,一顿凉皮,一场电影。。。

周日,真的非常想去找同学聚聚,可是周日还是想多多休息。中午隔壁的室友和他老婆开始下厨做饭了。只剩下可怜的自己和同屋两个人。两个“单身汉”很无奈的跑出去觅食。最后竟然跑去逛西单了。想来觉得真不可思议,周日陪没有老婆的自己上街的居然是个老婆不在身边的准单身汉!唉。。。这就是月子啊。在超市,三说要买个饭盒,可是看看那唬人的价格,心都凉到嗓子眼儿了。我说:“三,你攒钱吧。”突然不自觉的笑了出来——天啊,我们潦倒到攒钱买饭盒的地步了。虽然心里也觉得挺辛酸的,但是三的那句话真的让我重新找回了振作的感觉:每个人在他事业起步的时候都会经历一个黑暗期……

疲惫、辛酸、彷徨,但是我始终站立着,大声呼喊着我的口号,只是周围的乌云仍然将它淹没……

When fate is playing with me, I still choose the right way and continue...

2007年8月12日星期日

JavaFX写的简易Rss阅读器

 

 

http://www.oreillynet.com/onjava/blog/2007/05/javafx_first_steps_hello_onjav_1.html这篇文章的启发,加上想学习一下JavaFX,就在上面那篇文章的基础上,做了一个简易的RSS阅读器,其界面类似于Adobe Flex 2的一个demo:进入http://try.flex.org/index.cfm,找到Blog Reader的demo。

读取RSS feed并没有什么问题,使用rome:

java 代码

  1. package rssreader;   
  2. import com.sun.syndication.feed.synd.SyndEntry;   
  3. import com.sun.syndication.feed.synd.SyndFeed;   
  4. import com.sun.syndication.io.FeedException;   
  5. import com.sun.syndication.io.SyndFeedInput;   
  6. import com.sun.syndication.io.XmlReader;   
  7. import java.io.IOException;   
  8. import java.net.MalformedURLException;   
  9. import java.net.URL;   
  10. public class RssReader {   
  11. private URL url;   
  12. private Integer titleLength = 50;   
  13. public RssReader() {   
  14.     }   
  15. public SyndEntry[] load(String urlString) throws IllegalArgumentException, FeedException, IOException {   
  16. if (urlString != null && urlString != "") {   
  17. try {   
  18.                 url = new URL(urlString);   
  19.             } catch (MalformedURLException e) {   
  20.                 e.printStackTrace();   
  21. return null;   
  22.             }   
  23.         }   
  24. if (url == null) {   
  25. return null;   
  26.         }   
  27.         SyndFeedInput input = new SyndFeedInput();   
  28.         SyndFeed feed = input.build(new XmlReader(url));   
  29.         SyndEntry[] entries = (SyndEntry[]) feed.getEntries().toArray(new SyndEntry[0]);   
  30. for( SyndEntry entry : entries ) {   
  31.             entry.setTitle( entry.getTitle().length() > titleLength ? entry.getTitle().substring(0, titleLength - 3 ) + "..." : entry.getTitle() );   
  32.         }    
  33. return entries;   
  34.     }   
  35. }   

使用JavaFX构建UI:

JavaFX代码

  1. package rssreader;   
  2. import javafx.ui.*;   
  3. import javafx.ui.canvas.*;   
  4. import javax.swing.JComponent;   
  5. import com.sun.syndication.feed.synd.SyndEntry;   
  6. var reader:RssReader = READER;   
  7. class RssReaderModel {   
  8.     attribute rssUrl: String;   
  9.     attribute rssEntries: SyndEntry*;   
  10.     attribute rssContent: String?;   
  11.     attribute rssSelectedIndex: Integer;   
  12. }   
  13. var model = RssReaderModel {   
  14.     rssUrl: "http://woodstudio.javaeye.com/blog/rss_blog/alexcheng",   
  15. };   
  16. var panel = GroupPanel {   
  17. var row1 = Row {}   
  18. var row2 = Row {}   
  19. var col = Column {}   
  20.      rows: [row1, row2]   
  21.      columns: col   
  22.      content:    
  23.          [   
  24.             GroupPanel  {   
  25.                 row: row1   
  26.                 column: col   
  27. var row = Row {}   
  28. var labelCol =  new Column   
  29. var urlCol =  new Column   
  30. var butCol =  new Column   
  31.                 rows: row   
  32.                 columns: [labelCol, urlCol, butCol]   
  33.                 content:   
  34.                 [   
  35.                     SimpleLabel {   
  36.                         row: row   
  37.                         column: labelCol   
  38.                         text: "RSS URL:"
  39.                     },   
  40.                      TextField {   
  41.                         row: row   
  42.                         column: urlCol   
  43.                         columns: 60   
  44.                         value: bind model.rssUrl   
  45.                     },   
  46.                     Button {   
  47.                         row: row   
  48.                         column: butCol   
  49.                         text: "Get Entries"
  50.                         action: operation() {   
  51.                             model.rssEntries = reader.load(model.rssUrl);              
  52.                         }   
  53.                     }   
  54.                 ]   
  55.             },   
  56.             SplitPane   {   
  57.                 row: row2   
  58.                 column: col   
  59.                 orientation: HORIZONTAL   
  60.                 content:   
  61.                 [   
  62.                     SplitView  {   
  63.                         weight: 0.3   
  64.                         content:       
  65.                             Table {   
  66.                                     columns:   
  67.                                     [   
  68.                                         TableColumn {   
  69.                                             text: "Title"
  70.                                         },   
  71.                                         TableColumn {   
  72.                                             text: "Date"
  73.                                         },   
  74.                                     ]   
  75.                                     cells : bind foreach (entry in model.rssEntries)   
  76.                                             [   
  77.                                                 TableCell {   
  78.                                                     text: bind entry.title   
  79.                                                 },   
  80.                                                 TableCell {   
  81.                                                     text: bind entry.publishedDate.toLocaleString()   
  82.                                                 }   
  83.                                             ]   
  84.                                     selection: bind model.rssSelectedIndex   
  85.                                 }      
  86.                     },   
  87.                     SplitView  {   
  88.                         weight: 0.7   
  89.                         content:   
  90.                             EditorPane   {   
  91.                                 opaque: true
  92.                                 contentType: HTML   
  93.                                 editable: false
  94.                                 text: bind model.rssContent   
  95.                             }   
  96.                         }   
  97.                 ]   
  98.             }   
  99.          ]    
  100. };   
  101. trigger on (RssReaderModel.rssSelectedIndex = value) {   
  102. var desc = rssEntries[value].description.value;   
  103.     rssContent = " {desc} ";    
  104. }   
  105. MY_CONTAINER:JComponent.add(panel.getComponent());   

接着就是使用JSR223的scripting framework来执行:

java 代码

  1. package rssreader;   
  2. import javax.script.Bindings;   
  3. import javax.script.ScriptContext;   
  4. import javax.script.ScriptEngine;   
  5. import javax.script.ScriptEngineManager;   
  6. import javax.script.SimpleScriptContext;   
  7. import javax.swing.JFrame;   
  8. public class Main {   
  9. /**
  10.      * @param args
  11.      * @throws Exception
  12.      */
  13. public static void main(String[] args) throws Exception {   
  14.         RssReader reader = new RssReader();   
  15.         JFrame frame = new JFrame(   
  16. "RSS Reader implemented using JavaFX by alexcheng");   
  17.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
  18.         frame.setSize(800, 600);   
  19.         ClassLoader loader = Thread.currentThread().getContextClassLoader();   
  20.         ScriptEngineManager manager = new ScriptEngineManager(loader);   
  21.         ScriptEngine engine = manager.getEngineByExtension("fx");   
  22.         Bindings bindings = engine.createBindings();   
  23.         bindings.put("READER:rssreader.RssReader", reader);   
  24.         bindings.put("MY_CONTAINER:javax.swing.JComponent", frame   
  25.                 .getContentPane());   
  26.         ScriptContext context = new SimpleScriptContext();   
  27.         context.setBindings(bindings, ScriptContext.GLOBAL_SCOPE);   
  28.         context.setBindings(bindings, ScriptContext.ENGINE_SCOPE);   
  29.         engine.setContext(context);   
  30.         String script = "import rssreader.FxMain;";   
  31.         engine.eval(script);   
  32.         frame.setVisible(true);   
  33.     }   
  34. }   

最后的界面如下:

javafxrssreader.JPG

 描述:
 JavaFX RSS Reader

 文件大小:
 74 KB

 看过的:
 文件被下载或查看 28 次

javafxrssreader.JPG

2007年8月2日星期四

手机黑盒测试方法详细介绍

1。 Release Test
  Purpose:
  测试手机的基本功能是否实现,是否有进一步测试的必要性
  Attention:
  Release Test的Test Case具有一定的典型性,主要是反映手机最基本功能的Test Case
  本类测试只需要依据Test Case进行测试,不需要进一步发挥
  如果有发现与Case无关的Error, 在测试通过后才可以填报Error Report
  此类测试有一门槛值,即Test Case的Pass率达到一定值(如95%)才能宣布版本发布成功,进入进一步的测试,否则此版本无效。
  除了门槛值外,如果重要功能模块的Test Case没通过,也会终止这个版本。
  2 System Test
  Full Round System Test
  Purpose
  对手机的所有功能进行全面的测试(所有语言包)
  由于Case不可能包含所有方面,所以测试时应适度发挥,尽力完成全面测试
  Common System Test (Medium or Minor)
  Attention:
  System Test一般分为两个部分,“跑Case”和Free Test。
  在测试初期,一般只需要按照Test Case测,把一些不可重现的Error都记录下来。同时遇到Test Case的问题或者不充分,应该立即解决(和Team Leader或者Special List讨论,补写Test Case)。在这一阶段结束后,一般要写一个Summary Report。把这一阶段的测试结果和遇到的问题、自己的见解都写在里面(当然是用English)。
  当所有Test Case都测完后,就进入Free Test期间。这里的Free Test具有明确的目的性和范围。一般来说,这段时间的Free Test只需要测自己负责的模块。而且Free Test还负责重现前期“跑Case”是遗留的不可重现的Error。
  3 Focus Test
  Purpose:
  集中于一个或几个点进行测试(同System Test)

  4 Stress Test
  Purpose:
  为了解决市场上发现的重大Error,而进行的有针对性的强度测试
  主要是利用边缘测试(临界测试)手段
  Attention:
  压力测试,顾名思义,是给手机施加一定压力,从而找出手机软件上的Error。一般来说,对手机施加的压力主要有:
  存储压力:由于手机采用的是栈式存储,所以当一个存储块满了之后,如果程序员不做相应处理或者处理不好的话,很容易造成其他存储区被擦除,从而在UI上出现问题(其他功能无法正常使用)。
  边界压力:边界一直是程序员最容易忽略的地方。
  响应能力压力:有时候某个操作可能处理的时间很长,在处理期间如果测试者再不断地进行其他操作的话,很容易出现问题。
  网络流量压力(如在接电话时进行短信服务)等等。
  在项目中,Stress Test有时也会用来重现不可重现的Error。
  由于有不少不可重现的Error是由于Memory Leak(内存泄漏)引起的,所以不停的重复同一个操作是重现一个不可重现的Error的一个好方法。
  5 Free Test

  Purpose:
  测试System Test中没有做完的不可重现Error
  寻找平时没有找到的忽略的Error
  Attention:
  在System Test阶段所用的Free Test具有明显的目的性和范围
  平时的Free Test从理论上应该对所测试的范围穷尽所有的测试方法。但是,这是不现实的。在实际项目中,主要有两个方面是Free Test所需要重视的。
  一是从UI Spec上找灵感。应为Test Case是依据UI Spec写的,所以从UI Spec上突破是一个行之有效的方法。UI Spec有一定的探索深度,加大探索深度,是一种突破的途径;另外同一个功能用其他不同的方法去实现,也是一种突破途径。
  二是多关注不同Feature之间的Interaction。这是手机软件相对比较容易出问题,而Test Case又很少能反映的地方。这是一个很大的Free Test空间。

2007年8月1日星期三

I will be

 

Mmm...

The world seems so cold
When I face so much all alone
A little scared to move on
And knowing how fast I have grown

And I wonder just where I fit in
Oh the vision of life in my head
Oh yes

I will be
Strong on my own
I will see through the rain
I will find my way
I will keep on
Traveling this road
Till I finally reach my dream
Till I'm living, and I'm breathing
My destiny, yeah yeah

I can't let go now
Even when darkness surrounds
But if I hold on, yeah
I will show the world
All the things that you never expected to see
From little old me, this pittsburgh girl

And I wonder just where my place is
Close my eyes and I remind myself this
Oh yeah yeah

I will be
Strong on my own
I will see through the rain
I will find my way
I will keep on
Traveling this road
Till I finally reach my dream
Till I'm living, and I'm breathing
My destiny, ohh

It comforts me
Ooh it keeps me
Alive each day of my life
Always guiding me
Providing me
With the hope I desperately need

Well I gotta believe
There's something out there meant for me
Oh I get on my knees
Praying I will receive
The courage to grow and the faith to know

That I will be
Strong on my own
I will see through the rain
I will find my way
I will keep on
Traveling this road
Till I finally reach my dream
Till I'm living, and I'm breathing
My destiny

 

每次听这首歌,感觉心中充满了力量!

生活中或许是冷酷的,充满了困惑和无奈。
然而,正在走投无路,迷失方向的时候,
心中一个坚强的声音不停的提醒自己:
我要自强不息,
我要拨开层层迷雾,
我要坚持走下去,
直到我实现我的梦想。

感谢Christina Aguilera的声音!