Java 是一门由 Sun Microsystems(现为 Oracle)于 1995 年发布的高级编程语言。它以"一次编写,到处运行"(Write Once, Run Anywhere)的跨平台特性而闻名。
Java 概述 Java 的特点
跨平台 :通过 JVM(Java 虚拟机)实现
面向对象 :纯面向对象语言
自动内存管理 :垃圾回收机制
安全 :沙箱机制
多线程 :内置多线程支持
Java 的应用场景
企业级 Web 开发(Spring Boot)
Android 移动应用开发
大数据处理(Hadoop、Spark)
云计算(Hadoop、Docker)
桌面应用开发
JVM、JRE、JDK
JVM(Java Virtual Machine) :Java 虚拟机,负责执行 Java 字节码
JRE(Java Runtime Environment) :Java 运行时环境,包含 JVM 和核心类库
JDK(Java Development Kit) :Java 开发工具包,包含 JRE 和开发工具(编译器等)
第一个 Java 程序 1 2 3 4 5 6 public class HelloWorld { public static void main (String[] args ) { System.out.println("Hello, Java!" ); } }
编译与运行:
1 2 3 javac HelloWorld.java java HelloWorld Hello, Java!
基本语法 数据类型 1 2 3 4 5 6 7 8 9 byte b = 10 ; short s = 100 ; int i = 1000 ; long l = 1000000L ; float f = 3.14f ; double d = 3.14159 ; char c = 'A' ; boolean flag = true ;
变量与常量 1 2 3 4 5 int age = 25 ; final double PI = 3.14159 ; String name = "张三" ; name = "李四" ;
面向对象编程 类与对象 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 public class Person { private String name; private int age; public Person (String name, int age ) { this .name = name; this .age = age; } public void introduce () { System.out.println("我叫" + name + ",今年" + age + "岁。" ); } public int getAge () { return age; } public void setAge (int age ) { this .age = age; } }
1 2 3 4 5 Person p = new Person("张三" , 25 ); p.introduce(); p.setAge(26 ); System.out.println(p.getAge());
继承 1 2 3 4 5 6 7 8 9 10 public class Student extends Person { private String school; public Student (String name, int age, String school ) { super (name, age); this .school = school; } public void study () { System.out.println(getName() + "在" + school + "学习" );
多态 1 2 3 4 5 6 7 8 9 10 11 12 13 public abstract class Animal { public abstract void makeSound (); } public class Dog extends Animal { @Override public void makeSound () { System.out.println("汪汪汪" ); } } Animal animal = new Dog(); animal.makeSound();
接口 1 2 3 4 5 6 7 8 9 10 public interface Flyable { void fly(); int getMaxHeight(); } public class Bird implements Flyable { @Override public void fly () { System.out.println("鸟儿飞翔" ); } @Override public int getMaxHeight () { return 1000 ; }
集合框架 List 1 2 3 4 5 6 7 8 9 import java.util.ArrayList;import java.util.List;List<String> list = new ArrayList<>(); list.add("apple" ); list.add("banana" ); list.add("orange" ); for (String fruit : list) {
Map 1 2 3 4 5 6 7 import java.util.HashMap;import java.util.Map;Map<String, Integer> scores = new HashMap<>(); scores.put("张三" , 90 ); scores.put("李四" , 85 ); System.out.println(scores.get("张三" ));
Set 1 2 3 4 5 6 7 import java.util.HashSet;import java.util.Set;Set<Integer> numbers = new HashSet<>(); numbers.add(1 ); numbers.add(2 ); numbers.add(2 );
异常处理 1 2 3 4 5 6 7 8 9 10 11 try { int result = 10 / 0 ; } catch (ArithmeticException e) { System.out.println("算术错误: " + e.getMessage()); } finally { System.out.println("程序执行完毕" ); } throw new RuntimeException("自定义异常" );
多线程 继承 Thread 1 2 3 4 5 6 7 8 9 public class MyThread extends Thread { @Override public void run () { for (int i = 0 ; i < 10 ; i++) { System.out.println(Thread.currentThread().getName() + ": " + i); } } }
实现 Runnable 1 2 3 4 5 6 7 8 9 public class MyRunnable implements Runnable { @Override public void run () { } } Thread t = new Thread(new MyRunnable()); t.start();
线程池 1 2 3 4 5 6 7 8 import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;ExecutorService executor = Executors.newFixedThreadPool(5 ); for (int i = 0 ; i < 10 ; i++) { executor.submit(() -> System.out.println("任务执行" )); }
泛型 1 2 3 4 5 6 7 8 9 10 public class Box <T> { private T content; public Box (T content ) { this .content = content; } public T getContent () { return content; } }
1 2 Box<String> stringBox = new Box<>("Hello" ); Box<Integer> intBox = new Box<>(123 );
Lambda 表达式 1 2 3 4 5 6 7 List<String> names = Arrays.asList("张三" , "李四" , "王五" ); names.forEach(name -> System.out.println(name)); Collections.sort(names, (a, b) -> a.compareTo(b)); Runnable r = () -> System.out.println("Hello" );
总结 本文介绍了 Java 的基础知识,包括:
JVM、JRE、JDK 的概念
基本数据类型和变量
面向对象编程(类、对象、继承、多态)
接口
集合框架(List、Map、Set)
异常处理
多线程编程
泛型
Lambda 表达式
Java 是一门非常强大的编程语言,尤其是在企业级开发领域占据主导地位。掌握 Java 后,可以进一步学习 Spring Boot、微服务架构等热门技术。