Java必备的常用语法

阅读量: 1005 编辑

if-else的语法

//语法
if(expression){
    statement
}

if(expression){
    statement
}else{
    statement
}

if(expression){
    statement
}else if(expression){
    statement
}else if(expression){
    statement
}else{
    statement
}

switch语法

switch(expr){
    case constant1:
      statements;
      break;
    case constant2:
      statements;
      break;
    ...
    default:
      statements;
      break;
}

for循环语法

for( init_expr ; test_expr ; alter_expr){
    statement
}

for( type item : collection){  //for-each循环
    statement
}

while的语法

//while循环
while (test_expr){
    statement;
    alter_expr;
}

//do-while循环
do {
    statement;
    alter_expr;
} while(test_expr)

数组的语法

//语法
type[] arr_name; //推荐这种写法,更好理解:数组是一种类型
type arr_name[];

//在数组定义中,不能指定数组的长度,而需要在数组的创建阶段来指定
int[] a; 
String[] b; //声明

//数组的创建
int[] a = new int[5]; //创建,可以放5个int类型的数据,默认值都是0
int[] a = new int[]{10,20,30,40,50}; //创建并初始化数据
int[] a = {10,20,30,40,50}

类的语法

[<modifiers>] class <class_name> {
    [<attribute_declarations>]
    [<constructor_declarations>]
    [<method_declarations>]
}

// [..]:中括号中的内容表示可有可无
// <..>:中的内容表示可以自己定义,但是必须有
// <modifiers>:访问权限修饰符,包括 public default protected private,这个参考高级部分讲解
// class:表示的是类,必须有
// <class_name>:表示类的名称
// [<attribute_declarations>]:表示属性
// [<constructor_declarations>]:表示构造器
// [<method_declarations>]:表示函数

属性的语法

[<modifiers>]  type  <attr_name> [=defaultValue]; 

// <modifiers>:包括 public private protected default
// type:是8种基本类型,或者引用类型
// <attr_name>:属性的名字
// [=defaultValue]:默认值,可以有也可以没有

函数(方法)的语法

[<modifiers>] <return_type> <name>([<argu_list>]) {
    [<statements>]
}

// <modifiers>:public private protected default
// <return_type>:返回值类型,可以是8种基本类型,也可以是自定义的引用类型
// <name>:函数的名字
// [<argu_list>]:参数列表,参数列表一般是 type <arg-name> 的形式
// <statements>:代码片段

构造器的语法

[<modifiers>]  <class_name>([<argu_list>]){
    [<statements>]
}

// [<modifiers>] 访问权限修饰符,包括 public default protected private
// <class_name>:必须和类的名字一致
// [<argu_list>]:构造器的参数
// [<statements>]:构造器中的代码片段

类的继承的语法

<modifier> class  <name>  [extends <superclass>] {
    declaration statements
}

接口的语法

<modifier> interface <name> {
    [<attribute_declarations>]
    [<abstract_method_declarations>]//只有函数的声明,没有函数体
}

接口的实现语法

<modifier>  class  <name>  [extends <superclass>] [implements  <interface>  [,<interface>]] {
    <declarations> 
}

接口继承的语法

<modifier>  interface  <name>  [extends <super_interface> ,<super_interface>]{
    [<attribute_declarations>]
    [<abstract_method_declarations>]
}

枚举的语法

<modifier>  enum  <name> {
    instance1, //逗号分隔
    instance2
    
    ;//分号结束
}

异常捕获的语法

try{
    // 可能会抛出特定异常的代码段
}catch(ExceptionType exception){
    // 如果Exception 被抛出,则执行这段代码
}catch(ExceptionType otherException){
    //如果另外的异常otherException被抛出,则执行这段代码
} [finally{
    //无条件执行的语句
}]

异常抛出的语法

<modifer> <returnType> methodName([<argument_list>]) throws <exception_list> {
    //statements
}

注解的语法

@Retention(RetentionPolicy.Policy_Type_Value)
@Target(ElementType.Type_Value)
<modifer> @interface <interface-name> {
    [<type> <function_name()> [default value] ]
}
爱码岛编程公众号
试卷资料
爱码岛编程小程序
在线刷题
苏ICP备13052010号
©2023 南京匠成信息科技有限公司