Java Technology Guide: A Comprehensive Overview from Basics to Advanced
Introduction:
Java is a widely used programming language that is cross-platform and object-oriented. and a strong ecosystem among many other advantages. This article will gradually introduce the core concepts and main technologies of Java from basic to advanced, and provide specific code examples to help readers get started quickly and understand Java programming in depth.
Part 1: Basic knowledge of Java
int age = 25; double price = 12.5; char grade = 'A'; boolean isValid = true; String name = "John";
if (score >= 90) { System.out.println("优秀"); } else if (score >= 80) { System.out.println("良好"); } else { System.out.println("及格"); } for (int i = 0; i < 10; i++) { System.out.println(i); } while (count > 0) { System.out.println(count); count--; }
int[] numbers = new int[5]; numbers[0] = 1; numbers[1] = 2; numbers[2] = 3; numbers[3] = 4; numbers[4] = 5; String[] names = {"Alice", "Bob", "Charlie"};
Part 2: Object-Oriented Programming
public class Person { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } public void sayHello() { System.out.println("Hello, my name is " + name); } } Person person = new Person("John", 25); person.sayHello();
public class Animal { public void makeSound() { System.out.println("Animal makes sound"); } } public class Cat extends Animal { @Override public void makeSound() { System.out.println("Cat says Meow"); } } Animal animal = new Cat(); animal.makeSound();
public interface Drawable { void draw(); } public abstract class Shape implements Drawable { protected int x; protected int y; public Shape(int x, int y) { this.x = x; this.y = y; } @Override public void draw() { System.out.println("Drawing shape at (" + x + ", " + y + ")"); } public abstract double area(); } public class Circle extends Shape { private double radius; public Circle(int x, int y, double radius) { super(x, y); this.radius = radius; } @Override public double area() { return Math.PI * radius * radius; } } Shape shape = new Circle(0, 0, 5); shape.draw(); System.out.println("Area: " + shape.area());
Part 3: Java advanced features
try { int result = divide(10, 0); System.out.println(result); } catch (ArithmeticException e) { System.out.println("Error: " + e.getMessage()); } public int divide(int num1, int num2) { if (num2 == 0) { throw new ArithmeticException("Divisor cannot be zero"); } return num1 / num2; }
try (BufferedReader reader = new BufferedReader(new FileReader("file.txt"))) { String line; while ((line = reader.readLine()) != null) { System.out.println(line); } } catch (IOException e) { System.out.println("Error: " + e.getMessage()); } try (BufferedWriter writer = new BufferedWriter(new FileWriter("file.txt"))) { writer.write("Hello, World!"); } catch (IOException e) { System.out.println("Error: " + e.getMessage()); }
public class MyThread extends Thread { @Override public void run() { for (int i = 0; i < 10; i++) { System.out.println(Thread.currentThread().getName() + ": " + i); } } } MyThread thread1 = new MyThread(); MyThread thread2 = new MyThread(); thread1.start(); thread2.start();
Conclusion:
This article provides a comprehensive overview of Java technology from Java basics, object-oriented programming to Java advanced features, and provides specific code examples. I hope this article can help readers get started quickly and understand Java programming in depth, and lay a good foundation for further learning Java.
The above is the detailed content of Comprehensive understanding of Java technology: A guide from beginner to proficient. For more information, please follow other related articles on the PHP Chinese website!