public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String) anObject;
int n = count;
if (n == anotherString.count) {
int i = 0;
while (n-- != 0) {
if (charAt(i) != anotherString.charAt(i))
return false;
i++;
}
return true;
}
}
return false;
}
複製代碼
public boolean equals(Object obj) {
return (this == obj);
}
複製代碼
區別以下:
做用域 當前類 同包 子類 其餘
public √ √ √ √
protected √ √ √ ×
default √ √ × ×
private √ × × ×
複製代碼
public class MyUtil {
private MyUtil() {
throw new AssertionError();
}
public static <T> T clone(T obj) throws Exception {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bout);
oos.writeObject(obj);
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bin);
return (T) ois.readObject();
// 說明:調用ByteArrayInputStream或ByteArrayOutputStream對象的close方法沒有任何意義
// 這兩個基於內存的流只要垃圾回收器清理對象就可以釋放資源
}
}
class CloneTest {
public static void main(String[] args) {
try {
Person p1 = new Person("Hao LUO", 33, new Car("Benz", 300));
p1.clone();//淺克隆
Person p2 = MyUtil.clone(p1); // 深度克隆
p2.getCar().setBrand("BYD");
// 修改克隆的Person對象p2關聯的汽車對象的品牌屬性
// 原來的Person對象p1關聯的汽車不會受到任何影響
// 由於在克隆Person對象時其關聯的汽車對象也被克隆了
System.out.println(p1);
} catch (Exception e) {
e.printStackTrace();
}
}
}
複製代碼
int a = 10;
//裝箱操做
Integer integer1 = Integer.valueOf(a);
//拆箱操做
Integer integer2 = new Integer(5);
int i2 = integer2.intValue();
複製代碼
public class Main {
public static void main(String[] args) {
Integer y = 10;
int c = i;
}
}
複製代碼
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
複製代碼
protected Object clone() throws CloneNotSupportedException {
if (!(this instanceof Cloneable)) {
throw new CloneNotSupportedException("Class " + getClass().getName() +
" doesn't implement Cloneable");
}
return internalClone();
}
複製代碼
開發中的泛型擦除案例php
public class GenericTest {
public static void main(String[] args) {
new GenericTest().testType();
}
public void testType(){
ArrayList<Integer> collection1 = new ArrayList<Integer>();
ArrayList<String> collection2= new ArrayList<String>();
System.out.println(collection1.getClass()==collection2.getClass());
//二者class類型同樣,即字節碼一致
System.out.println(collection2.getClass().getName());
//class均爲java.util.ArrayList,並沒有實際類型參數信息
}
//輸出結果
//true
//java.util.ArrayList
}
複製代碼
如何獲取泛型的具體的類型?技術博客大總結java
public class GenericTest {
public static void main(String[] args) {
swap(new String[]{"111","222"},0,1);//編譯經過
//swap(new int[]{1,2},0,1);
//編譯不經過,由於int不是引用類型
swap(new Integer[]{1,2},0,1);//編譯經過
}
/*交換數組a 的第i個和第j個元素*/
public static <T> void swap(T[]a,int i,int j){
T temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
複製代碼
public class GenericTest {
public static void main(String[] args) {
new GenericTest().testType();
int a = biggerOne(3,5);
//int 和 double,取交爲Number
Number b = biggerOne(3,5.5);
//String和int 取交爲Object
Object c = biggerOne("1",2);
}
//從x,y中返回y
public static <T> T biggerOne(T x,T y){
return y;
}
}
複製代碼
Number b = biggerOne(3,5.5);
改成String c = biggerOne(3,5.5);
則編譯報錯:Error:(17, 29) java: 不兼容的類型: 推斷類型不符合上限
推斷: java.lang.Number&java.lang.Comparable<? extends java.lang.Number&java.lang.Comparable<?>>
上限: java.lang.String,java.lang.Object
複製代碼
int count = 0;
private void startThread() {
for (int i = 0;i < 200; i++){
new Thread(new Runnable() {
@Override
public void run() {
for (int k = 0; k < 50; k++){
count++;
}
}
}).start();
}
// 休眠10秒,以確保線程都已啓動
try {
Thread.sleep(1000*10);
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
Log.e("打印日誌----",count+"");
}
}
//指望輸出10000,最後輸出的是9818
//注意:打印日誌----: 9818
複製代碼
private static AtomicInteger atomicInteger = new AtomicInteger(1);
static Integer count1 = Integer.valueOf(0);
private void startThread1() {
for (int i = 0;i < 200; i++){
new Thread(new Runnable() {
@Override
public void run() {
for (int k = 0; k < 50; k++){
// getAndIncrement: 先得到值,再自增1,返回值爲自增前的值
count1 = atomicInteger.getAndIncrement();
}
}
}).start();
}
// 休眠10秒,以確保線程都已啓動
try {
Thread.sleep(1000*10);
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
Log.e("打印日誌----",count1+"");
}
}
//指望輸出10000,最後輸出的是10000
//注意:打印日誌----: 10000
//AtomicInteger使用了volatile關鍵字進行修飾,使得該類能夠知足線程安全。
private volatile int value;
public AtomicInteger(int initialValue) {
value = initialValue;
}
複製代碼
Integer x = new Integer(123);
Integer y = new Integer(123);
System.out.println(x == y); // false
Integer z = Integer.valueOf(123);
Integer k = Integer.valueOf(123);
System.out.println(z == k); // true
複製代碼
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
複製代碼
static final int low = -128;
static final int high;
static final Integer cache[];
static {
// high value may be configured by property
int h = 127;
String integerCacheHighPropValue =
sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
if (integerCacheHighPropValue != null) {
try {
int i = parseInt(integerCacheHighPropValue);
i = Math.max(i, 127);
// Maximum array size is Integer.MAX_VALUE
h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
} catch( NumberFormatException nfe) {
// If the property cannot be parsed into an int, ignore it.
}
}
high = h;
cache = new Integer[(high - low) + 1];
int j = low;
for(int k = 0; k < cache.length; k++)
cache[k] = new Integer(j++);
// range [-128, 127] must be interned (JLS7 5.1.7)
assert IntegerCache.high >= 127;
}
複製代碼
Integer m = 123;
Integer n = 123;
System.out.println(m == n); // true
複製代碼
//好比下面就會編譯錯誤
String s = null;
s instanceof null
s instanceof Integer
複製代碼