Java 快速入门 知识精简(2)基础知识-关键字

关键字

定义:被Java语言赋予了特殊含义,用做专门用途的字符串(单词)。

特点:关键字中所有字母都为小写。

用于定义数据类型的关键字:

类的类型定义 class  interface  enum

方法返回值的类型定义 void 

数据类型定义 byte  short  int  long  float  double  char  boolean

public class MyClass {
    // 类    类名: MyClass 
}

public interface MyInterface {
    // 接口  接口名: MyInterface 
}

public enum Day {
    // 枚举  枚举名: Day
    MONDAY, TUESDAY, WEDNESDAY;
}

public class TestClass{

    public void doSomething() {    // 方法  方法名: doSomething
        // void: 不返回任何值
    }
    // byte:8位有符号整数
    byte b = 100;

    // short:16位有符号整数。
    short s = 10000;

    // int:32位有符号整数。
    int i = 1000000;

    // long:64位有符号整数。
    long l = 100000000000L;

    // float:单精度浮点数。
    float f = 1.23F;

    // double:双精度浮点数。
    double d = 1.23;

    // char:单字符。
    char ch = 'A';

    // boolean:布尔值。
    boolean flag = true;
        
    }
}

用于定义流程控制的关键字:

if else switch case default do while for

break continue return

public class testClass{  // 类 testClass

    public String test(){  // 方法 test 定义了String字符串类型的返回值

        // if-else:条件控制
        if (condition) {
            // 条件为真时执行
        } else {
            // 条件为假时执行
        }
        

        // switch-case-default:多条件选择
        switch (day) {
            case MONDAY:
                // day = MONDAY时, 执行代码
                break;
            default:
                // 默认执行的代码
                break;


        // do-while:循环控制
        do {
            a += 1;     // a = a + 1
        } while (a < 10); // 先执行一次do{}中的代码 , 再判断while()

        // while:循环控制
        while (a < 10) {  // 当 a < 10 执行while(){}
            System.out.println("a小于10");
        }

        // for:循环控制
        for (int i = 0; i < 10; i++) {
            // 循环体
        }

        // break:跳出循环
        for (int i = 0; i < 10; i++) {
            if (i == 5) {
                break; // 跳出循环
            }
        }

        // continue:跳过当前循环
        for (int i = 0; i < 10; i++) {
            if (i % 2 == 0) {
                continue; // 跳过偶数
            }
            // 执行奇数的代码
        }

        // return:结束方法
   
        return "返回值"; // 返回字符串
    }
}

用于定义访问权限修饰符的关键字:

public protected private 默认无修饰符 default

// public:公共访问权限。
public class PublicClass {
    // 类体
}

// protected:受保护的访问权限。
protected int protectedVar;

// private:私有访问权限。
private void privateMethod() {
    // 方法体
}

// default(无修饰符):包访问权限。
class DefaultAccessClass {
    // 默认包访问权限的成员
}

用于定义类、函数、变量修饰符的关键字:

abstract final static synchronized

// abstract:抽象的
public abstract class AbstractClass {
    // 抽象类不能被实例化
}


// final:最终的,不可改变的
final int CONSTANT = 100;



public class MyClass {
    // static:静态的
    public static void staticMethod() {
        // 静态方法
    }

    // synchronized:同步的
    public synchronized void synchronizedMethod() {
        // 同步方法
    }
}

用于定义类与类之间关系的关键字:

extends implements

// extends:继承
public class SubClass extends SuperClass {
    // 子类 继承 父类
}


// implements:实现
public class MyClass implements MyInterface {
    // 类 实现 接口
}

用于定义建立实例与引用实例,判断实例的关键字:

new this super instanceof

// new:创建新实例
MyClass obj = new MyClass();


// this:指向当前对象的引用
public class MyClass {
    public void print() {
        System.out.println(this);
    }
}

// super:指向父类的引用
public class SubClass extends SuperClass {
    public void method() {
        super.someMethod(); // 调用父类的方法
    }
}

// instanceof:判断实例类型
if (obj instanceof MyClass) {
    // obj是MyClass类型或其子类的实例
    boolean isMyClass = true
}

用于异常处理的关键字:

try catch finally throw throws

public void method() throws Exception {

    try-catch-finally:异常处理。
    try {
        // 尝试执行的代码
    } catch (Exception e) {
        // 异常发生时执行的代码
        throw new Exception("Error occurred");    // throw:抛出异常。
    } finally {
        // 无论是否发生异常都会执行的代码
    }
}


// throws:方法可能抛出的异常。
public void method() throws IOException {
    // 可能会抛出IOException的方法体
}

用于包的关键字:

package import

package com.example;    // package:定义包

// import:导入类或包
import com.example.MyClass;
import java.util.*;

其他修饰符关键字:

native  strictfp transient volatile

public class TestClass {

    // native:方法用其他语言实现
    public native void nativeMethod();


    // strictfp:严格模式浮点数计算
    public strictfp double strictfpMethod(double a, double b) {
        return a + b;
    }


    // transient:对象序列化时忽略该变量
    public transient int transientVar;


    // volatile:确保变量的读写操作的原子性,保证内存可见性
    private volatile int counter;

}

用于定义数据类型值的字面值

true false null

public class TestClass {

    // true 和 false:布尔值。
    boolean isTrue = true;
        
    boolean isFalse = false;


    // null:空引用。
    String str = null;

}

保留字(reserved word):

goto const

  • 5
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Tangy范

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值