《java语法基础1》由会员分享,可在线阅读,更多相关《java语法基础1(97页珍藏版)》请在金锄头文库上搜索。
1、Wen Jiabao, College of Information Science and Engineering in Hunan University.,第2讲 Java语法基础,OUTLINE,了解Java的语法基础 Java数据类型 Java的运算符、表达式、语句、程序 对象包装类 Java编程的基本技巧 Java流程控制语句(分支、循环、跳转) Java数组 编程实践,OUTLINE,了解Java的语法基础 1.1 标识符的命名约定和惯例 1.2 关键字 1.3 Java注释 Java数据类型 Java的运算符、表达式、语句、程序 对象包装类 Java编程的基本技巧 Java流程控
2、制语句(分支、循环、跳转) Java数组 编程实践,1 了解Java的语法基础,与所有的程序设计语言一样,Java语言也是由Java语言规范和Java开发包(API)组成的。学习任何语言都要从这两个方面入手。 Java语言主要由以下六种元素组成:标识符、关键字、分隔符、数据类型、运算符、表达式组成。 如何运用这些语言构成自己的程序呢? import java.io.*; public class HelloworldApp public static void main(String args) int i=100; System.out.println(i/8): 找出上面程序中的语法要素:
3、,1 了解Java的语法基础,Identifier Keyword vs Reserved word 分隔符空格 tab: 分隔符. :用于包与包、包与类、类和方法、对象与方法之间 分隔符; :每条Java语句以;结束,Java允许将一个长语句写到多行中去,但是不以断开Keyword、String。一般不将多个语句写到一行中。 分隔符 :类体;方法体;复合语句 分隔符 :数组 分隔符( ) :方法的定义或方法的调用 分隔符“ ” :字符串String 分隔符 :字符 三种注释/ /*/ /*/ 运算符,1.1 Java标识符的命名约定和惯例,标识符的命名规则: 标识package,class,
4、 method, variable, parameter, interface等成份的名字,是字母、下划线、$ 、数字组成的字符混合序列,不能以数字开头,区别大小写,不能使用Java的关键字或保留字。 命名时应尽量体现各自描述的事物或属性、功能等。一般性命名约定: 尽量使用完整的英文单词或确有通用性的英文缩写或专业术语。 词组中采用大小写混合,使之更易于识别。 避免使用过长的标识符,一般控制在15个字符以内。 避免使用类似的标识符,或者仅仅是大小写不同。,1.1 Java标识符的命名约定和惯例,具体命名惯例如下: 包名就为名词或名词性短语,全部小写。 类名、接口名应为名词或名词性短语,各单词首
5、字母大写。 方法名应为动词或动宾短语,首字母小写,其余各单词首字母大写。 变量名应为名词或名词性短语,首字母小写,其余各单词首字母大写。 常量名应全部大写。 举例: package cn.edu.hdc; import java.util.StringTokenizer; private static final int MAX_AVAILABLE = 100; public class PrintStream extends FilterOutputStream implements Appendable, Closeable public boolean equalsIgnoreCase(
6、String anotherString),1.2 Keyword and Reserved Words,关键字KeyWord或保留字ReservedWord全部小写是Java语言本身使用的系统标识符,有特定的语法含义,不能用作标识符使用。 Java所有数据类型的长度都固定,并与平台无关,因此没有sizeof保留字。 Keywords are special reserved words in Java that you cannot use as identifiers (names) for classes, methods, or variables. They have meaning
7、 to the compiler; it uses them to figure out what your source code is trying to do. Table 1-1 contains all 49 of the reserved keywords.,1.2 Keyword and Reserved Words,1.2 Keyword and Reserved Words,The following are access modifiers: private Makes a method or a variable accessible only from within i
8、ts own class. (私有:类内可访问) friendly protected Makes a method or a variable accessible only to classes in the same package or subclasses of the class. (受保护:包内或子类内可访问) public Makes a class, method, or variable accessible from any other class. (公开:任何类均可访问) 资源方法和属性 人类class 家庭包Package 门牌号码手机号码电视日记本,1.2 Key
9、word and Reserved Words,The following are class, method, and/or variable modifiers: abstract Used to declare a class that cannot be instantiated, or a method that must be implemented by a nonabstract subclass. (抽象类:不可建立实例;抽象方法:必由非抽象子类予以实现) final Makes it impossible to extend a class, override a meth
10、od, or reinitialize a variable. (最终类:不可继承;最终方法:不可重载;最终变量:不能再次初始化) class Keyword used to specify a class. interface Keyword used to specify an interface. (接口类) extends Used to indicate the superclass that a subclass is extending. (扩展:指定被继承的超类) implements Used to indicate the interfaces that a class w
11、ill implement. (实现:指定被实现的接口类) new Used to instantiate an object by invoking the constructor. (激活构造方法,实例出一个对象),1.2 Keyword and Reserved Words,static Makes a method or a variable belong to a class as opposed to an instance. (静态方法或变量:属于类而非方法) synchronized Indicates that a method can be accessed by only
12、 one thread at a time. (同步方法:线程独占访问) transient Prevents fields from ever being serialized. Transient fields are always skipped when objects are serialized. (暂态域:不可序列化) volatile Indicates a variable may change out of sync because it is used in threads. (易失变量:线程同步可能改变其值) native Indicates a method is w
13、ritten in a platform-dependent language, such as C. (本地方法:用平台相关的语言编写的方法),1.2 Keyword and Reserved Words,The following are keywords used to control the flow through a block of code: if Used to perform a logical test for true or false. else Executes an alternate block of code if an if test is false. s
14、witch Indicates the variable to be compared with the case statements. case Executes a block of code, dependent on what the switch tests for. default Executes this block of code if none of the switch-case statements match. for Used to perform a conditional loop for a block of code. while Executes a b
15、lock of code repeatedly while a certain condition is true. do Executes a block of code one time, then, in conjunction with the while statement, it performs a test to determine whether the block should be executed again.,1.2 Keyword and Reserved Words,break Exits from the block of code in which it re
16、sides. continue Stops the rest of the code following this statement from executing in a loop and then begins the next iteration of the loop. instanceof Determines whether an object is an instance of a class, superclass, or interface. (是否是某个类/超类/接口类的实例?) return Returns from a method without executing any code that followsthe statement (can optionally return a variable).,1.2 Keyword and Reserved Words,The following are keywords used in error handling: try Block of code tha