西维蜀黍

【Java】String - String,StringBuilder 和 StringBuffer

String类

打开 String 类就会发现,它是被 final 修饰的:

  ...


【Java】装箱(Boxing)与拆箱(Unboxing)

背景

基本数据类型

基本类型,或者叫做内置类型,是Java中不同于类(Class)的特殊类型。它们是我们编程中使用最频繁的类型。

Java是一种强类型语言,第一次申明变量必须说明数据类型,第一次变量赋值称为变量的初始化。

  ...


【Java】String - String 类和常量池

String 类

概览

String 被声明为 final,因此它不可被继承。

在 Java 8 中,String 内部使用 char 数组存储数据。

public final class String
    implements java.io.Serializable, Comparable<String>, CharSequence {
    /** The value is used for character storage. */
    private final char value[];
}Copy to clipboardErrorCopied

在 Java 9 之后,String 类的实现改用 byte 数组存储字符串,同时使用 coder 来标识使用了哪种编码。

public final class String
    implements java.io.Serializable, Comparable<String>, CharSequence {
    /** The value is used for character storage. */
    private final byte[] value;

    /** The identifier of the encoding used to encode the bytes in {@code value}. */
    private final byte coder;
}Copy to clipboardErrorCopied

value 数组被声明为 final,这意味着 value 数组初始化之后就不能再引用其它数组。并且 String 内部没有改变 value 数组的方法,因此可以保证 String 不可变。

String 对象的两种创建方式

String str1 = "abcd";
String str2 = new String("abcd");
String str3 = new String("abcd");

System.out.println(str1 == str2); //false
System.out.println(str1 == str3); //false
System.out.println(str2 == str3); //false

这两种不同的创建 String 对象实例的方法是有差别的:

  • 第一种方式是直接指向一个**字符串常量池(String Constant Pool)**中的"abcd"字符串对象的引用;
  • 第二种方式是直接在堆(Heap)内存中创建一个新的对象,而这个对象的 value 指向了"abcd"字符串对象。
  ...


【Java】多线程-Callable、Future 和 FutureTask

背景

我们知道,创建线程类有 2 种方式,一种是直接继承 Thread,另外一种就是实现 Runnable 接口。

这 2 种方式都有一个缺陷就是:在执行完任务之后无法获取执行结果。

  ...


【Java】JVM-双亲委派模型(Parents Delegation Model)

双亲委派模型(Parents Delegation Model)

**双亲委派模型(Parents Delegation model)**要求:除了顶层的启动类加载器(BootStrap ClassLoader)外,其余的类加载器都应当有自己的父类加载器。

  ...


【Java】JVM-自定义类加载器

自定义类加载器中的方法

findClass()

findClass() 方法是自定义类加载器类必须重写(override)的方法,用于告诉自定义类加载器到哪里去加载类,比如某个目录或者 JAR URL等。参数name 为要加载的类全名,如 java.lang.String。

  ...


【Java】JVM - JVM 内存区域(Memory Area)

JVM 内存区域

JVM 内存区域 主要由**运行时数据区域(Runtime data area)、类加载器子系统(Class Loader Subsystem)和执行引擎(Execution Engine)**组成。

  ...


【Java】JVM - 类加载机制(Class Load Mechanism)

背景

虚拟机把描述类的数据从 Class 文件加载到内存,并对数据进行校验、转换解析和初始化,最终形成可以被虚拟机直接使用的 Java 类型,这就是虚拟机的类加载机制(ClassLoad Mechanism)。

  ...


【Java】JVM-类加载器(Class Loader)

类加载器(Class Loader)

在 Java 中,默认提供的三种类加载器,分别是 BootStrapClassLoader(启动类加载器)、ExtClassLoader(扩展类加载器)、AppClassLoader(应用程序类加载器)。

同时,我们也可以定义自己的类加载器 (CustomClassLoader),那么它的 parent 肯定就是 AppClassLoader(应用程序类加载器)了。类加载器的这种层次关系称为双亲委派模型(Parents Delegation Model)。

  ...


【Java】JVM 入门

前言

A Java virtual machine (JVM) is a virtual machine that enables a computer to run Java programs as well as programs written in other languages that are also compiled to Java bytecode. The JVM is detailed by a specification that formally describes what is required in a JVM implementation. Having a specification ensures interoperability of Java programs across different implementations so that program authors using the Java Development Kit (JDK) need not worry about idiosyncrasies of the underlying hardware platform.

The JVM reference implementation is developed by the OpenJDK project as open source code and includes a JIT compiler called HotSpot. The commercially supported Java releases available from Oracle are based on the OpenJDK runtime. Eclipse OpenJ9 is another open source JVM for OpenJDK.

  ...