技术开发 频道

Java程序设计知识与技能(五)

【IT168 技术教程】

变量、声明和赋值
  Java技术规范的浮点数的格式是由电力电子工程师学会(IEEE)754定义的,它使用表2-3的长度,并且是独立于平台的。下列程序显示了如何为整数、浮点、boolean、字符和string类型变量声明和赋值。
public class Assign { public static void main(String args []) { int x, y; // declare int // variables float z = 3.414f; // declare and assign // float double w = 3.1415; // declare and assign // double boolean truth = true; // declare and assign // boolean char c; // declare character // variable String str; // declare String String str1 = "bye"; // declare and assign // String variable c = 'A'; // assign value to char // variable str = "Hi out there!"; // assign value to // String variable x = 6; y = 1000; // assign values to int variables ... } }
非法赋值举例
y = 3.1415926; // 3.1415926 is not an int. // Requires casting and decimal will // be truncated. w = 175,000; // the comma symbol ( , ) cannot appear truth = 1; // a common mistake made by ex- C / C++ // programmers. z = 3.14156 ; //can't fit double into a //Float. Requires casting.
Java编码约定
 
Java编程语言的一些编码约定是:
 
·classes类名应该是名词,大小写可混用,但首字母应大写。例如:
class AccountBook
class ComplexVariable
 
·interface界面名大小写规则与类名相同。
interface Account
 
·method方法名应该是动词,大小写可混用,但首字母应小写。在每个方法名内,大写字母将词分隔并限制使用下划线。例如:
balanceAccount()
addComplex()
 
·Variables所有变量都可大小写混用,但首字符应小写。词由大写字母分隔,限制用下划线,限制使用美元符号($),因为这个字符对内部类有特殊的含义。
currentCustomer
 
·变量应该代表一定的含义,通过它可传达给读者使用它的意图。尽量避免使用单个字符, 除非是临时"即用即扔"的变量(例如,用i, j, k作为循环控制变量)。
 
·constant原始常量应该全部大写并用下划线将词分隔;对象常量可大小写混用。
HEAD-COUNT
MAXIMUM-SIZE
 
·control structures当语句是控制结构的一部分时,即使是单个语句也应使用括号({})将语句封闭。例如:
if (condition) { do something }else { do something else }
·spacing每行只写一个语句并使用四个缩进的空格使你的代码更易读。
 
·comments用注释来说明那些不明显的代码段落;对一般注释使用 // 分隔符, 而大段的代码可使用 /*···*/分隔符。使用 /**···*/将注释形成文档,并输入给javadoc以生成HTML代码文档。
// A comment that takes up only one line. /* Comments that continue past one line and take up space on multiple lines...*/ /** A comment for documentation purposes. @see Another class for more information */
注意:@see是一个有关类或方法的特殊的javadoc标记符("see also")。有关javadoc的详细资料, 请参见"The Design of Distributed Hyperlinked Programming Documentation"(Lisa著)的有关文档系统的完整定义。该资料可从下列地址获得:http://www.javasoft.com/doc/api_documentation.html
0
相关文章