跳转到内容

变量与数据类型

java
public class HelloWorld {
  public static void main(String[] args) {
    // 定义变量 x, 值为 1
    int x = 1;

    // 变量赋值
    x = 100;

    // 输出变量的值
    System.out.println("变量 x 的值为: " + x);
  }
}
bash
cd code/02/02

# 编译
javac -encoding UTF-8 HelloWorld.java

# 运行
java HelloWorld
  • int 数据类型
  • x 变量名
  • = 变量赋值

基本数据类型

  • 整数类型: byteshortintlong
  • 浮点数类型: floatdouble
  • 布尔类型: boolean
  • 字符类型: char

整数类型

对于整数类型,Java 只定义了带符号的整型,因此,最高位的 bit 表示符号位(0 表示正数,1 表示负数)。各种整型能表示的最大范围如下:

  • byte:-128 ~ 127
  • short:-32768 ~ 32767
  • int:-2147483648 ~ 2147483647
  • long:-9223372036854775808 ~ 9223372036854775807
java
public class HelloInteger {
  public static void main(String[] args) {
    byte b1 = 127;
    byte b2 = -128;

    short s1 = 32767;
    short s2 = -32768;

    int i1 = 2147483647;
    int i2 = -2147483648;
    int i3 = 2_000_000_000; // 加下划线更容易识别
    int i4 = 0xff0000; // 十六进制 0x 开头
    int i5 = 0b1000000000; // 二进制 0b 开头

    long l1 = 9000000000000000000L; // long 型的结尾需要加 L
    long l2 = 900; // 没有加 L,此处 900 为 int,但 int 类型可以赋值给 long
    // int l6 = 900L; // 错误:不能把 long 型赋值给 int

    System.out.println(i1);
    System.out.println(i4);
  }
}
bash
cd code/02/02

# 编译
javac -encoding UTF-8 HelloInteger.java

# 运行
java HelloInteger

浮点数类型

浮点类型的数就是小数,因为小数用科学计数法表示的时候,小数点是可以 “浮动” 的,如 1234.5 可以表示成 12.345x102,也可以表示成 1.2345x103,所以称为浮点数。

java
public class HelloFloat {
  public static void main(String[] args) {
    float f1 = 3.14f; // float 型需要加 f
    float f2 = -2.5f;

    double d1 = 3.14; // double 型不需要加后缀
    double d2 = -2.5;

    // 科学记数法
    float f3 = 1.23e3f; // 1.23 * 10^3
    float f4 = 1.23e-3f; // 1.23 * 10^-3

    System.out.println(f1);
    System.out.println(f2);
  }
}
bash
cd code/02/02

# 编译
javac -encoding UTF-8 HelloFloat.java

# 运行
java HelloFloat

布尔类型

布尔类型 boolean 只有 true 和 false 两个值,布尔类型总是关系运算的计算结果:

java
public class HelloBoolean {
  public static void main(String[] args) {
    boolean b1 = true;
    boolean b2 = false;

    boolean isGreater = 5 > 3; // 计算结果为 true

    int age = 12;
    boolean isAdult = age >= 18; // 计算结果为 false

    System.out.println(isGreater);
    System.out.println(isAdult);
  }
}
bash
cd code/02/02

# 编译
javac -encoding UTF-8 HelloBoolean.java

# 运行
java HelloBoolean

字符类型

字符类型 char 表示一个字符。Java 的 char 类型除了可表示标准的 ASCII 外,还可以表示一个 Unicode 字符:

java
public class HelloChar {
  public static void main(String[] args) {
    char c1 = 'A';
    char c2 = '中';
    char c3 = '\u0041'; // Unicode 字符
    char c4 = '\t'; // 制表符

    System.out.println(c1);
    System.out.println(c2);
    System.out.println(c3);
    System.out.println(c4);
  }
}
bash
cd code/02/02

# 编译
javac -encoding UTF-8 HelloChar.java

# 运行
java HelloChar

引用数据类型

在 Java 中,引用数据类型是指那些不直接存储值,而是存储指向内存中对象地址的变量类型。

除了 8 种基本数据类型(byte、short、int、long、float、double、char、boolean)外,其余所有的数据类型都是引用数据类型。

主要可以分为以下四大类:

  • 类 Class
  • 接口 Interface
  • 数组 Array
  • 枚举 Enum

类(Class)

这是最常用的引用类型。任何使用 new 关键字创建的对象都属于类类型。

比较常见的: String(字符串)、Integer(包装类)、Scanner、BigDecimal 等。

java
public class HelloClass {
  public static void main(String[] args) {
    String name = new String("小明");
    Intgeger age = new Intgeger(18);

    System.out.println(name);
    System.out.println(age);
  }
}
bash
cd code/02/02

# 编译
javac -encoding UTF-8 HelloClass.java

# 运行
java HelloClass

自定义的类:

java
public class HelloStudent {
  public static void main(String[] args) {
    // 创建 Student 对象
    Student student = new Student("小明", 18);

    System.out.println(student);
  }
}
bash
cd code/02/02

# 编译
javac -encoding UTF-8 HelloStudent.java

# 运行
java HelloStudent
java
// 自定义类 Student
public class Student {
  private String name;
  private int age;

  // 构造方法
  public Student(String name, int age) {
    this.name = name;
    this.age = age;
  }

  // 重写 toString 方法
  public String toString() {
    return "Student{name=" + name + ", age=" + age + "}";
  }
}

接口(Interface)

接口类型的引用可以指向任何实现了该接口的类的对象。

java
// 导入依赖包
import java.util.List;
import java.util.ArrayList;

public class HelloInterface {
  public static void main(String[] args) {
    // List 是接口, ArrayList 是实现了 List 接口的类
    List<String> list = new ArrayList<>();
    list.add("Hello");
    list.add("World");

    System.out.println(list);
    System.out.println(list.get(0));
    System.out.println(list.get(1));
  }
}
bash
cd code/02/02

# 编译
javac -encoding UTF-8 HelloInterface.java

# 运行
java HelloInterface

数组(Array)

数组在 Java 中是一个对象,因此也是引用数据类型。包括一维数组、二维数组以及对象数组。

java
public class HelloArray {
  public static void main(String[] args) {
    int[] a = new int[5];
    String[] s = new String[5];

    a[0] = 1;
    a[1] = 2;

    s[0] = "Hello";
    s[1] = "World";

    System.out.println(a[0]);
    System.out.println(a[1]);
    System.out.println(s[0]);
    System.out.println(s[1]);

  }
}
bash
cd code/02/02

# 编译
javac -encoding UTF-8 HelloArray.java

# 运行
java HelloArray

枚举(Enum)

使用 enum 关键字定义的类型。枚举常量也类似于静态对象引用。

java
public class HelloEnum {
  public static void main(String[] args) {
    // 引用 Color 枚举值
    System.out.println(Color.BLUE);
    System.out.println(Color.RED);
    System.out.println(Color.GREEN);

    // 引用 Gender 枚举值
    System.out.println(Gender.MALE);
    System.out.println(Gender.FEMALE);
    System.out.println(Gender.UNKNOWN);

    // 引用 Weekday 枚举值
    System.out.println(Weekday.MON);
    System.out.println(Weekday.TUE);
    System.out.println(Weekday.WED);
  }
}
bash
cd code/02/02

# 编译
javac -encoding UTF-8 HelloEnum.java

# 运行
java HelloEnum
java
// 定义枚举类型 Color
public enum Color {
  RED, GREEN, BLUE
}
java
// 定义枚举类型 Gender
public enum Gender {
  MALE, FEMALE, UNKNOWN
}
java
// 定义枚举类型 Weekday
public class Weekday {
  public static final int SUN = 0;
  public static final int MON = 1;
  public static final int TUE = 2;
  public static final int WED = 3;
  public static final int THU = 4;
  public static final int FRI = 5;
  public static final int SAT = 6;
}

基于 MIT 许可发布