2 基础语法.ppt

  1. 1、有哪些信誉好的足球投注网站(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
  2. 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  3. 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
2 基础语法

1、注意命名规则 2、每行一条一句 3、适当的注释 4、适当的缩进和空行(indentation and spacing) 现在的IDE基本都具有代码风格化的功能了,所以照章执行即可,不要另外改动代码格式。 NOTE(浮点数运算不精确、但整数运算精确) Calculations involving floating-point numbers are approximated because these numbers are not stored with complete accuracy. For example, System.out.println(1.0 - 0.1 - 0.1 - 0.1 - 0.1 - 0.1); displays 0.5000000000000001, not 0.5, and System.out.println(1.0 - 0.9); displays 0.09999999999999998, not 0.1. Integers are stored precisely. Therefore, calculations with integers yield a precise integer result. Increment and Decrement Operators, cont. Numeric Type Conversion (数值类型转换) Consider the following statements: byte i = 100; long k = i * 3 + 4; double d = i * 3.1 + k / 2; Type Casting(类型转换)(注意casting这个词) 1) Implicit casting(隐式转换) double d = 3; (type widening) 2) Explicit casting(显式转换) int i = (int)3.0; (type narrowing) int i = (int)3.9; (Fraction part is truncated 截断) What is wrong? int x = 5 / 2.0; Casting between char and Numeric Types(字母与数值类型之间的转换) JDK1.5中for 循环的优化 将一个集合作为一个整体放入for循环中,在for循环中可将集合中的元素进行逐个处理。 String[ ] names ={”Wang”,”Zhang”,”Li”,”Wu”};   for(String option: names) {    System.out.println(option); } Note (下面2例写法不常见,但都是正确的,不推荐这样写代码) Caution(不要用浮点数比较大小来作为循环控制的条件) Don’t use floating-point values for equality checking in a loop control. Since floating-point values are approximations for some values, using them could result in imprecise counter values and inaccurate results. Consider the following code for computing 1 + 0.9 + 0.8 + ... + 0.1: double item = 1; double sum = 0; while (item != 0) { // No guarantee item will be 0 sum += item; item -= 0.1; } System.out.println(sum); Variable item starts with 1 and is reduced by 0.1 every time the loop body is executed. The loop should terminate when item becomes 0. However, there is no guarantee that item will be exactly 0, because the floating-point arithmetic is approximated. This loop seems OK on the surface, but it is actually an infi

文档评论(0)

sh4125733 + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档