Java编程从入门到放弃
1.配置开发环境
安装JDK
官网下载地址: https://www.oracle.com/java/technologies/downloads/
配置环境变量
最新版本JDK22无需手动配置环境变量。
老版本: 此电脑-右键属性-高级系统设置-环境变量-系统变量-Path-编辑 C:\Java\jdk1.8.0_65\bin
检查结果 java -version
Hello World
//HelloWorld.java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
# 编译
javac HelloWorld.java
# 执行
java HelloWorld
安装 IntelliJ IDEA
下载地址: https://www.jetbrains.com/idea/download/?section=windows
安装配置 Maven
2. 基础语法
2.1 数据类型
基本数据类型
Data Type | Size | Description |
---|---|---|
byte | 1 byte | -128 to 127 |
short | 2 bytes | -32,768 to 32,767 |
int | 4 bytes | -2,147,483,648 to 2,147,483,647 |
long | 8 bytes | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
float | 4 bytes | Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits |
double | 8 bytes | Stores fractional numbers. Sufficient for storing 15 decimal digits |
boolean | 1 bit | true or false |
char | 2 bytes | Stores a single character/letter or ASCII values |
类型转换
-
自动类型转换
byte -> short -> char -> int -> long -> float -> double -
手动类型转换
double -> float -> long -> int -> char -> short -> byte
数组
String[] cars = {"Volvo", "BMW", "Ford"};
2.2 流程控制
If ... Else
if (condition) {
// block of code to be executed if the condition is true
} else {
// block of code to be executed if the condition is false
}
// 三元运算符
variable = (condition) ? expressionTrue : expressionFalse;
Switch
switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
While Loop
while (condition) {
// code block to be executed
}
do {
// code block to be executed
}
while (condition);
For Loop
for (statement 1; statement 2; statement 3) {
// code block to be executed
}
// For-Each
for (type variableName : arrayName) {
// code block to be executed
}
2.3 函数方法
public class Main {
static void myMethod() {
System.out.println("I just got executed!");
}
public static void main(String[] args) {
myMethod();
}
}
3. 面向对象
3.1 类与对象
属性与方法
public class Main {
int x = 5; // 属性
// 方法
static void myMethod() {
System.out.println("Hello World!");
}
public static void main(String[] args) {
Main myObj1 = new Main();
myObj1.x = 25;
System.out.println(myObj1.x);
myMethod();
}
}
构造函数
public class Main {
int x;
public Main(int y) {
x = y;
}
public static void main(String[] args) {
Main myObj = new Main(5);
System.out.println(myObj.x);
}
}
修饰符
访问修饰符
For classes, you can use either public or default:
Modifier | Description |
---|---|
public | The class is accessible by any other class |
default | The class is only accessible by classes in the same package |
For attributes, methods and constructors, you can use the one of the following:
Modifier | Description |
---|---|
public | The code is accessible for all classes |
private | The code is only accessible within the declared class |
default | The code is only accessible in the same package |
protected | The code is accessible in the same package and subclasses |
非访问修饰符
For classes, you can use either final or abstract:
Modifier | Description |
---|---|
final | The class cannot be inherited by other classes |
abstract | The class cannot be used to create objects |
For attributes and methods, you can use the one of the following:
Modifier | Description |
---|---|
final | Attributes and methods cannot be overridden/modified |
static | Attributes and methods belongs to the class, rather than an object |
abstract | Can only be used in an abstract class, and can only be used on methods |
transient | Attributes and methods are skipped when serializing the object containing them |
synchronized | Methods can only be accessed by one thread at a time |
transient | The value of an attribute is not cached thread-locally, and is always read from the "main memory" |
3.2 封装
public class Person {
private String name; // private = restricted access
// Getter
public String getName() {
return name;
}
// Setter
public void setName(String newName) {
this.name = newName;
}
}
3.2 继承
class Vehicle {
protected String brand = "Ford"; // Vehicle attribute
public void honk() { // Vehicle method
System.out.println("Tuut, tuut!");
}
}
class Car extends Vehicle {
private String modelName = "Mustang"; // Car attribute
public static void main(String[] args) {
// Create a myCar object
Car myCar = new Car();
// Call the honk() method (from the Vehicle class) on the myCar object
myCar.honk();
// Display the value of the brand attribute (from the Vehicle class) and the value of the modelName from the Car class
System.out.println(myCar.brand + " " + myCar.modelName);
}
}
3.3 多态
class Animal {
public void animalSound() {
System.out.println("The animal makes a sound");
}
}
class Pig extends Animal {
public void animalSound() {
System.out.println("The pig says: wee wee");
}
}
class Dog extends Animal {
public void animalSound() {
System.out.println("The dog says: wow wow");
}
}
class Main {
public static void main(String[] args) {
Animal myAnimal = new Animal(); // Create a Animal object
Animal myPig = new Pig(); // Create a Pig object
Animal myDog = new Dog(); // Create a Dog object
myAnimal.animalSound();
myPig.animalSound();
myDog.animalSound();
}
}
3.4 抽象
抽象类和方法
// Abstract class
abstract class Animal {
// Abstract method (does not have a body)
public abstract void animalSound();
// Regular method
public void sleep() {
System.out.println("Zzz");
}
}
// Subclass (inherit from Animal)
class Pig extends Animal {
public void animalSound() {
// The body of animalSound() is provided here
System.out.println("The pig says: wee wee");
}
}
class Main {
public static void main(String[] args) {
Pig myPig = new Pig(); // Create a Pig object
myPig.animalSound();
myPig.sleep();
}
}
4. 进阶
多线程
class ThreadDemo extends Thread {
private Thread t;
private String threadName;
ThreadDemo( String name) {
threadName = name;
System.out.println("Creating " + threadName );
}
public void run() {
System.out.println("Running " + threadName );
try {
for(int i = 4; i > 0; i--) {
System.out.println("Thread: " + threadName + ", " + i);
// 让线程睡眠一会
Thread.sleep(50);
}
}catch (InterruptedException e) {
System.out.println("Thread " + threadName + " interrupted.");
}
System.out.println("Thread " + threadName + " exiting.");
}
public void start () {
System.out.println("Starting " + threadName );
if (t == null) {
t = new Thread (this, threadName);
t.start ();
}
}
}
public class TestThread {
public static void main(String args[]) {
ThreadDemo T1 = new ThreadDemo( "Thread-1");
T1.start();
ThreadDemo T2 = new ThreadDemo( "Thread-2");
T2.start();
}
}