前端进阶全栈计划:Java基础语法

76 阅读2分钟

wallhaven-7p7wev.png

前言

本教程旨在帮助初学者系统地掌握Java的基础知识。我们将从Java的基本语法开始,逐步深入到面向对象编程、异常处理、多线程编程等核心概念。无论你是编程新手,还是希望夯实基础的开发者,这份指南都将带你走进Java的世界,打下坚实的编程基础。

1. 基本语法

  • 变量与数据类型:理解不同的数据类型(如 int、double、boolean、char 等),以及如何声明和初始化变量。
  int number = 10;
  double price = 29.99;
  boolean isTrue = true;
  char letter = 'A';

2. 操作符

  • 算术操作符:加(+)、减(-)、乘(*)、除(/)和取模(%)。

  int sum = 5 + 3;
  int difference = 5 - 3;
  int product = 5 * 3;
  int quotient = 5 / 3;
  int remainder = 5 % 3;
  • 比较操作符:等于(==)、不等于(!=)、大于(>)、小于(<)、大于等于(>=)、小于等于(<=)。
  boolean isEqual = (5 == 3);
  boolean isNotEqual = (5 != 3);
  boolean isGreater = (5 > 3);
  • 逻辑操作符:与(&&)、或(||)、非(!)。

  boolean andResult = (true && false);
  boolean orResult = (true || false);
  boolean notResult = !true;

3. 控制结构

  • 条件语句ifelse ifelse 和 switch
  if (condition) {
      // code block
  } else if (anotherCondition) {
      // another code block
  } else {
      // another code block
  }

  switch (expression) {
      case value1:
          // code block
          break;
      case value2:
          // code block
          break;
      default:
          // code block
  }
  • 循环结构forwhile 和 do-while
  for (int i = 0; i < 10; i++) {
      // code block
  }

  int j = 0;
  while (j < 10) {
      // code block
      j++;
  }

  int k = 0;
  do {
      // code block
      k++;
  } while (k < 10);

4. 方法

  • 方法定义与调用:如何定义方法,并在程序中调用它们。
  public int sum(int a, int b) {
      return a + b;
  }

  public static void main(String[] args) {
      MyClass myObject = new MyClass();
      int result = myObject.sum(5, 3);
  }

5. 面向对象编程(OOP)

  • 类与对象:理解类的定义和对象的创建。
  public class Car {
      // attributes
      String color;
      int maxSpeed;

      // methods
      public void display() {
          System.out.println("Car color: " + color);
          System.out.println("Max speed: " + maxSpeed);
      }
  }
  • 构造方法:如何定义和使用构造方法初始化对象。

  public class Car {
      String color;
      int maxSpeed;

      // Constructor
      public Car(String color, int maxSpeed) {
          this.color = color;
          this.maxSpeed = maxSpeed;
      }
  }

  Car myCar = new Car("Red", 180);
  • 封装:使用修饰符(privatepublicprotected)保护类的属性。
  public class Employee {
      private String name;
      private double salary;

      // Getter and Setter methods
      public String getName() {
          return name;
      }

      public void setName(String name) {
          this.name = name;
      }

      public double getSalary() {
          return salary;
      }

      public void setSalary(double salary) {
          this.salary = salary;
      }
  }
  • 继承:通过 extends 关键字实现类的继承。
  public class Animal {
      void eat() {
          System.out.println("This animal eats food.");
      }
  }

  public class Dog extends Animal {
      void bark() {
          System.out.println("The dog barks");
      }
  }

  Dog myDog = new Dog();
  myDog.eat();
  myDog.bark();
  • 多态:方法重载和方法重写。
  // Method Overloading
  public class MathUtils {
      public int add(int a, int b) {
          return a + b;
      }

      public double add(double a, double b) {
          return a + b;
      }
  }

  // Method Overriding
  public class Animal {
      void sound() {
          System.out.println("This animal makes a sound");
      }
  }

  public class Cat extends Animal {
      void sound() {
          System.out.println("The cat meows");
      }
  }

  Animal myCat = new Cat();
  myCat.sound(); // Output: The cat meows

6. 包和导入

  • :组织代码结构。
  package com.example.myapp;

  public class MyApp {
      // class code
  }
  • 导入:使用其他包的类。
  import java.util.Scanner;

  public class MyApp {
      public static void main(String[] args) {
          Scanner scanner = new Scanner(System.in);
          // code
      }
  }

7. 文件 I/O

  • 基本的文件读写:使用 FileReaderFileWriter 等进行文件操作。
  import java.io.FileWriter;
  import java.io.IOException;

  public class MyApp {
      public static void main(String[] args) {
          try {
              FileWriter myWriter = new FileWriter("filename.txt");
              myWriter.write("Hello, world!");
              myWriter.close();
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
  }