Java技術堆疊:從入門到精通
Java是一門廣泛應用於軟體開發領域的高階程式語言,具有跨平台、物件導向、可靠性高等特點。身為Java開發者,掌握Java技術堆疊是不可或缺的。本文將從Java的基礎知識入門開始,逐步深入探討Java的各個技術要點,幫助讀者從入門到精通Java技術堆疊。
一、基礎入門
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, world!"); } }
public class Animal { private String name; public Animal(String name) { this.name = name; } public void sayHello() { System.out.println("Hello, I am " + name); } } public class Dog extends Animal { public Dog(String name) { super(name); } public void bark() { System.out.println("Woof!"); } } public class Main { public static void main(String[] args) { Dog dog = new Dog("Tommy"); dog.sayHello(); dog.bark(); } }
public class Main { public static void main(String[] args) { try { int result = divide(10, 0); System.out.println("Result: " + result); } catch (ArithmeticException e) { System.out.println("Error: " + e.getMessage()); } } public static int divide(int a, int b) throws ArithmeticException { if (b == 0) { throw new ArithmeticException("Divisor cannot be zero"); } return a / b; } }
二、中階提升
import java.util.ArrayList; import java.util.List; public class Main { public static void main(String[] args) { List<String> fruits = new ArrayList<>(); fruits.add("Apple"); fruits.add("Banana"); fruits.add("Orange"); for (String fruit : fruits) { System.out.println(fruit); } } }
public class MyThread extends Thread { private String name; public MyThread(String name) { this.name = name; } public void run() { for (int i = 0; i < 5; i++) { System.out.println(name + " " + i); } } } public class Main { public static void main(String[] args) { MyThread thread1 = new MyThread("Thread 1"); MyThread thread2 = new MyThread("Thread 2"); thread1.start(); thread2.start(); } }
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class Main { public static void main(String[] args) { try (BufferedReader reader = new BufferedReader(new FileReader("input.txt")); BufferedWriter writer = new BufferedWriter(new FileWriter("output.txt"))) { String line; while ((line = reader.readLine()) != null) { writer.write(line); writer.newLine(); } System.out.println("File copied successfully"); } catch (IOException e) { System.out.println("Error: " + e.getMessage()); } } }
三、高階進階
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class Main { public static void main(String[] args) { try (Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "username", "password"); PreparedStatement statement = connection.prepareStatement("SELECT * FROM users"); ResultSet resultSet = statement.executeQuery()) { while (resultSet.next()) { String username = resultSet.getString("username"); String email = resultSet.getString("email"); System.out.println("Username: " + username + ", Email: " + email); } } catch (SQLException e) { System.out.println("Error: " + e.getMessage()); } } }
import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; public class Server { public static void main(String[] args) { try (ServerSocket serverSocket = new ServerSocket(8000)) { System.out.println("Server started"); while (true) { Socket socket = serverSocket.accept(); new Thread(() -> handleClient(socket)).start(); } } catch (IOException e) { System.out.println("Error: " + e.getMessage()); } } private static void handleClient(Socket socket) { // 处理客户端请求的逻辑 } } public class Client { public static void main(String[] args) { try (Socket socket = new Socket("localhost", 8000)) { // 发送请求与服务器通信的逻辑 } catch (IOException e) { System.out.println("Error: " + e.getMessage()); } } }
import javax.servlet.*; import javax.servlet.http.*; import java.io.IOException; public class HelloWorldServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<html>"); out.println("<body>"); out.println("<h1>Hello, world!</h1>"); out.println("</body>"); out.println("</html>"); } }
綜上所述,我們從Java的基礎知識入門開始,逐步深入學習了Java的各項技術要點,從而實現了從入門到精通Java技術堆疊的過程。透過不斷的實踐與學習,相信讀者在Java技術堆疊上的造詣也將持續提升。希望本文能為讀者提供一些指導與協助,讓大家更能掌握並應用Java技術。
以上是Java技術棧:從入門到精通的詳細內容。更多資訊請關注PHP中文網其他相關文章!