java基础语法 Acemyzoe

JAVA语法

hello world

在 Java 中,所有的代码都必须写在类里面,定义一个 HelloWorld 类。

/* hello world demo */
public class HelloWorld{
    public static void main(String []args){
        System.out.println("Hello World!"); // 打印
    }
} 

基本数据类型

  • 整型类型:byte(字节)、short(短整型)、int(整型)、long(长整型)
  • 浮点类型:float(单精度浮点)、double(双精度浮点)
  • 字符型:char
  • 布尔型:boolean

封装这些基本数据类型的类

  • Integer:对应封装了基本类型 int
  • Long:对应封装了基本类型 long
  • Float:对应封装了基本类型 float
  • Double:对应封装了基本类型 double
  • Boolean:对应封装了基本类型 boolean
  • String:对应封装了字符串类型 char[]
int a = 6;
Integer oa = new Integer(6);
int a[] = new int[10]; //定义长度为10的int类型数组
a[1] = 3;
System.out.println(a[2])

流程控制

/* if-else */
int a;
if (a>1){
    //todo
} else if (a=1){
    //todo
} else{
    //todo
}

/* switch-case */
switch (a) {
    case 1:
        //todo
        break;
    case 2:
        //todo
        break;
    default:
        //default
}

/*  for/while */
for (int i = 0; i < 10; ++i) {
    if(i==4){
        continue;//跳出本次循环
    }
    if(i==3){
        break;//提前终止循环
    }
}

while (i<10) {
    //todo
}

类、对象

public class Dog {
    private int age;
    private int weight;
    
    public Dog(int age,int weight){//构造函数,命名同类名
        this.age=age;
        this.weight=weight;
    }
    
    public int getAge(){
        return age;
    }
    
    public void run(){
        //todo
    }
}

Dog dog1 = new Dog(2,111); //通过new创建一个Dog对象
int age = dog1.getAge();
dog1.run();

权限修饰符

  • private 修饰的函数或者成员变量,只能在类内部使用。
  • protected 修饰的函数或者成员变量,可以在类及其子类内使用。
  • public 修饰的函数或者成员变量,可以被任意访问。
  • 权限修饰符还可以修饰类

继承

Java 语言使用 extends 关键字来实现继承

public class Animal {//父类
    protected int age;//protected
    protected int weight;
    
    public Animal(int age,int weight){
        this.age=age;
        this.weight=weight;
    }
    
    public int getAge(){
        return age;
    }
}

public class Dog extends Animal {//子类
    public Dog(int age,int weight){
        super(age,weight);//调用父类的构造函数
    }
    
    public void wang(){
        //...
    }
}

Dog dog1 = new Dog(2,22);
dog1.run();
dog1.wang();

接口

Java 语言通过 interface 关键字来定义接口。

接口中只能声明方法,不能包含实现,也不能定义属性。

类通过 implements 关键字来实现接口中定义的方法。

public interface Runable{
    void run();
}

public class Dog implements Runable {
    private int age;
    private int weight;
    
    public Dog(int age,int weight){
        this.age=age;
        this.weight=weight;
    }
    
    public int getAge(){
        return age;
    }
    
    @Override
    public void run(){//实现接口中定义的run方法
        //todo
    }
}

容器

容器可以理解为一些工具类,底层封装了各种数据结构。

如 ArrayList 底层就是数组,LinkedList 底层就是链表,HashMap 底层就是散列表等

public class DemoA{
    private ArrayList<User> users;
    public void addUser(User user){
        users.add(user);
    }
}

异常处理

通过关键字 throw 来抛出一个异常,

通过 throws 声明函数抛出异常,

通过 try-catch-finally 语句来捕获异常。

public class UserNotFoundException extends Exception{//自定义一个异常
    public UserNotFoundException(){
        super();
    }
    public UserNotFoundException(String message){
        super(message);
    }
    public UserNotFoundException(String message,Throwable e){
        super(message,e);
    }
}

public class UserService{
    private UserRepository uerRepo;
    public UserService(UseRepository userRepo){
        this.userRepo = userRepo;
    }
    public User getUserById(long userId) throws UserNotFoundException{
        User user = userRepo.findUserById(userId);
        if (user == null){// throw用来抛出异常
            throw new UserNotFoundException();// 代码从此处返回
        }
    }
}

public class UserController{
    private UserService userService;
    public UserController(UserService userService){
        this.userService = userService;
    }
    public User getUserById(long userId){
        User user = null;
        try{// 捕获异常
            user = userService.getUserById(userId);
        } catch (UserNotFoundException e){
            System.out.println("User not found: " + userId);
        } finally {// 不管异常会不会发生,finally包裹的语句块总会被执行
            System.out.println("I am always printed.");
        }
        return user;
    }
}

package包

Java 通过 pacakge 关键字来分门别类地组织类,通过 import 关键字来引入类或者package。

/*class DemoA*/
package com.xzg.cd; // 包名 com.xzg.cd
public class DemoA{
    //...
}
/*class DemoB*/
package com.xzg.alg;
import java.util.HashMap; // Java 工具包JDK中的类
import java.util.Map;
import com.xzg.cd.DemoA;
public class DemoB{
    //...
}