Java learning route (super detailed)

Table of contents

Foreword:

1. Learn the basics of Java:

2. Master the concept of object-oriented programming (OOP):

3. Familiar with the Java collection framework:

4. Learn Java exception handling:

5. Learn Java IO:

6. Master Java multi-threaded programming:

7. Learn Java network programming:

8. Learn Java database programming:

9. Learn the Java framework:


Foreword:

Why learn java, some people say "java is the best programming language", for a programmer, learning java is essential, as a novice Java development engineer, you need to read my Java learning path, let you For the learning route and a deeper understanding of Java, this route includes some basic module codes in the learning process, which can allow you to understand more intuitively what you need to learn, and go straight to the dry goods without further ado;

1. Learn the basics of Java:

When learning the basics of Java, you need to understand the basics of Java's syntax, variables and data types, control structures, arrays, strings, and input/output operations.

The syntax of Java is relatively simple and easy to understand, suitable for beginners to get started. At the same time, Java is also a strongly typed language, so you need to pay attention to the conversion of data types when using it.

 Java basic syntax sample code:

public class HelloWorld {
  public static void main(String[] args) {
    System.out.println("Hello, World!");
  }
}

Java variable and data type sample code:

int age = 20;
double score = 90.5;
String name = "Tom";
boolean isMale = true;

Java control structure sample code:

if (age > 18) {
  System.out.println("Adult");
} else if (age > 12) {
  System.out.println("Teenager");
} else {
  System.out.println("Child");
}

Java array sample code:

int[] nums = {1, 2, 3, 4};
String[] names = new String[3];
names[0] = "Tom";
names[1] = "Jerry";
names[2] = "Lucy";

Java string manipulation sample code:

String str1 = "Hello";
String str2 = "world";
String str3 = str1.concat(str2);
System.out.println(str3); // Output: HelloWorld

Java input/output operation sample code:

Scanner scanner = new Scanner(![img](file:///C:\Users\张远浩\AppData\Roaming\Tencent\QQ\Temp\%W@GJ$ACOF(TYDYECOKVDYB.png)System.in);
System.out.println("Please input your name:");
String name = scanner.nextLine();
System.out.println("Hello, " + name);

2. Master the concept of object-oriented programming (OOP):

Object-oriented programming is at the core of Java, so you need a deep understanding of object-oriented programming principles such as encapsulation, inheritance, and polymorphism, and learn to program using classes and objects.

When learning object-oriented programming, you also need to understand concepts such as abstract classes and interfaces, inner classes, and enumerations in Java, which are important components of object-oriented programming.

 Master object-oriented programming (OOP) concepts:

Java class and object sample code:

public class Person {
  private String name;
  private int age;

AppData\Roaming\Tencent\QQ\Temp\%W@GJ$ACOF(TYDYECOKVDYB.png)]this.name = name;
    this.age = age;
  }

  public void sayHello() {
    System.out.println("Hello, my name is " + name);
  }
}

Person person = new Person("Tom", 20);
person.sayHello();

Java inheritance and polymorphism sample code:

public class Animal {
  public void eat() {
    System.out.println("Animal is eating");
  }
}

public class Cat extends Animal {
  @Override
  public void eat() {
    System.out.println("Cat is eating fish");
  }
}

Animal animal = new Animal();
Cat cat = new Cat();
animal.eat(); // Output: Animal is eating
cat.eat(); // Output: Cat is eating fish

Java abstract class and interface sample code:

public abstract class Shape {
  public abstract double getArea();
}

public interface Drawable {
  void draw();
}

public class Circle extends Shape implements Drawable {
  private double radius;

  public Circle(double radius) {
    this.radius = radius;
  }

  @Override
  public double getArea() {
    return Math.PI * radius * radius;
  }

  @Override
  public void draw() {
    System.out.println("Drawing circle...");
  }
}

Circle circle = new Circle(5);
System.out.println(circle.getArea()); // Output: 78.53981633974483
circle.draw(); // Output: Drawing circle...

3. Familiar with the Java collection framework:

Java provides a variety of data structures, such as arrays, linked lists, queues, stacks, hash tables, and trees, etc. These data structures are integrated in the Java collection framework.

To learn the Java collection framework, you need to understand the characteristics of collection classes, common collection classes and their usage methods, and their performance optimization techniques.

 Java collection framework sample code:

List<String> list = new ArrayList<>();
list.add("Tom");
list.add("Jerry");
list.add("Lucy");
for (String str : list) {
  System.out.println(str);
}

Map<String, Integer> map = new HashMap<>();
map.put("Tom", 20);
map.put("Jerry", 18);
map.put("Lucy", 22);
for (Map.Entry<String, Integer> entry : map.entrySet()) {
  System.out.println(entry.getKey() + " -> " + entry.getValue());
}

4. Learn Java exception handling:

When writing Java programs, you may encounter various exceptions, such as null pointer exceptions, array out-of-bounds exceptions, etc. Java's exception handling mechanism can help us catch and handle these exceptions.

To learn Java exception handling, you need to understand exception classification, exception capture and handling in Java to ensure the robustness and reliability of the program.

 Java exception handling sample code:

try {
  int a = 10 / 0;
} catch (ArithmeticException e) {
  System.out.println("Divide by zero");
} finally {
  System.out.println("Finally");
}

5. Learn Java IO:

In Java, input and output operations are very common operations, such as reading files, network communication, etc.

To learn Java IO, you need to understand the input and output technologies in Java, including file reading and writing, network communication, and serialization.

 

 Java file read and write sample code:

try (BufferedReader reader = new BufferedReader(new FileReader("file.txt"))) {
  String line;
  while ((line = reader.readLine()) != null) {
    System.out.println(line);
  }
} catch (IOException e) {
  e.printStackTrace();
}

6. Master Java multi-threaded programming:

Java supports multi-thread programming, and using multi-thread can improve the efficiency and concurrency of the program.

To learn Java multithreaded programming, you need to master the basic concepts of multithreaded programming, thread synchronization and mutual exclusion, thread pool and thread safety, etc.

 Java multithreading sample code:

public class MyThread extends Thread {
  @Override
  public void run() {
    for (int i = 0; i < 10; i++) {
      System.out.println("Thread 1: " + i);
    }
  }
}

MyThread thread1 = new MyThread();
thread1.start();

for (int i = 0; i < 10; i++) {
  System.out.println("Main thread: " + i);
}

Java thread synchronization and mutual exclusion sample code:

public class Counter {
  private int count;

  public synchronized void increment() {
    count++;
  }

  public int getCount() {
    return count;
  }
}

Counter counter = new Counter();
Runnable task = () -> {
  for (int i = 0; i < 10000; i++) {
    counter.increment();
  }
};
Thread[] threads = new Thread[10];
for (int i = 0; i < threads.length; i++) {
  threads[i] = new Thread(task);
  threads[i].start();
}
for (int i = 0; i < threads.length; i++) {
  threads[i].join();
}
System.out.println(counter.getCount());

Java thread pool sample code:

ExecutorService executor = Executors.newFixedThreadPool(2);
Runnable task = () -> {
  for (int i = 0; i < 10; i++) {
    System.out.println(Thread.currentThread().getName() + ": " + i);
  }
};
executor.submit(task);
executor.submit(task);
executor.shutdown();

Java thread safe sample code:

public class SafeCounter {
  private AtomicInteger count = new AtomicInteger(0);

  public void increment() {
    count.getAndIncrement();
  }

  public int getCount() {
    return count.get();
  }
}

SafeCounter counter = new SafeCounter();
Runnable task = () -> {
  for (int i = 0; i < 10000; i++) {
    counter.increment();
  }
};
Thread[] threads = new Thread[10];
for (int i = 0; i < threads.length; i++) {
  threads[i] = new Thread(task);
  threads[i].start();
}
for (int i = 0; i < threads.length; i++) {
  threads[i].join();
}
System.out.println(counter.getCount());

7. Learn Java network programming:

Network programming technology in Java is widely used. When learning Java network programming, you need to understand Socket, HTTP protocol, TCP/IP protocol and UDP protocol in Java to realize network communication.

 Java Socket sample code:

try (Socket socket = new Socket("localhost", 8080)) {
  OutputStream output = socket.getOutputStream();
  output.write("Hello, server!".getBytes());
  InputStream input = socket.getInputStream();
  byte[] buffer = new byte[1024];
  int len = input.read(buffer);
  System.out.println(new String(buffer, 0, len));
} catch (IOException e) {
  e.printStackTrace();
}

Java HTTP protocol sample code:

try {
  URL url = new URL("![img](file:///C:\Users\张远浩\AppData\Roaming\Tencent\QQTempSys\[5UQ[BL(6~BS2JV6W}N6[%S.png)http://www.baidu.com");
  HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  conn.setRequestMethod("GET");
  BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
  String line;
  while ((line = reader.readLine()) != null) {
    System.out.println(line);
  }
  reader.close();
} catch (IOException e) {
  e.printStackTrace();
}

Java TCP/IP protocol sample code:

try (ServerSocket serverSocket = new ServerSocket(8080)) {
  Socket socket = serverSocket.accept();
  InputStream input = socket.getInputStream();
  byte[] buffer = new byte[1024];
  int len = input.read(buffer);
  System.out.println(new String(buffer, 0, len));
  OutputStream output = socket.getOutputStream();
  output.write("Hello, client!".getBytes());
} catch (IOException e) {
  e.printStackTrace();
}

Java UDP protocol sample code:

try (DatagramSocket socket = new DatagramSocket()) {
  String message = "Hello, server!";
  InetAddress address = InetAddress.getByName("localhost");
  byte[] data = message.getBytes();
  DatagramPacket packet = new DatagramPacket(data, data.length, address, 8080);
  socket.send(packet);
  byte[] buffer = new byte[1024];
  packet = new DatagramPacket(buffer, buffer.length);
  socket.receive(packet);
  System.out.println(new String(packet.getData(), 0, packet.getLength()));
} catch (IOException e) {
  e.printStackTrace();
}

8. Learn Java database programming:

Database is one of the indispensable components in web application, so it is very necessary to learn Java database programming.

When learning Java database programming, you need to understand JDBC, SQL language, transaction processing, etc., in order to realize the operation of the database.

 Java JDBC sample code:

try (Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456")) {
  Statement stmt = conn.createStatement();
  String sql = "SELECT * FROM user";
  ResultSet rs = stmt.executeQuery(sql);
  while (rs.next()) {
    System.out.println(rs.getInt("id") + "\t" + rs.getString("name") + "\t" + rs.getInt("age"));
  }
} catch (SQLException e) {
  e.printStackTrace();
}

Java connection pool sample code:

DataSource dataSource = new BasicDataSource();
((BasicDataSource) dataSource).setUrl("jdbc:mysql://localhost:3306/test");
((BasicDataSource) dataSource).setUsername("root");
((BasicDataSource) dataSource).setPassword("123456");
try (Connection conn = dataSource.getConnection()) {
  Statement stmt = conn.createStatement();
  String sql = "SELECT * FROM user";
  ResultSet rs = stmt.executeQuery(sql);
  while (rs.next()) {
    System.out.println(rs.getInt("id") + "\t" + rs.getString("name") + "\t" + rs.getInt("age"));
  }
} catch (SQLException e) {
  e.printStackTrace();
}

9. Learn the Java framework:

There are many commonly used frameworks in Java, such as Spring, Hibernate, Struts, etc. These frameworks can help us develop faster and improve the efficiency and quality of programs;

The Java framework is not easy to display. You can refer to my spring boot column to learn the spring boot framework. This is a powerful spring series framework that is very friendly to novices;

If you want to learn the spring framework in depth, you can also learn springmvc, tomcat, mybatis, mybatis-plus, etc.;

To learn the Java framework, you need to have a deep understanding of the principles, common functions and usage methods of the framework;

Guess you like

Origin blog.csdn.net/weixin_59367964/article/details/130634325
Recommended