Java源码总结整理注意点
Posted 低调的洋仔
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java源码总结整理注意点相关的知识,希望对你有一定的参考价值。
1. Throwable
是异常的鼻祖,下设Error 和Exception两大类异常,Exception又分为了两大类检查型和非检查型的异常。
2. Boolean
boolean中存储true和false全是常量final staic的,相当于缓存
public int hashCode()
return value ? 1231 : 1237;
public boolean equals(Object obj)
if (obj instanceof Boolean)
return value == ((Boolean)obj).booleanValue();
return false;
实现了serializable,comparable
public static int compare(boolean x, boolean y)
return (x == y) ? 0 : (x ? 1 : -1);
3. Byte
继承了Number实现了Comaparebale,Number里面实现了Serializable
Number主要定义了转为int,long,byte,short,float,double
从-128到127的值全部缓存在里面
存在compareTo方法。
public static int compare(byte x, byte y)
return x - y;
4. ClassLoader
protected Class<?> loadClass(String name, boolean resolve)
throws ClassNotFoundException
synchronized (getClassLoadingLock(name))
// First, check if the class has already been loaded
Class c = findLoadedClass(name);
if (c == null)
long t0 = System.nanoTime();
try
if (parent != null)
c = parent.loadClass(name, false);
else
c = findBootstrapClassOrNull(name);
catch (ClassNotFoundException e)
// ClassNotFoundException thrown if class not found
// from the non-null parent class loader
if (c == null)
// If still not found, then invoke findClass in order
// to find the class.
long t1 = System.nanoTime();
c = findClass(name);
// this is the defining class loader; record the stats
sun.misc.PerfCounter.getParentDelegationTime().addTime(t1 - t0);
sun.misc.PerfCounter.getFindClassTime().addElapsedTimeFrom(t1);
sun.misc.PerfCounter.getFindClasses().increment();
if (resolve)
resolveClass(c);
return c;
一般实现自定义的ClassLoader要实现findClass()
5 注解
元注解
@Target
@Retention
@Documented
@Inherit
本质上注解是一个接口,继承自Annotation接口
6. Double
继承了Number且实现了Comparator
没有缓存
public boolean equals(Object obj)
return (obj instanceof Double)
&& (doubleToLongBits(((Double)obj).value) ==
doubleToLongBits(value));
public static int compare(double d1, double d2)
if (d1 < d2)
return -1; // Neither val is NaN, thisVal is smaller
if (d1 > d2)
return 1; // Neither val is NaN, thisVal is larger
// Cannot use doubleToRawLongBits because of possibility of NaNs.
long thisBits = Double.doubleToLongBits(d1);
long anotherBits = Double.doubleToLongBits(d2);
return (thisBits == anotherBits ? 0 : // Values are equal
(thisBits < anotherBits ? -1 : // (-0.0, 0.0) or (!NaN, NaN)
1)); // (0.0, -0.0) or (NaN, !NaN)
7枚举
Enum
他的本质上是一个final的class内部存储的是一些常量,final static的常量
1)构造函数在构造枚举值的时候被构造。
2)构造器绝对是私有的。
3)所有枚举类都继承自Enum类
8.Float
继承自Number并实现了Comparable
有compareto方法,可以进行比较,但是内部实现是转为bit位整型值比较
public static int compare(float f1, float f2)
if (f1 < f2)
return -1; // Neither val is NaN, thisVal is smaller
if (f1 > f2)
return 1; // Neither val is NaN, thisVal is larger
// Cannot use floatToRawIntBits because of possibility of NaNs.
int thisBits = Float.floatToIntBits(f1);
int anotherBits = Float.floatToIntBits(f2);
return (thisBits == anotherBits ? 0 : // Values are equal
(thisBits < anotherBits ? -1 : // (-0.0, 0.0) or (!NaN, NaN)
1)); // (0.0, -0.0) or (NaN, !NaN)
public boolean equals(Object obj)
return (obj instanceof Float)
&& (floatToIntBits(((Float)obj).value) == floatToIntBits(value));
9. Integer
继承自Number并实现了Comparable接口
缓存了-128到127
public int hashCode()
return value;
public boolean equals(Object obj)
if (obj instanceof Integer)
return value == ((Integer)obj).intValue();
return false;
10. Long
继承自Number并实现了Comparable
缓存了-128到127数据
public static Long valueOf(long l)
final int offset = 128;
if (l >= -128 && l <= 127) // will cache
return LongCache.cache[(int)l + offset];
return new Long(l);
public int hashCode()
return (int)(value ^ (value >>> 32));
public boolean equals(Object obj)
if (obj instanceof Long)
return value == ((Long)obj).longValue();
return false;
11. Object
1.getClass
2.hashCode
3.equals
4.clone
5.toString
6.notify
7.notifyAll
8.wait
9.finalize
12.Short
继承自Number并实现了Comparable
缓存了-128到127De数据
private static class ShortCache
private ShortCache()
static final Short cache[] = new Short[-(-128) + 127 + 1];
static
for(int i = 0; i < cache.length; i++)
cache[i] = new Short((short)(i - 128));
public int hashCode()
return (int)value;
public boolean equals(Object obj)
if (obj instanceof Short)
return value == ((Short)obj).shortValue();
return false;
13. String
final
实现了Comparable和Serializable以及CharSequence
/** The value is used for character storage. */
private final char value[];
/** The offset is the first index of the storage that is used. */
private final int offset;
/** The count is the number of characters in the String. */
private final int count;
/** Cache the hash code for the string */
private int hash;
public int length()
return count;
public boolean isEmpty()
return count == 0;
public int hashCode()
int h = hash;
if (h == 0 && count > 0)
int off = offset;
char val[] = value;
int len = count;
for (int i = 0; i < len; i++)
h = 31*h + val[off++];
hash = h;
return h;
s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
14. StringBuffer
继承自AbstractStringBuilder实现Serializable,charsequence
默认16个,线程安全的因为加了synchronized在大多数的方法上
public StringBuffer()
super(16);
主要的属性:count和char[] values
15. StringBuilder
继承自AbstractStringBuilder 实现了Serializable, Charsequence.
线程不安全,默认是16字符
主要属性Count和char[] values
16. Thread
实现了Runnable
Thread中存在组的概念
每次启动的时候都会添加到组中去,填充线程中需要知道的信息,调用native start方法启动线程,启动后自动执行run
17. ThreadLocal
无集成和实现,线程安全,因为作为线程内部成员变量,线程本身有个threadLocals然后设置的时候将这个值设置为new 出来的 ThreadLocalMap变量就可以了。
ThreadLocalMap这个里面会把key包装成WeakReference。
public void set(T value)
Thread t = Thread.currentThread();
ThreadLocalMap map = getMap(t);
if (map != null)
map.set(this, value);
else
createMap(t, value);
static class Entry extends WeakReference<ThreadLocal>
/** The value associated with this ThreadLocal. */
Object value;
Entry(ThreadLocal k, Object v)
super(k);
value = v;
以上是关于Java源码总结整理注意点的主要内容,如果未能解决你的问题,请参考以下文章