Reference思考
原写于 2010-12-18
一. Java数据类型
数据类型就是对内存位置的抽象表达(很多编程语言都依赖于特定的计算机类型和对数据类型属性的具体编译实现,比如word和integer数据类型的大小等;Java通过JVM保证数据所占存储空间的大小不会随硬件的改变发生变化)。
1.Primitive data type :A primitive type is predefined by the language and is named by a reserved keyword. Primitive values do not share state with other primitive values. The eight primitive data types supported by the Java programming language。
原生类型是语言自身预先定义的,原生值不会与其他原生值共享状态。有8中原生类型被Java语言支持,如下:byte(8位)、short(16位)、int(32位)、long(64位)、float(浮点数,32位)、double(浮点数,64位)、boolean(1位,只有true或false可取)、char(16-bit Unicode character)
2.复杂数据类型
运行期动态创建,一切皆为对象。
- 接口(定义行为)
- 类(String objects are immutable, which means that once created, their values cannot be changed.)
- 数组
- 引用(软、弱、虚引用)
二. Reference分类
1. strong Reference :正常情况下的引用都属于strong引用,如:String str = new String();str是strong reference。其它引用必须是显式的声明,如:SoftReference softRef = new SoftReferencne(str),softRef是弱引用。
2. SoftReference :软引用。在内存溢出前,垃圾收集器将回收其引用的对象。
3. WeakReference :弱引用。任何垃圾回收,扫描到弱引用时,其引用的对象都会被回收。
4. PhantomReference :不理解。
Reference queues, to which registered reference objects are appended by the garbage collector after the appropriate reachability changes are detected.
当某个引用对象的可达性被垃圾搜集器改变或清除后,会被放入对应的 ReferenceQueue 中(如果注册引用队列)。
String str = new String("hello");
ReferenceQueue<String> refQuence = new ReferenceQueue<String>();
SoftReference<String> softRef = new SoftReference<String>(str, refQuence);
str = null; // 垃圾回收
如上例:当“hello”对象被收回后,则softRef会被加入到ReferenceQueue。
三.WeakHashMap简单分析
简化WeakHashMap结构: class WeakHashMap<K, V> { private final ReferenceQueue<K> queue = new ReferenceQueue<K>(); private Entry[] table; private static class Entry<K,V> extends WeakReference<K> implements Map.Entry<K,V> {} } 分析:WeakHashMap的put方法(Map对数据的保存也是通过数组实现) public V put(K key, V value) { K k = (K) maskNull(key); int h = HashMap.hash(k.hashCode()); Entry[] tab = getTable(); //获取table(关键:调用expungeStaleEntries方法,清洗table数组中,value所指的实际对象已被GC) int i = indexFor(h, tab.length); //根据key.hash计算在数组的位置 //从此位置开始,遍历单向链表,判断此位置是否已经存在相同元素 //1.有,更新为value //2.无,创建新的对象,置原头部元素为自己的next,让自身为单向链表头部 for (Entry<K,V> e = tab[i]; e != null; e = e.next) { //如果hash相等,且equals相等 if (h == e.hash && eq(k, e.get())) { V oldValue = e.value; if (value != oldValue) e.value = value; return oldValue; } } modCount++; Entry<K,V> e = tab[i]; //queue是ReferenceQueue,h自身key计算得到的hash值,e指向单链表下一个节点next tab[i] = new Entry<K,V>(k, value, queue, h, e); if (++size >= threshold) //判断数组容量是否需要扩充,策略:2倍 resize(tab.length * 2); return null; } /* * 清洗已被gc对象的引用,释放数组的容量 */ private void expungeStaleEntries() { Entry<K,V> e; while ( (e = (Entry<K,V>) queue.poll()) != null) { //从队列获取第一个加入ReferenceQueue的引用,循环 int h = e.hash; int i = indexFor(h, table.length); Entry<K,V> prev = table[i]; Entry<K,V> p = prev; while (p != null) { //检查单链表的数据是否已陈旧,清洗 Entry<K,V> next = p.next; if (p == e) { //若相等,说明引用的对象已被gc if (prev == e) table[i] = next; else prev.next = next; //因为其引用对象被垃圾收集器改变可达性时,也会被放入queue,所以强制设置其next、value为null,帮助gc收集其引用对象(不这样的理解正确不?请大家指点) e.next = null; // Help GC e.value = null; // " " size--; break; } prev = p; p = next; } } }
四.总结
引用也是属于Java的比较特殊的数据类型,对于软、弱引用的使用场景大多是“缓存”。
相关推荐
Your author likes to read reference manuals (believe it or not)—at least if they are reasonably complete—on the grounds that this is the most efficient way to absorb a language quickly.
Java reference sheet
Java in a Nutshell Java Language Reference Java AWT Reference Java Fundamental Classes Reference Exploring Java Combined Index Combined Search Web Version Credits
伯克利:A Java Reference Paul N. Hilfinger University of California, Berkeley 1 Java Overview 9 1.1 Basic Program Structure . . . . . . . . . . . . . . . . . . . . . . . . . 9 1.2 Compilation and ...
Neo4j Java Reference 3.0
主要为大家详细解析了Java Reference源码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
是一篇JAVA关于REFERENCE类使用的一篇文章,解析透彻,可以看看
Java: The Complete Reference, Eleventh Edition By 作者: Herbert Schildt ISBN-10 书号: 1260440230 ISBN-13 书号: 9781260440232 Edition 版本: 11 出版日期: 2018-12-12 pages 页数: (1955) The Definitive ...
neo4j-java-reference-3.3neo4j-java-reference-3.3neo4j-java-reference-3.3
Java The Complete Reference 10th Edition Java9 编程官方参考(第10版) 带书签 文字版
reference; for a more thorough coverage of Java, consult the Bibliographical Notes.We do not assume that you are familiar with object-oriented principles, such as classes, objects, and encapsulation, ...
neo4j java referece 3.3 , neo4j java referece 3.3 ,
Java The Complete Reference 9th pdf
Java The Complete Reference, 11th Edition 9781260440232 c.pdf Java The Complete Reference, 11th Edition 9781260440232 c.pdf Java The Complete Reference, 11th Edition 9781260440232 c.pdf
spring-javaconfig-reference Spring JavaConfig Reference Guide Version 1.0.0.m3 Copyright © 2005-2008 Rod Johnson, Costin Leau, Chris Beams About this document 1. Document structure Part I, ...
Java AWT 教程
这将是一个Java Swing应用程序,它将用作Java API名称的快速参考。 它将具有增量搜索和过滤功能,从而提高了搜索效率。 这将是非常简单并且是统计分析Java api的好方法。
neo4j-java-reference-3.0 不多说 都懂的
neo4j-java-reference-3.1(neo4j官方Java开发指南)..