小编ilk*_*ker的帖子

接口和类对象内存分配的区别

假设有A接口和B类,B类实现了接口;

interface A {
  void hello();
}

class B implements A {
  public int justAField;

  @Override
  public void hello() {
    System.out.println("Hi.");
  }

  public void anotherMethod() {
    System.out.println("Another one.");
  }
}
Run Code Online (Sandbox Code Playgroud)

假设我们有这两个对象;

A typeInterface = new B();
B typeClass = new B();
Run Code Online (Sandbox Code Playgroud)

我的问题是,当编译器编译代码和内存分配开始时,我们有两个对象对吗?但是一种是类型A,一种是类型B,这意味着'typeInterface'将只有一个方法,而'typeClass'将包含一个多字段和一个多方法。

这两个对象分配相同数量的内存还是“typeInterface”基本上消耗更少的内存?

java oop jvm memory-management interface

4
推荐指数
1
解决办法
170
查看次数

标签 统计

interface ×1

java ×1

jvm ×1

memory-management ×1

oop ×1