Java 支持三种注释方式,合理使用注释能显著提升代码可读性:
// 单行注释
/*
* 多行注释
* 常用于方法说明
*/
/**
* JavaDoc注释
* 用于生成API文档
* @param args 命令行参数
*/
public class CommentDemo {
public static void main(String[] args) {
System.out.println("注释示例");
}
}
Java标识符命名规则:
常见关键字:
class, public, static, void, if, else,
for, while, do, switch, case, break,
continue, return, try, catch, finally,
new, this, super, extends, implements
Java 是强类型语言,数据类型分为两大类:
基本数据类型分类
类型 | 大小 | 范围 | 默认值 |
---|---|---|---|
byte |
8位 | -128 ~ 127 | 0 |
short |
16位 | -32,768 ~ 32,767 | 0 |
int |
32位 | -2³¹ ~ (2³¹-1) | 0 |
long |
64位 | -2⁶³ ~ (2⁶³-1) | 0L |
float |
32位 | IEEE 754浮点数标准 | 0.0f |
double |
64位 | IEEE 754浮点数标准 | 0.0d |
char |
16位 | Unicode字符 (\u0000~\uffff) | '\u0000' |
boolean |
无明确大小 | true /false |
false |
Java变量分为三种类型:
public class VariableDemo {
// 实例变量(成员变量)
private int instanceVar = 10;
// 类变量(静态变量)
public static String classVar = "Static";
public void method() {
// 局部变量
int localVar = 20;
System.out.println(localVar);
}
}
变量特性对比表:
特性 | 局部变量 | 实例变量 | 类变量 |
---|---|---|---|
声明位置 | 方法/代码块内 | 类内方法外 | 类内方法外+static |
生命周期 | 方法执行期间 | 对象存活期间 | 程序运行期间 |
存储位置 | 栈 | 堆 | 方法区 |
默认值 | 无(必须初始化) | 有默认值 | 有默认值 |
访问权限 | 仅方法内可见 | 对象级别访问 | 类名直接访问 |
示例 | int count = 0; |
private String name; |
static int total; |
// if-else
if(condition1) {
// code
} else if(condition2) {
// code
} else {
// code
}
// switch (Java 12+支持表达式形式)
switch(day) {
case 1 -> System.out.println("Monday");
case 2 -> System.out.println("Tuesday");
default -> System.out.println("Weekend");
}
// for循环
for(int i=0; i<10; i++) {
System.out.println(i);
}
// 增强for循环
for(String item : collection) {
System.out.println(item);
}
// while循环
while(condition) {
// code
}
// do-while循环
do {
// code
} while(condition);
break; // 跳出循环
continue; // 跳过本次循环
return; // 方法返回
[访问修饰符] [static] 返回类型 方法名(参数列表) {
// 方法体
return 返回值; // void方法可省略
}
// 示例
public static int add(int a, int b) {
return a + b;
}
class Calculator {
// 方法名相同,参数列表不同
int add(int a, int b) { return a+b; }
double add(double a, double b) { return a+b; }
}
public static void printNames(String... names) {
for(String name : names) {
System.out.println(name);
}
}
// 类定义
[访问修饰符] class 类名 {
// 字段
// 构造方法
// 方法
}
// 示例
public class Person {
private String name;
private int age;
// 构造方法
public Person(String name, int age) {
this.name = name;
this.age = age;
}
// 方法
public void introduce() {
System.out.println("I'm " + name);
}
}
class Animal {
public void sound() {
System.out.println("Animal sound");
}
}
class Dog extends Animal {
@Override
public void sound() {
System.out.println("Bark");
}
}
// 接口
interface Flyable {
void fly();
}
// 抽象类
abstract class Bird implements Flyable {
abstract void sing();
}
class Sparrow extends Bird {
public void fly() { /* 实现 */ }
void sing() { /* 实现 */ }
}
try {
// 可能抛出异常的代码
} catch(IOException e) {
// 异常处理
} finally {
// 最终执行
}
class Box<T> {
private T content;
public void set(T content) {
this.content = content;
}
public T get() {
return content;
}
}
List<String> list = new ArrayList<>();
list.add("Java");
list.add("Python");
Map<String, Integer> map = new HashMap<>();
map.put("Alice", 25);
List<String> languages = Arrays.asList("Java", "Python", "C++");
long count = languages.stream()
.filter(lang -> lang.startsWith("J"))
.count();
Java 语法体系经过多年的发展已经非常成熟,而掌握这些核心语法是成为 Java 开发者的基础。建议大家通过实际编码练习来巩固这些概念。