1. Stack 栈
Stack in java is a section of memory which contains methods, local variables, and reference variables. Stack memory is always referenced in Last-In-First-Out order. Local variables are created in the stack.
- java 中存储 方法 局部变量 和 引用变量的地方, 遵循 后进先出/先进后出 原则。
- 如果栈空间不够了会出现StackOverflowError
怎么快速实现一个StackOverflowError的例子呢?
void testStackOverflow(){ testStackOverflow(); // Exception in thread "main" java.lang.StackOverflowError}
只需要重复调用一个方法,这些方法再调用他们自己,这样循环,因为方法存在栈中,最终会导致StackOverflowError
2. Heap 堆
Heap is a section of memory which contains Objects and may also contain reference variables. Instance variables are created in the heap
- 堆是存放对象的地方。
- 如果堆不够用了会出现OutOfMemoryError
怎么快速实现一个OutOfMemoryError的例子呢?
final List testIntList = new ArrayList<>();while (true) { testIntList.add(new TestInt()); // Exception in thread "main" java.lang.OutOfMemoryError: Java heap space}
这里要注意,如果你一味的创建对象,那么java的GC(GarbageCollection) 会把没有引用指向的对象回收。我的这个例子是一直向一个list里面加对象,直到堆内存溢出。
3. Java 基本类型
- 8 primary types.
- ( 1 Byte = 8 bits )
short s3 = 3;s3++; System.out.println(s3);s3 = s3 + 1; // compile error: required short but provided int
自加运算不会改变类型
byte b10 = 10;byte b20 = 20;// byte b30 = b10 + b20; // compile errorbyte b30 = (byte) (b10 + b20);
4. == 和 equals 以及 IntegerCache
== 比较的是两个变量的地址, equals 则根据实现不同IntegerCache -128 ~ 127final int a20 = 20;final int b20 = 20;System.out.println(a20 == b20); // truefinal int a200 = 200;final int b200 = 200;System.out.println(a200 == b200); // truefinal Integer c20 = 20;final Integer e20 = 20;System.out.println(c20 == e20); // trueSystem.out.println(c20.equals(e20)); // truefinal Integer f200 = 200;final Integer x200 = 200;System.out.println(f200 == x200); // falseSystem.out.println(f200.equals(x200)); // trueSystem.out.println(f200 == a200); // trueSystem.out.println(x200 == a200); // trueSystem.out.println(f200 == x200); // falseSystem.out.println(c20 == a20); // true
a20,b20,a200,b200 是基本类型,都是栈中的变量,指向相同的值c20,e20 是引用类型,但是会指向 IntegerCache 中相同的对象 Integer(20)f200,x200 是引用类型,超过了 IntegerCache 的范围,所以他们分别指向堆内存中的新对象如果一个引用变量和一个基本类型比较,那么比较的是他们的值
造成 == 比较 相同与不同的原因,我们来看看Integer的源码
@HotSpotIntrinsicCandidate public static Integer valueOf(int i) { return i >= -128 && i <= Integer.IntegerCache.high ? Integer.IntegerCache.cache[i + 128] : new Integer(i); }
如果要创建的Integer的int值在-128 和 high(一般是)127之间,那么返回的是IntegerCache数组中的Integer对象。如果不在这个范围,返回的是创建在堆中的新对象。
private static class IntegerCache { static final int low = -128; static final int high; static final Integer[] cache; static Integer[] archivedCache;
IntergerCache 是Integer中的一个静态内部类,cache又是一个静态数组,所以唯一且在栈中。
同样 Long,Short,等也有cache。Cache缓存的作用呢就是快,节约了创建对象的时间与空间。
为了防止出错,建议大家引用类型相比一定要用equals,而基本类型只能用==。
Kotlin 才是万物皆对象
以前听说过java中万物皆对象,不对啊,基本类型(或叫原始类型)不是啊!没有继承Object啊。
Kotlin 呢, 只有 Int 类类型,没有基本类型。
看个例子
val c20 = 20 val e20 = 20 println(c20 == e20) // true println(c20 === e20) // true println(c20.javaClass) // int val f200 = 200 val x200 = 200 println(f200 == x200) // true println(f200 === x200) // true println(f200.javaClass) // int
c20,e20,f200,x200都是Kotlin的Int型,java的原始int型,所以比较起来相等为true。
这里呢留个悬念,有机会再分析 Kotlin 对象的比较,== 和 === 的区别。
西瓜视频讲解地址,点击观看
欢迎大家关注我 [收藏] [点赞] [转发] ,谢谢,如果有问题可以评论或私信我哦。